THINGS YOU REALLY SHOULD KNOW IF YOU USE A COMMAND LINE
Eric Saunders, October 2007
Here is a small of the most critical commands needed to make reasonable use of
the unix command line. A small number are specific to the c-shell. This list is
by no means complete. The general rule is: if you want to do something with
files in unix, there's probably a tool to do it already, better and faster than
anything you will come up with.
****The tcsh shell****
CTRL-A Move cursor to start of line
CTRL-E ... to end of line
CTRL-D Provide a list of completions
* All files, e.g. 'rm *' = 'delete all files'
| 'Pipe': chain two processes
>
Redirect output to a file
alias name command Create an alias for command called name
.cshrc File for storing preferences, aliases etc.
****History functions****
history Print your command line history
command Execute the last line matching command
!$ Use the arguments of the previous command
****Basics****
man command Get useful information on command
ls List files in current directory
cd dir Move to directory dir
mkdir dir Create a directory called dir
rm files Delete files
rm -r dir Delete dir and all files and subdirs below
mv file1 file2 rename file1 to be file2
****Finding files****
locate file Find matching files indexed by the system
find . -name 'file' Find file if it exists here or in any subdir
find . -mmin -20 Find all files modified in the last 20 minutes
****Using text files****
cat file Print the contents of file to the terminal
head file Print the first few lines of a file
tail file Print the last few lines of a file
less file Print file one screenful at a time
grep text file Find all lines that match text in file
grep -v text file Find all lines that
don't match
wc file Get the number of words and lines in file
diff file1 file2 Print the differences between file1 and file2
****Packaging files****
gzip file Compress ('zip') a file
gunzip file Uncompress a zipped file
tar cvzf file.tar.gz files Make a single zipped tarball containing files
tar xvzf file.tar.gz Extract files from zipped tarball
tar tvzf file.tar.gz Look at files in tarball without extracting
****Managing processes****
top View system resource usage (CPU, memory etc.)
ps aux List all processes
kill proc_id End the process with id proc_id
kill -9 proc_id
Really end it
****Connecting to other machines****
ssh -Y user@machine Log in to machine as user, with X forwarding
scp user@machine:path/to/file . Copy file from remote machine to current dir
****Examples****
1) Find the size of all files in the current directory, in 'human' units.
ls -lh *
2) Print all lines in all files ending in '.dat' containing 'star08' (upper
or lower case), and print the line numbers.
grep -in star08 *.dat
3) Find all perl code in your home directory or any subdirs.
cd
find . -iname '*.pl' -or -iname '*.pm'
4) Alias an annoyingly long log-in command to the new command 'pinky'.
alias pinky 'ssh
user@pinky.astro.ex.ac.uk'
5) Find all occurences of function 'do_calc' that are not function calls in all
fortran files in the current directory, and write them to a file called
'do_calc_definitions'.
grep do_calc *.f90 *.F | grep -iv 'call' > do_calc_definitions
6) Create an alias for your .cshrc file to print the 20 most recent files in the
current directory, from oldest to newest:
alias newest 'ls -lrt \!* | head -n 20'