Everyday UNIX Commands
2007
find
Recursively find and print all files, having ‘txt’ extention:
find ./ -type f -name "*.txt"
The same but case insensititve:
find ./ -type f -iname "*.txt"
cat all found ‘txt’ files:
find ./ -type f -name "*.txt" -exec cat '{}' \;
rm all found ‘txt’ files, starting with capital letter:
find ./ -type f -name "[A-Z]*.txt" -exec rm '{}' \;
rm all except ‘txt’ files:
find ./ -type f ! -name "*.txt" -exec rm '{}' \;
grep
Find and print all lines in all files, containing ‘tester’:
grep tester *
Find and print all lines of all files, which do not contain ‘tester’:
grep -v tester *
The same but recursively:
grep -r -v tester *
The same but case insensitive:
grep -i -v tester *
The same but coloured:
grep -n -r --color=auto -v tester *
processes
simple Perl daemon:
perl -e 'use POSIX qw(setsid); fork; setsid; sleep 1, print $c++,$/ while 1'
checking the process existance: ps x | perl
another way to find the process id: pgrep perl
kill all processes, related to perl interpreter:pkill perl
monitor all processes within 1 second interval:top -d1
monitor all processes, which access the storage devices:iotop -d1
monitor all open files (pipes, sockets, directories, etc.):lsof
archive
Archive directory and all files and sub-directories:
tar cvf home.tar /home
The same but gzip compresses:
tar zcvf home.tar.gz /home
The same but bzip2 compressed:
tar jcvf home.tar.bz2 /home
“untar” gzip compressed archive:
tar zxvf home.tar.gz
“untar” bzip2 compressed archive:
tar jxvf home.tar.gz
column extraction, assuming, that all user names are needed “/etc/passwd”:
cut command
cat /etc/passwd | cut -f1 -d:
awk script
cat /etc/passwd | awk -F':' '{print $1}'
Perl source code
perl -ne '/(\w+)/ && print $1,$/' /etc/passwd
Ruby source code
ruby -ne 'puts $1 if /(\w+)/' /etc/passwd
ISO image manipulation
ISO image creation:
dd if=/dev/cdrom of=/tmp/cdr.iso
mkisofs -rJTV "books label" /home/books > /tmp/books.iso
ISO image reading:
mount -o loop -t iso9660 /tmp/books.iso /mnt/isoimage