Everyday UNIX Commands
Posted by root Thu, 23 Aug 2007 15:23:00 GMT
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 in 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 *
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
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
Let's assume that we need all user names from "/etc/passwd", which is the 1st column (the columns delimiter is : ):
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