Difference between revisions of "Selected Linux commands"
m (→Searching: + pipe character) |
m (→Searching: +regular expression search) |
||
Line 104: | Line 104: | ||
# find from the upper directory level | # find from the upper directory level | ||
find ../ -name index.html | find ../ -name index.html | ||
+ | # regular expression search: | ||
+ | # find a file with extension .svg and ä-something in the current subdirectory e | ||
+ | find ./e/ -name '*' | grep ä.*\.svg | ||
</source> | </source> | ||
Revision as of 08:51, 15 October 2010
Contents
Tips & first steps
Command-line tricks:
- Type Control-R and start typing = will match last command that started that way
- Tab will autocomplete everything, but from all available commands
Most useful to quickly locate commands, executable is "locate", such as:
locate memcached # or
whereis memcached
apropos ftp # searches for all ftp programs
Traditional find files uses:
find / -name 'file.ext' # "/" to start at root, option -name to search for file names
Very useful to find software which may not yet be installed:
apt-cache search YourSearchTerm
Get help
apropos ftp # searches for all program descriptions with ftp
man 7z # manual page for 7z
# shift + ? → searching a string (by regular expression)
# shift + n/n → find the next/previous match
# q → quit
Most commands provide a help option and the following works:
a-command --help
a-command -h
# sometimes there is an info on usage
a-command --usage
Stop services / demons
/etc/init.d/apache2 stop
/etc/init.d/memcached stop
/etc/init.d/mysql stop
/etc/init.d/tomcat5.5 stop
/etc/init.d/webmin stop
to restart (template to copy and use directly):
/etc/init.d/apache2 start
# /etc/init.d/memcached start
/etc/init.d/mysql start
/etc/init.d/tomcat5.5 start
/etc/init.d/webmin start
Fedora may or may not be installed under the main tomcat. As of 2009-08, it can be started/stopped using (fedora folder is a softlink to current installed version; alternatively one can use "$FEDORA_HOME"):
/usr/share/fedora/tomcat/bin/shutdown.sh
/usr/share/fedora/tomcat/bin/startup.sh
Disk usage and rights
How much space on disks?
df -l # diskfree, local disks only
df -lh # diskfree, human friendly (size in MB, GB, etc.)
df . # diskfree current disk
Disk usage = "tree size": where is the space used:
du -h --max-depth=1 # '''analyze only 1 level of directories deep'''
# -h = human readable, MB, GB, factor 1024; -si would be factor 1000.
du -S # do not add up content of folders, keep values Separate (useful for manual analysis)
For rights, it is often necessary to change the group of a file or folder. It is not necessary to change the owner as well (chown -R).
ls -l # will display owner and group names
ls -g # will only display group names (easier)
chgrp -R # R: do it recursively
When copying folder trees, it is easy to loose essential information. Use
cp -pr # preserve owner, rights, etc., copy recursively;
# but devices, sockets, etc. still are not handled, if this is necessary use:
tar -p
Searching
Locate does not work, and using Find is tough. Good info: http://content.hccfl.edu/pollock/unix/findcmd.htm Example for find:
# find from the root level
find / -name index.html
# find from the current directory level
find ./ -name index.html
# find from the upper directory level
find ../ -name index.html
# regular expression search:
# find a file with extension .svg and ä-something in the current subdirectory e
find ./e/ -name '*' | grep ä.*\.svg
For searching inside files, use grep (-r searches recursive, but only within the file pattern):
grep -r "<script>" *
grep -r "x" index.html would not work! But using the “pipe” (|) character and combine the two commands will do the trick:
# find but only with content <script>-something
find ./ -name index.html | grep -r "<script>" *
Other commands
-
less
to read,tail
to read end of log file,nano
: an easier editor on debian 4 with syntax highlighting set globally in /etc/nanorc. -
ls -lap
to better see all files, a for hidden, p to see folders marked with trailing "/"
-
cat /etc/passwd
to see user list, cat = list, here: the password file -
cat -v filename
to display non-printing characters so they are visible. If the file has been edited on a Windows machine it can sometimes add CR/LF (VM) characters on the end of each line (hidden by default on most editors), so #!/bin/sh becomes #!/bin/shVM. This causes error: bad interpreter ^M. To remove such characters, use e.g.cat infilename | tr -d "\r" > outfilename
-
adduser, deluser, passwd
more on user management: http://www.cae.wisc.edu/site/public/?title=linaccounts - "taskmanager":
ps -ef
list all processes, including services, usekill (number)
orpkill (name)
- "taskmanager" interactive:
top
ortop u root
list all processes;u
switches the user;Shift+F
change sorting column;>
or<
switches sorting to → or ←;Shift+R
alters descending or ascending column sorting;k
kill;c
show command paths instead of process names;q
quit - kill processes by name: for example
killall memcached
vnc = special vnc user, not sure whether useful.
A source helping to understand the Linux file system is: http://www.pathname.com/fhs/pub/fhs-2.3.html
Install commands
- To find package names use:
-
aptitude search (keyword)
-
apt-cache search (keyword)
-
- Clean-up:
-
apt-get autoremove
will remove packages no longer needed. -
apt-get clean
will clear the repository completely
-
-
dpkg -l
to list all packages with codes for status, e.g.'ii' for installed,'rc' for removed, but configuration files still there;dpkg -s (package name)
to get information on the status of a package
ls colors
Putty displays ls out in colors. These are:
- Executable files: Green
- Normal file : Normal
- Directory: Blue
- Symbolic link : Cyan
- Pipe: Yellow
- Socket: Magenta
- Block device driver: Bold yellow foreground, with black background
- Character device driver: Bold yellow foreground, with black background
- Orphaned syminks : Blinking Bold white with red background
- Missing links ( - and the files they point to) : Blinking Bold white with red background
- Archives or compressed : Red (.tar, .gz, .zip, .rpm)
- Image files : Magenta (.jpg, gif, bmp, png, tif)
OR (other source):
Type Foreground Background Folder/Directory blue (default) Symlink magenta (default) Socket green (default) Pipe brown (default) Executable red (default) Block blue cyan Character blue brown Exec. w/ SUID black red Exec. w/ SGID black cyan Dir, o+w, sticky black green Dir, o+w, unsticky black brown
Renaming file extensions
Linux does not support wildcards in the target of a move command the way Windows does. The equivalent for Windows:
rename *.jpeg *.jpg
is
for files in *.jpeg;
do n=${files/.jpeg/.jpg}; # saves to variable $n
mv $files $n;
done
Related: To add a prefix use:
for i in *.jpg; do mv -i "$i" "XXX_$i"; done
Archiving
With the general zip, p7zip-full etc. installed, the following commands work:
# -9 is optional, higher compression
zip archivename.zip file.sql /folder -9
unzip archivename.zip
# for real good compression use:
# (a = add, -mx7 and -mx9 = higher compression, x = extract)
7z a archivename.7z file.sql /folder
7z x archivename.7z
Note: Because 7z will not store owner or group information, the option -r = recurse into subfolders is not recommended. To archive folders use (where ! is the folder name, as in WinSCP custom commands):
# tar + 7z a folder: cf = create file, a = add, -si = Read data from StdIn
tar cf - "!" | 7za a -si -mx7 "!.tar.7z"
# tar/7z to folder: x = eXtract with full paths, -so = Write data to StdOut, bd = Disable percentage indicator
# xf = extract file
7za x -so -bd "!" | tar xf -
Note: inside WinSCP, the tar/7z command to folder results in error (ok in ssh), but simply selecting SKIP results in correct result, this seems to be more a bug of the way WinSCP handles messages than of the process (?).