Friday, August 31, 2012

Short List of Useful Unix Commands

These commands should work for bash shell and other Bourne-style shells.

Environment variables 

env - List all existing environment variables
echo $<variable_name> - Check value for some particular environment variable (e.g. echo $JAVA_HOME
export <variable-name>=<java-install-dir> - Set an environment variable:(e.g. export JAVA_HOME=/opt/jdk1.6_20)

To set environment variables permanently (instead for current session only) you'll need to add export line (see above) in your .profile file (${HOME}/.profile) if you are using one of the Bourne-style shells, or in ${HOME}/.bash_profile or .bash_login if you are using bash shell in particular. 


Navigation and listing content

cd <dir_name> - Go to specified subdirectory.
cd ~ - Go back to home directory (from wherever you are)
cd .. - Go back one directory (space is required)
pwd - Show current position as a full path

ls - Lists all files/directories in a current director my
ls <dir> - Lists files/directories in specified directory 
dir <dir> - Same as above (just like in DOS) 
ls -l <dir> - List and show date, size and permissions 
ls -F <dir> - Show file types ("/" = directory, "*" = executable) 
ls -R <dir> - List dir and all subdirs (recursive listing) 
ls <dir> | more - Show listing one screen at the time


Search, sort, compare files

find / -name *.java - finds all files whose name ends with .java (/ tells that search should start from the root directory, if "/" is missing search will start in the current directory)
grep '<pattern>' <file> - Find regular expression in file (e.g. grep --color=auto -R 'main' *.java to find all 'main' occurrences in all java files in current directory and subdirectories (-R) painting them differently for better visibility (--color=auto))
tail -200f <fiel1> grep | <filter> - Show last 200 lines of file1 looking for specified regular expression (e.g. tail -400f SomeClass.java grep | 'public static')

diff <file1> <file2> - Show the differences
sdiff <file1> <file2> - Show files side by side
sort <file1> > <file2> - Sort file1 and save as file2 
sort -o <file> <file> - Replace file with sorted version


Processes

ps - list processes (e.g. ps -ef | grep java will list a full information (-ef) about all java (grep java) processes currently running)
top - list all processes ordered by CPU time consumption
nohup - command which will ignore hangup signal enabling the command to keep running after the user who has issued the command has logged out
command & - run command in the background (e.g. nohup some_command & will run some_command in the backup and keep it running after the user has logged out)
kill - kill a process (e.g. kill -9 1389 will kill process with id 1389)


Wildcards

* - Match any string of characters (e.g. test* gets test1, and test.txt)
? - Match any single character (e.g. test? gets test1, test2, but not test14)
[...] - Match any characters in a range (e.g. test[1-2] gets test1, and test2)

. - The current directory 
.. - Parent directory ~ - Home directory (e.g. cd ~ takes you home)

No comments:

Post a Comment