Common Unix Commands

Feel free to add commands, change the formating, or otherwise modify this page. Recommended UNIX book: Learning the UNIX Operating System, Fifth Edition, by by Jerry Peek, Grace Todino-Gonguet, and John Strang For additional bash commands, see

Some General Information

Top Directory: The top (or root) directory starts with a /, such as /work or /home. If a directory location does not start with a slash, such as unix/newfiles, then this directory chain is located within the currect directory (so if the currect directory is /home/user, then the full location of this sub directory is /home/user/unix/newfiles)

Wildcards: When using Unix commands, you can refer to multiple objects using wildcards. For instance, if you want to move all the pdf files in the currect directory into a second directory dir2, you would say
mv *.pdf dir2
The * wildcard refers to multiple characters, so, in the example, any characters are allowed in the filename as long as it ends in .pdf. This is probably the most used wildcard. Additional wildcards are ?, which means "any single character", and the range wild card. The range wildcard works like this. Say I want to view all of the files and directories that start with any letter from a to h. I would say
ls [a-h]*
where the * wildcard allows for the rest of the filename to be whatever it happens to be. Note that this will also result in applying ls to any directories within that a-h first letter range, not just files, and the directory list will not have any restrictions on the files displayed (it will display all of them, not just ones that start with a-h)

The ls Command: file information

This shows a list of the files in the current directory. There is a variety of different information you can get out of this command, depending on how you call it. For instance,
ls -a
shows all of the files, including files that begin with a period, such as .bashrc
ls -l
shows the files ordered by the filename, and also informs you as to whether they are readable, writeable or executable (rwx), the user, the last time when they were changed, and the file size. If there are directories, the full size is not shown- instead it usually gives the number 4096.
ls -ltr
shows the same information as with -l, but the files are ordered by the time that they were created
ls -color
color-codes the contents according to type of file.

The cd Command: changing directories

cd  dir
This moves you to the directory specified (for instance, dir might be /work/hx5/). By using
cd ../ 
you are moving to the directory directly above the current directory
cd - 
this moves you to the directory you were last in

The rm Command: removing files

rm file
This removes the file file. Depending on your settings, it will ask if you are certain you want to remove the file. Be careful when using wildcards as you might be removing more than you'd like!
rmdir dir
This removes the directory dir as long as it is empty. To remove a non-empy directory (and of course, its contents) see below
rm -r dir
This will descend into the directory dir and recursively remove all files and directories therein. Be careful! This will remove the whole directory without warning messages, so make sure you really want to remove all of the contents before using.
rm -f file
This option will force the removal of file file, without prompting you. Use this with care as you can end up deleting a lot of things by accident. Example of use: rm -rf dir will delete the entire directory dir and its contents without prompting.

  • note: removing symbolic links with these commands can be a little aggravating, especially if you use tab complete. Say you had a symbolic link named 'blah'. If you try 'rm blah/' it gives you an error that says blah/ is a directory and therefore rm cannot remove it. If, on the other hand, you were to try 'rmdir blah/', it tells you that blah/ is indeed not a directory and rmdir cannot remove it. So what do you do? You use 'rm blah' without the '/'.

The mkdir Command: making directories

mkdir dir
This command makes a new directory, dir, in the current directory (unless you list the whole location, from the root directory down, such as /home/root/dir)

The mv Command: moving things (aka renaming or relabeling things)

mv file1 file2
This command "moves" file1 to file2. This is the same as renaming file1, as long as file2 does not exist. In reality, the file is not moved, but the label used to refer to it is.
mv dir1 dir2
This command moves directory dir1 into dir2. Now, there is dir2/dir1, where dir1 still contains all of the files and subdirectories it did before the move
mv dir1 dir2
This command moves directory dir1 into dir2. Now, there is dir2/dir1, where dir1 still contains all of the files and subdirectories it did before the move

The cp Command: copying files

cp file1 file2
This command makes a copy of file1 called file2

cp -R dirtocopy/ newdir/
This copies an entire directory.

The scp Command: copying files/directories to another server

This is how to copy files:
scp SourceFile user@host:directory/TargetFile
scp user@host:directory/SourceFile TargetFile
This is how to copy an entire directory:
scp -rp directory/ remotehost:

The ln Command: linking files

ln -s file1 file2
This command makes a "soft" link of file1 called file2, so file1 is the original file name and location, and file2 is the new link name and location. "Soft" just means that it won't take up extra memory in the directory it is linked to, unlike an actual copy. If you do an ls -ltr, the link will show up in light blue, with an arrow pointing to the original file. You can also remove a link without removing the original file. However, if you remove the original file, or move it to a new location, the link will be broken, and will show up in red highlight if you do an ls -ltr.

The chmod Command: change file permissions

chmod a+rwx file 
This command changes the permissions so that the file is readable, writable, and executable by everyone. If you can't open a file, it is probably because the permissions are set incorrectly. If you can't run a program, it is probably not set as executable. You can also be more precise about it and only change the u, g, or o level and only + or - the r, w, or x label, so to make the o level executable I might say
chmod o+x file 
where earlier the "a" stood for all (u, g and o).

The passwd Command: changing password

This command will start a subprogram that will change your password- it will ask for the current password and then ask for your new password. It will quit if the currect password is incorrect. NOTE: at CERN, passwords are now changed online, not with this command.

The grep Command: locating a phrase

grep 'phrase' ./
This command will search the current directory (the dot-slash at the end of the line) for the term phrase and return the names of files it is contained within (as well as the interior of the file, around where it is located).
grep -r 'phrase' ./
This does the same thing, but will search subsequent directories as well
grep -h 'phrase' ./
This lists other options for this function Note: this command has quite a lot of usage options beyond this, feel free to google grep and learn more.

The which Command: locating an executable

which kpdf
This will return the location of the kpdf program, even if it is not in the current directory structure. If it can't find it, it will tell you where it could not find it.

The df Command: disk space usage

This will return the location amount of space left on disk.

The diff Command: differences in files

diff file1 file2
This will return lines that are different between the files, by printing out the lines.

The echo Command: echos the location

This will whatever the argument points too. For the example, it will return the root system location. $ROOTSYS would also be specified in your .bashrc file, for instance.

The nohup Command: keeps program running after logging off

nohup python &
This command will keep a program running after a logoff. It is particularly useful if you are logged into a computer remotely, as servers have a tendency to log you off after a few hours, whether you want to or not. The & symbol at the end causes the program to run in the background.

The nice Command: reduces priority of the thing you are running

Using example above, to make it 'nice':
nohup nice python &
The nice command reduces the priority of what you are running. If your program will take a long time or a lot of CPU, it will be appreciated by other users if you use this command.

The ps command: shows processes running on your machine

ps-aux shows processes of all users, username, %CPU, start time, status and more.

How to write a script to automize stuff

start with:
comment out with:
now you can use all your normal shell commands
run your script by just typing in the name
you'll have to change the rights so that you can execute your script by typing:
 chmod +x nameOfScript

how to manipulate text files from the command line

echo 'whateverText' >> newFile 
puts "whateverText" into a (newly created) file calles "newFile"
if you want to put text into an already created file, do the same and it will add the text in the end
sed 's/dog/Stinktier/g' words.txt > words2.txt 
copies "words.txt" to "words2.txt" but substitutes every occurence of "dog" for "Stinktier"
sed -i 's/dog/Stinktier/g' words.txt 
replaces "dog" with "Stinktier" within the textfile "words.txt"

To find the host name of the computer you're working on.

Just open a terminal and type

-- SarahHeim - 26 Jan 2009 -- JennyHolzbauer - 14 Jul 2008 -- JennyHolzbauer - 24 Jun 2008 -- EmilyJohnson - 30 Apr 2008 -- JennyHolzbauer - 07 Apr 2008
Topic revision: r31 - 28 May 2010, SarahHeim

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback