Thursday, May 1, 2008

Useful Linux Commands

Command to find files accessed in last 30 days. will find files that is accessed in last 30 days, under root folder.
# find / type f -atime -30
----------------------------------------------------------------------------------------------------------------------------------------

List contents of a folder along with contents of its subfolder. But it will traverse only to a depth of one. ie, it will not show the contents of subfolder's subfolder.
# ls *
-----------------------------------------------------------------------------------------------------------------------------------------

To print the iptables rules along with line number.
# iptables -L --line-numbers
-----------------------------------------------------------------------------------------------------------------------------------------

To find a particular rule with rule number #; where # is the rule number you want to list
# iptables -L OUTPUT --line-numbers | grep ^#
-----------------------------------------------------------------------------------------------------------------------------------------

Change permission only for folders
# find . -type d -exec chmod 755 {} \;
-----------------------------------------------------------------------------------------------------------------------------------------

List with 777 permission
#find . -type d -perm 777
--------------------------------------------------------------------------------------------------------------------------

To list all the processes listening to port 80
# lsof -i TCP:80|awk {'print $2'}
-----------------------------------------------------------------------------------------------------------------------------------------

To kill all the process listening to apache port 443/80
# lsof -i TCP:443|awk {'print $2'} | xargs kill -9
-----------------------------------------------------------------------------------------------------------------------------------------

Recursively chmod only directories
find . -type d -exec chmod 755 {} \;
-----------------------------------------------------------------------------------------------------------------------------------------

Recursively set the execute bit on every directory
chmod -R a+X *
The +X flag sets the execute bit on directories only
-----------------------------------------------------------------------------------------------------------------------------------------

Recursively chmod only files
find . -type f -exec chmod 644 {} \;
-----------------------------------------------------------------------------------------------------------------------------------------

Recursively chmod only PHP files (with extension .php)
find . -type f -name '*.php' -exec chmod 644 {} \;
-----------------------------------------------------------------------------------------------------------------------------------------

Find all files in /home/user/demo directory
$ find /home/user/demo -print
-----------------------------------------------------------------------------------------------------------------------------------------

Now find all files in /home/user/demo directory with permission 777
$ find /home/user/demo -perm 777 -print
-----------------------------------------------------------------------------------------------------------------------------------------

Next you need to apply chmod on all these files using -exec option:
$ find /home/user/demo -perm 777 -print -exec chmod 755 {} \;
-----------------------------------------------------------------------------------------------------------------------------------------

Command to find files modified on July 12
ll|grep dr|awk '{print $9}' > 123
for i in `cat 123`;do ls -ld $i;done|grep "Jul 12"
-----------------------------------------------------------------------------------------------------------------------------------------

How to See the SSH password guesses

First, find the PID of the listening SSH daemon process:
# ps axuww | egrep 'PID|ssh'
Now become root and attach to the running daemon with strace:
#
strace -f -e 'read,write' -p12345
----------------------------------------------------------------------------------------------------------------------------------------

Screen Command

Command to create screen:
# screen -S screen_name

To exit from screen:
Just close the shell without logout

To list all running screens:
# screen -ls

To login to a particular screen with screen name "xxxx.screen_name"
# screen -r xxxx.screen_name



No comments: