Delete All Directories Except

Mar 27
2010

Write a Bash function or an alias.

#######
### alias 
#######
alias _rm_except="rm -rf $(ls | grep -v ^$1$)"
 
##########
### function
##########
_rm_except () { rm -rf $(ls | grep -v ^$1$) }
 
# sample usage:
$ cd /tmp && mkdir 1 2 3 11 22 33
$ _rm_except 1; ls
11  2  22  3  33

`mkdir` and `cd` at once

Dec 16
2009

I am sick of mkdir dir_name; cd dir_name;
The solution (put this function in “.bashrc” or whatever shell config file you use):

_mkcd () {
       mkdir -p $1
       cd $1
}
# usage: _mkcd /tmp/dir_1/dir_2
# pwd will show: /tmp/dir_1/dir_2

unarchive files

Dec 14
2009

a sample BASH function (usually resides in $HOMES/.bashrc)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
extract () {
        if [ -f $1 ] ; then
                case $1 in
                        *.tar.bz2) tar xjf $1;;
                        *.tar.gz) tar xzf $1;;
                        *.bz2) bunzip2 $1;;
                        *.rar) rar x $1;;
                        *.gz) gunzip $1;;
                        *.tar) tar xf $1;;
                        *.tbz2) tar xjf $1;;
                        *.tgz) tar xzf $1;;
                        *.zip) unzip $1;;
                        *.Z) uncompress $1;;
                        *) echo "'$1' cannot be extracted via extract()";;
                esac
        else
                echo "'$1' is not a valid file"
        fi
}

how to monitor ip addresses from a log file

Jan 28
2009
tail -f production.log  | egrep "([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}"

Everyday UNIX Commands

Aug 23
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

Calendar

July 2010
M T W T F S S
« Apr    
 1234
567891011
12131415161718
19202122232425
262728293031  

Tags