Common shell commands

From Tuxwiki

Table of contents

Text file manipulation

Extract each 10th line from a file with 5000 lines

$ perl -we 'while(<STDIN>){$.==$ARGV[0]&&print&&shift&&(@ARGV||exit)}' `seq 1 10 5000`

Squeeze white spaces

$ sed -r -e "s/ +/ /g" inputFile

or even simpler

$ tr -s ' ' < inputFile

Sum numbers

$ cat numbers
23
1
145
33
999
$ cat numbers | awk '{s+=$1} END {print s}'
1201


Delete lines by pattern

Delete all lines which end with /CVS from a file

$ sed -e '\,.*/CVS,d' inputfile

Delete all empty lines

sed -e '/^$/d' inputfile

Disable the audio bell

setterm -bfreq 0

Switch columns in a text file

$ cat my.txt
X1 X2
A1 A2
Z1 Z2

Desired output:

$ cat my.txt
X2 X1
A2 A1
Z2 Z1

Solution:

$ cat my.txt | awk '{print $2, $1}'
X2 X1
A2 A1
Z2 Z1

Additional sed tips: http://www.tty1.net/sed-tutorium/html/ar01s03.html

find

Find all c and cpp files and search a word in them

$ find . \( -name "*.c" -o -name "*.cpp" \)  -exec grep "main" {} \; -print

grep

sed

The basic sed algorithm

awk

The basic awk algorithm

cpio


  • List contents of cpio archive
$ cpio -tv < file.cpio
  • Extract cpio archive
$ cpio -idmv < file.cpio

Redirection

sh like shells

Redirect both standard input and standard error to file:

$ ls > dirlist 2>&1

Pipe both standard output and standard error to standard input of other application:

$ ls 2>& 1 | more

In any case, you can be sure that you always end up with files which are called "1" or "2" whenever you try these things...

csh like shells

Do not use them (http://www.littletux.net/serendipity/index.php?/archives/13-The-csh-madness.html). If you are forced to:

Pipe both standard output and standard error to standard input of other application:

% runSomeApp |& more

Sub shells and variables

rsync

rsync 2.6.2

$ rsync -a --files-from=/tmp/foo /usr remote:/backup

If /tmp/foo contains the string "bin" (or even "/bin"), the /usr/bin directory will be created as /backup/bin on the remote host (but the contents of the /usr/bin dir would not be sent unless you specified -r or the names were explicitly listed in /tmp/foo)

rsync 2.6.6

$ rsync -a --files-from=/tmp/foo /usr remote:/backup 

If /tmp/foo contains the string "bin" (or even "/bin"), the /usr/bin directory will be created as /backup/bin on the remote host. If it contains "bin/" (note the trailing slash), the immediate contents of the directory would also be sent (without needing to be explicitly mentioned in the file -- this began in version 2.6.4)

Inclusion and exclusion patterns

The list of inclusion and exclusion patterns is processed one pattern after the other. As soon as a pattern matches, the list iteration stops and the action (include or exclude) associated to the pattern is performed.