2012-09-03

Useful Unix Commands

Preprending a line to a file under unix, do echo "This is my line" | cat - filename > newfile. You can't redirect filename to filename itself , though.

A little shell trick to remove the first lines from a file is tail -n +x file where x is the number of the first line you want in your output. So to remove the first line from a file yould set it to +2.

Restarting apache: /etc/init.d/httpd restart

Restarting samba: The documentation of Samba tells you it'll reload it's config file (usually /etc/samba/smb.conf) when sent a SIGHUP signal, which translates to a kill -1 pid (see man signal -S 7 for the list of signals). You also can restart it, with the usual /etc/init.d/smb restart, just like you'd do with other daemons.

To zip/unzip .zip files unter unix, use zip/unzip instead of gzip/gunzip. See here. For cleaning up your directory tree under KDE, kdirstat is nice.

Using rpm:
rpm -U packagefile install (really update)

rpm -e package erease

rpm -qa query: list all installed packages

rpm -ql package query: list all files installed for package


When the installer complains that a library is missing, e.g.

error: failed dependencies:

libblas.so.3 is needed by octave-2.1.35-4

go to rpmfind.net, and put in the name of the library to find the RPM that provides it, download that and install it. When looking for file names, try leaving out the .number at the end, to find the more up to date versions, too. Apache, by the way, is called httpd here.
Basic I/O redirection: see Unix Shell.
tee file pipe input to output and to file (like a 'T' crossing: one way in, two out)
cmd1 | xargs cmd2 use the output of cmd1 as args for cmd2
Finding Stuff
find dir -name 'pattern' files in subtree with pattern in the name. find . -name *.txt -exec grep -l 'myword' '{}' \; lists all text files that contain myword. The -l grep option prints the filename instead of the line. The quotes protect the {} and the search term from shell interpolation. Alternatively, one can use grep myword /dev/null, because /dev/null will be treated as a second file name, and when there is more than one filename, the name is printed by default (the -H option would do the same). The {} is the placeholder for the filename, the \; is needed to pass a ; through to find, so it knows where the -exec arguments end.
find dir -iregex 'pattern' files in subtree with pattern in the name, using case independent real regexes (bash only).
locate pattern prints from a nightly built index of the filesystem all names containig pattern. (fast)
which name print full path to name prog. Useful if there are several versions istalled, and you want to know which one is first on the path.
man [-k] [-sn] name print the manual entry for name. -k searches full text ("keyword"). -sn forces the search in a section. Section1 is the commands you usually need. Man pages are usually stored under /usr/(local/)man/man[1-6]
System information

cat /proc/cpuinfo Find processor information under linux on the command line.
alias all aliases
showrev, uname [-p] processor and kernel information. showrev -p option prints list of installed patches. uname -a gives all kernel information. To find out which linux release is installed, on Redhat/Fedora, you can also try less /etc/redhat-release
df [-k] free disk space [in kbytes] for all file systems. du dir prints the used space for a dir.
netstat [-n] [f] [ip] adresses and status of the network
vmstat cpu usage and memory
iostat io information
nslookup ip-address domain name bound to the ip-adress
ps [-elf] active processes. -elf prints nearly all of them in long form (and is easy to remember).
top eternally print active processes until ^C
crontab -l cron jobs for current user
Displaying and formatting
Whenever input is expected, it can be given as a filename afterwards (command file.txt) or fed from another command via a pipe (command-1 | command-2).
grep[-i][-v] pattern print [case-independend] all lines in input [not] matching the pattern. Patterns . * [] ^ $ behave normally, + ? | () have to be escaped with \. \w is word (alpahanumeric), \W non-word, \b word-boundary, \B non-boundary. Inside [] [:alpha:]=a-zA-Z [:digit:]=0-9.
more or less paginate input
head [-n] prints first few [n] lines of file.
tail [-n] prints last few [n] lines of file.
cat print input
wc [-l] count bytes, words, lines [only]
dos2unix remove all \r from file (these are in there because on dos, newline is \n\r instead unixe's \n).
strings extract ascii srings from binary files. Take a look at Word or kernel error messages.
Compressing and expanding
Usually, since FTP cannot recursively get or put dir trees, these are tape-archived (tar'd) into one file, compressed with compress(.Z) or gzip (.gz), so the result is a filename.tar.gz file. This in turn has to be uncompressed and extracted to be used. Beware: DOS clients do not like two dots and tend to save this as filename_tar.tar or filename_tar.gz.
tar c[v]f filename.tar dir [verbosely] roll dir into tar ball. Be careful that dir does not end in an / as it does with auto-completion.
tar x[v]f file.tar [verbosely] unroll tar ball.
compress file compresses the file into its file.tar.Z version.
uncompress file.Z inflates back to the file version. You can also use gunzip for this.
gzip file compresses the file into its file.gz version.
gunzip file inflates back to the file version.
gunzip < file | tar xf - unzip and untar. I think the < is needed as then the data is read from a stream, and thus no internmediate file is written?
unzip file.zip unzips a .zip file
gtar xzf file unzip and untar, even shorter.
File and directory handling
ln [-s] real symbol create a [sym]link to another file. (Symlinks may span file systems). If you provide a dot for the symbol, the last name in the dirlist will be used, eg: ln -s /usr/local/pub . creates a link named pub
rm [-R] expr recursivly remove all files matching expr. recursive version also removes dirs.
cp file target copy a file.
mv source target moves (or renames, depending on your pov) file or dierectory. To move a dirctory tree into the working dir type (eg): mv ~/texts/books . To move everything fro m a dir to another: mv * another/*
ls [dir] list files in dir. common options are -F (append symbols for: dir, link etc), -A (list all including ones starting with .) -l (long with all info).
mkdir name / rmdir name make or remove directories.
touch file update the date on the file or create a new one with 0 bytes if it did not exist.
pwd print present working directory
Process handling
kill [-9] PID [thoroughly] kill process with PID.
command & run process in background.
^Z suspend process
bg send suspended process to background
fg [%n] fetch process with id n back to foreground.
Shell scripting
eval Read the arguments as input to the shell and execute them as commands. Usually used to execute commands generated by command or variable substitution (because the result is the input string that is eval'd.
test Implicitly used to evaluate conditionals.
expr Calculate mathematical expressions.
exec Read the input and execute it instead of the shell in the current process.