Sunday, 1 May 2011

Linux command tips

As I am getting older and my memory sometimes fails embarrassingly, I decided to dedicate this post for some useful bits and peaces here and there which saves a lot of time.

Find
Is an extremely useful utility specially when used with proper piping.
# This will search for the word 'xml' in every file under
# current folder that starts with . (hidden dirs)
kibo@kibo-UbuntuVM:~$ find . -regex '^.\/\..*' -exec grep xml {} \;

# Now to limit the search to directories and only first level
kibo@kibo-UbuntuVM:~$ find . -regex '^.\/\..*' -type d -prune

# To do inverse selection(everything but the ones you selected)
kibo@kibo-UbuntuVM:~$ find . -regex '^.\/\..*' -type d -prune -o -print

# Thus interestingly following prints only files not folders
kibo@kibo-UbuntuVM:~$ find . -type d -o -print

# Search everything except Desktop folder
kibo@kibo-UbuntuVM:~$ find . -path Desktop   -o -print

# Lists all symbolic links from current path downwards
kibo@kibo-UbuntuVM:~$ ls -l `find . -type l`

# List all broken symbolic links from current path downwards
kibo@kibo-UbuntuVM:~$ find -L . -type l

# List all directories with maximum 2 levels of depth
kibo@kibo-UbuntuVM:~$ find . -maxdepth 2 -type d

# Global search and replace! Very handy indeed
kibo@kibo-UbuntuVM:~$ find ./Desktop -type f -exec sed -i 's/hello/goodbye/g' {} \;


Ls
Very basic command but the most useful!
# List all directories nicely!
kibo@kibo-UbuntuVM:~$ ls -ldr */
drwxr-xr-x 2 kibo kibo 4096 2011-04-30 23:36 Videos/
drwxr-xr-x 2 kibo kibo 4096 2011-04-30 23:36 Templates/
drwxr-xr-x 2 kibo kibo 4096 2011-04-30 23:36 Public/
drwxr-xr-x 2 kibo kibo 4096 2011-04-30 23:36 Pictures/
drwxr-xr-x 2 kibo kibo 4096 2011-04-30 23:36 Music/
drwxr-xr-x 2 kibo kibo 4096 2011-04-30 23:36 Downloads/
drwxr-xr-x 3 kibo kibo 4096 2011-05-01 11:02 Documents/
drwxr-xr-x 2 kibo kibo 4096 2011-04-30 23:36 Desktop/
drwxr-xr-x 3 kibo kibo 4096 2011-05-01 11:02 Aptana Studio 3 Workspace/
drwxrwx--- 9 kibo kibo 4096 2011-05-01 21:35 Aptana Studio 3/

Ps
Good during trouble shooting along with top and nmon!
# Lists all python processes with first column(pid)
# and fourth column(cpu time)
kibo@kibo-UbuntuVM:~$ ps fax | grep python | awk {'print $1,$4'}

Rsync
Syncing two directories through SSH
rsync -av -e ssh /blah/*  host105:/opt/blah/blah 

netstat
Lists every single open port based on PID
netstat -taucp

No comments:

Post a Comment