Unix commands

Unix help
man
Directories
df report disk partition usage/space
pwd print working directory
cd change directory shorthand notations: ~ (home directory), no argument (home directory), . (current directory) , .. (one directory up), - (previous directory)
mkdir make directory
rmdir remove directory directory must be empty: rm -rf dirname will remove directory and files in it, recursively
basename report filename without leading directories
dirname report directory without trailing filename
Files
ls list files useful options: -a, -l, -F, -r, -t, -R, others
touch create new empty file
mv Rename file
cp Copy file
echo "text" >> file add text to file
chmod, chown, umask permissions commands Unix permissions cover three types of users: owner, group, world. Each of these can have read, write, execute permission, expressed in three bits, in order rwx. You can change permissions using characters, e.g., o+w,g-x, or via a permissions mask, e.g., 755 (rwxr-xr-x) or 644 (rw-r--r--)
ln link files -s for symbolic link, else hard link (hard links must be on same file system)
mkfifo Create pipe file
find find files under specified directory -name, -type, -mtime
locate locate files using system database
catdisplay contents of file to stdout (use more or less instead)
moredisplays contents by screenful
lessisplays contents by screenful
head displays specified \# of lines from top of file-n displays n lines
tail displays specified \# of lines from bottom of file-n displays n lines
od displays contents in "raw" format -a for ascii, -x for hexadecimal, -o,-b for octal (2 bytes and 1 byte)
Command line editing
grep regexp file search within files -i (ignore case), -v (reverse search, i.e. find where given expression is \textbf{not} matched
sed line-by-line processing
tr line-by-line processing with simple replacement of special characters
sort sorting alphabetically and numerically by column -n (numerical sort), --key=n by nth column
paste join files "horizontally"
join join files "horizontally", matching lines on common columns
awk File processing awk 'condition \{action}
archiving and compression
tar
gzip
compress
xzip
fpack \subsection{Unix resources and usage}
Unix resources and resource usage
memory, cpu, etc.
dushow disk usage for files/directories-s displays summary of specified files, good for directories, -k output in kB, -h outputs in "human" readable (but not sortable!)
Unix job control
psshow running process e.g. aux for all processes
top shows processes sorted by usage, updates<
kill -SIGNAL processid sends signals processes -KILL full kill, -STOP pauses, -CONT continues
Intermachine communication
ssh
scp
sftp
ftp
rsync
ssh-keys

Unix shells

function sh/bash csh/tcsh
Overall shell
Shebang! (first line of a script) #!/bin/bash #!/bin/tcsh
Multiple commands on the same line (semicolon) command1; command2; command3
Extending commands across multiple lines ( backslash) command1 argument | command2 | command3 | \
command 4 | command5 > file
Variables
Variable assignment VAR="Here is a string" set VAR="Here is a string"
Setting environment variables export VAR=" Here is a string"
(No spaces around the = sign!)
setenv VAR "Here is a string"
(No equal sign (=) when using setenv!)
Unsetting a variable unset VAR unset VAR
Aliases alias top='cd /top' alias top 'cd /top'
Conditionals
If statements: can use conditional operators: ==, !=, &&, ||, and others; string sorting with < and >. if [[ $VAR1 == $VAR2 ]]; then
  echo "True"
else
  echo "False"
fi
if ($VAR1 == $VAR2) then
  echo "True"
else
  echo "False"
endif
if
If statements with file property testing (see property table below) if [[ -d $VAR ]]; then
  echo "Directory!
fi
if ( -d $VAR ) then
  echo "Directory!"
endif
Arguments
Passing arguments to a script myscript.sh arg1 arg2 arg3 ... argN
Corresponding variables $1 $2 $3 ... $N, $* refers to all command line arguments (useful with loops!)
Assigning command output to variables (backtick) VAR=`command1; command2; command3` (bash) Set VAR="`command1; command2; command3`" (tcsh)
String replacement NEWVAR=${VAR/search/replace} set NEWVAR= "$VAR:gas/search/replace/"
Loops
For loop on a list for i in 1 2 3 4 5; do
  echo $i
done
foreach i (1 2 3 4 5)
  echo $i
end
For loop using wildcards for i in *.in; do
  touch ${i/.in/.out}
done
foreach i ( *.in )
  touch "$i:gas/.in/.out/"
end
For loop using commands for i in `cat files`; do
  grep "string" $i >> list
done
foreach i ( `cat files` )
  grep "string" $i >> list
end
While i=0
while [ $i -lt 10 ] do   echo $i   i=$[$i+1] done
set i = 0
while ( $i < 10)
  echo $i   @ i = $i + 1 end