Unix command cheat sheet

  • Find and delete files older than n days

This is particularly useful to free up space in a dir . Often log files accumulate in a dir and unless there is a house keeping job setup to clean up, you might see job failures with “No space left on device” message.   If you want to skip a particular directory , you can use the –prune option.

Command :  find /tmp/logs –name *.log –mtime +n –exec  rm –rf {} \;

-mtime – modification time in days

the first argument is the location from where you want to delete the files.

-name wildcard for searching filenames

-exec executes the command specified for the find results. ( can also pipe output to xargs instead of –exec but that would result in invoking two commands instead of 1)

 

  • Find duplicates in a file

This is useful in case some duplicates have crept in your data and cause your database statements to fail due to primary key violations.

Lets assume we have a pipe delimited text file and the third column has some duplicates that we want to identify.

cat filename | cut –f3 –d”|”  | sort  | uniq –d

uniq –d gives out the duplicates

uniq command needs sorted data to work on, hence we have used sort here.

  • Find directories taking up most space

du –s <dirname> | sort –nr | more

  • Find which process is writing a particular file

This is useful in identifying the process writing runaway log files or those filling up filesystems due to bugs.

lsof | grep <filename>

lsof lists all open filesname and processes accessing those file.

  • Check what a running process is doing in realtime.

strace -p<PID>

Gives the output of system level calls being made by the process.

  • Find and replace strings in a file.

This is useful in scenarios where you need to change some data inside a file. For e.g  a server name is is to be changed in 100 config files.

cat <filename> | s/<old>/<new>/g > tmp.filename

mv tmp.filename <filename>

or better still perl –p –I –e  ‘s/<old>/<new>/g’  <filename>

8 thoughts on “Unix command cheat sheet

  1. Very Good Read!!!

    I attended couple of your session while you were in Pune. They were very good, helped me a lot to grow.

    Regards
    Prateek

    Like

  2. Thank you so much for these real time interviewer asking questions. It will helpful for interviews for quick reading.

    Like

  3. Hi i am planning to go for an L2 support job can you please help me out in this by giving and Unix production support live project because i have a good knowledge of unix but don’t have and realtime project can you help me out in this.

    Like

  4. Hello, sir. I am planning to search job in application support/production support by putting experience. I have good knowledge in Unix, Shell Scripting and SQL but I do not have real time project. Could you please help me.
    Thanking you.

    Like

  5. Hello, sir. I am planning to search job in application support/production support by putting experience. I have good knowledge in Unix, Shell Scripting and SQL but I do not have real time project. Could you please help me.
    Thanking you.

    Like

Leave a comment