Saturday, July 9, 2011

Some useful Linux commands & tips

1) List users with cat or awk in a system
cat /etc/passwd | cut -d: -f 1,3,6 | grep "[5-9][0-9][0-9]" | grep "/home" | cut -d: -f1
awk -F: '$6 ~ /\/home/ && $3 >= 500 {print $1}' /etc/passwd

2) Count the results with the pipe wc -l
Above commands can be rewritten as following
cat /etc/passwd | cut -d: -f 1,3,6 | grep "[5-9][0-9][0-9]" | grep "/home" | cut -d: -f1 | wc -l
awk -F: '$6 ~ /\/home/ && $3 >= 500 {print $1}' /etc/passwd | wc -l

3) Grep command
Recursively search "some_string" only in python files in home directory
grep -r --include="*.py" "some_string" /home
Display the process containing "some_string"
ps aux | grep "some_string"