Unix Cheat Sheets and Tricks
While Unix is an extremely flexible, powerful, and stable operating system, mastering it can require apprenticing yourself to an expert and monitoring every keystroke. This article will hopefully grow as others add their favorite Unix tricks.
The best trick is to learn to read man pages and understand them. If you can do that, you’ll have Unix at your fingertips.
- ‘The Art of Unix Programming’ by Eric Raymond – full-text available online
- Learn 10 good UNIX usage habits – IBM
- Advanced Shell Topics
- Slicing and Dicing on the Command Line – Linux Magazine article on a host of methods for reformatting plain text — including the text used by graphical applications like spreadsheets and email programs.
- Cheat Sheets
Searches (and Replace)
- Search and replace text in a list of files: perl -pi.bak -e ‘s/OLDSTRING/NEWSTRING/g’ FILELIST
- search for tablename references in db program:
- egrep -rl -e ‘(select|insert|update)(.*)tablename’ *
- Extract username and realnames from passwd file: cut -d: -f1,5 /etc/passwd | sort
- Extract unique IP addresses (first field) from web server logs. In this case, any looking for winnt to try and track nimda worm.
- tail -10000 access_log | grep winnt | cut -d" " -f1 | sort -u
Finding Files
- find files owned by group xxxx: find . -group xxxx -print
- find files owned by user xxxx: find . -user xxxx -print
- list most recent logins (and FTP): last
- list most recent logins (and FTP) by username: last username
- to change date/time of file:
/usr/bin/touch -am -t 195401010000 filename _makes it Jan 01 1954 _ - show environment of all processes: ps -ae
- list table of contents of tarfile tar tvf tarfilename
- To change permissions on directories, not files, recursively. Here’s two examples:
- find . -type d -exec chmod 775 {} \;
- find . -type d -exec chmod g+s {} \;
- To change permissions on all htm files recursively:
chmod -R 664 *htm - To find and print out all files owned by user:
- go to /etc/passwd to find user ID number
- go to the directory where you want to start your search
- type: find . -user 30152 -print
- To find and replace owner: find . -user 30152 -exec chown newuser {} \;
- Find all php files in this directory or below
- Linux find . -name *php
- Solaris find . -name \*php
- Find all php files in this directory or below and display first few lines of each
- find . -name *php -exec head {} \;
- Remove all files that end with xxx: find . -name \*xxx -print -exec rm {} \;
- This one searches all php, pl and pm files for the term board_extras
find . \( -name \php -o -name \pl -o -name \*pm \) -exec grep board_extras {} \; -print | less - Find and chgrp to classweb for all files with other group ID
find . -not -group classweb -exec chgrp classweb {}
\; | less - Find all files with rw-r—r—: find . -perm 644 -ls | more
- chmod on them with ok? y/n: find . -perm 644 -ok chmod 664 {} \;
- chmod on all of them: find . -perm 644 -exec chmod 664 {} \; ran as root. Important to have space after {}
- Find all files named ta.inc and run search and replace to change " <?" to “<?”
find . -name ta.inc -exec perl -pi.bak -e 's/^ <\?/<?/' {} \;
- Find all files named ta.inc and print first line only (to check results of above script)
- find . -name ta.inc -exec head -1 {} \;
- Find any core files left on system{ find . -name core
Custom Printing
- Print two pages to sheet, nicely formatted, with line numbers:
- a2ps —line-numbers=1 filename | lpr
Comparing Files
- Side by side diff is an excellent way to visually compare files: sdiff file1 file2 | more
- Side by Side diff only showing lines on left if they’re the same, and 250 cols instead of 130
- sdiff -lw250 wwwboard.pm wwwboardjon.pm
- vimdiff – edit two or three versions of a file with Vim and show differences
- diff3 – Merging and More with diff3
This article was originally posted on the UCLA Programmers Wiki.