subscribe to our free newsletter

This article will go over some of the most basic commands you might need when using Linux (or any Unix variant such as Mac OS X or FreeBSD). Please remember that Linux is case sensitive. So FoO is something completely different from foo. I will use the term <filename> to represent the place where you should insert a filename. You won't need to include the brackets. For example,

rm <filename>

should be typed as,

rm bobsmith.txt

or whatever the filename is you are working with.

Table of basic Linux commands

You may have seen some of these before, but here they are in a handy table.

ls Lists all non-hidden files in the current directory. ls is equivalent to the DOS command dir. You can also specify a directory to view. For example, ls /home/bobsmith will show the files in the /home/bobsmith directory even if you are not in it. ls -a shows all files, even hidden one (in Linux a file is hidden if the filename starts with a period). ls -l gives you the long details of each unhidden file such as permissions, date modified, owner, and size. ls -al will give you the long listing of all files. Play around with this one, it won't hurt anything
rm Removes (deletes) a file. rm is the equivalent to the DOS command del. This action cannot be undone. The usage is rm <filename> You can also delete directories by typing rm -rf <directoryname> Be careful because this command will delete every file within that directory and there is no undelete command.
mkdir Makes a directory. It's really just that simple, mkdir <directory_name> will create a directory with the name you specify.
man Lists the manual page for a command. man <command_name> will give you a detailed instruction set for just about any command line command. It will show you how to use the command and will list all the options available for the command. You can even do a man man to find out more about the man command.
less Shows the contents of a file. less <filename> will print the contents of a file to standard output (i.e. the screen). If the file is more that one screen length, you can scroll down or up by using the arrow keys or page down using the space bar and page up using the U key. Press Q to quit back to the command line.
top Shows current processes and memory usage. The information is shown in real time and you can quit back to the prompt by typing Q. This is similar to the Task Manager in Windows.
cd Changes directory. cd <directory_name> is the typical usage. cd by itself will take you to your home directory. cd - will take you back one directory.
pwd Lists your current position in the directory structure.
cp Makes a copy of a file. cp <old_filename> <new_filename> will make a copy of the file. You can also specify a different directory. For example, cp foo.txt /home/bobsmith/backups/foo.txt will make a copy of the foo.txt file in your current directory into the /home/bobsmith/backups directory.
mv Moves and/or renames a file. mv <filename> <new_directory> will move the specified file to the new directory. You can also rename files while moving or rename files within a single directory. mv foo.txt foo.txt.bak will rename foo.txt to foo.txt.bak. mv foo.txt /backups/renamed_foo.txt will move and rename the file.
tar Archive a file or group of files. tar xvfz <filename.tar.gz> is the most common usage. Tar was originally designed to archive backups to tape machines, but now many Linux software packages are released as a tarred zip file (i.e. with the extension .tar.gz). The options "xvfz" will unzip and extract the contents of such a file while showing the results. Please note that you should not use the "-" before the options in tar as you will with most other commands.
ln Creates a link to a file. This is the equivalent to a shortcut in the Windows world. ln -s <file_or_directory_name> <linkname> will create a symbolic link with the name <linkname>. You should almost always use the "-s" option when using ln. The ln command can come in handy if you need to access files from a directory that is deep in a directory structure. ln -s ~/backups/bobsmail/2008/July July will create a link called July in your current working directory. The you can just type cd July instead of having to type in the full path. ls -l <linkname> will show you the file to which the link is linked.
ps Shows a list of running processes. ps by itself shows only your processes you are running in terminals. ps -a will show all terminal processes for all users. ps -ax will show all running processes, even windowed processes. This command is useful for quickly obtaining a process's ID number (PID, this is important for the following command).
kill Forces a specified process to quit. kill <PID> will cause the process to quit. Let's say that Firefox has locked up on you and you need to kill it. First go to a terminal and run ps -ax to get the PID. In this case we will say it is 11347. Now type kill 11347 and Firefox should quit gracefully. If it does not quit, you can add the "-9" option like so, kill -9 11347. This will force Firefox to quit no matter how much it protests.
sudo Allows a specified command to be executed as the administrative user. sudo <command> is the proper usage. You can use this command to do anything to any user's files or processes. Let's say your roommate started a program that is using all your system's resources and then you came over and logged into your account. If you have the administrator's password, you can use sudo to kill your roommate's process so you can get your work done. Example, sudo kill -9 11234. Depending on your settings, sudo will usually ask for a password the first time you use it and will retain that password in memory for about 15 minutes. After that time you will have to enter the password again the next time you use sudo. You will also need to use sudo when doing the dangerous task of editing system files.

Nano Text Editor

The easiest to use command line text editor for Linux is GNU nano. Nano is a free replacement for the old Unix editor Pico. nano <filename> will open the specified file in nano or you can just type nano and an empty document will be opened. sudo nano <filename> will allow you to edit files as the administrator which is quite handy. Here is a screen shot of me editing this article with nano in Mac OS X.

nano text editor

As you can see nano is fairly simple, but does offer more advanced options such as syntax highlighting. At the bottom is a handy list of commands to be used within nano. The ^ symbol represents the CTRL button on your keyboard. ^O and ^X are the most commonly used commands. ^X will quit nano and ask if you want to save any changes. ^O (this is the letter, not the number) will save your document without quitting. Don't forget that this is the command line. Your mouse will be of little use to you while in nano, so I recommend just forgetting about it. Also, other keyboard shortcuts may not work as they normally do. For example, in nano, CTRL+V does not paste, it pages down.

Now it's time to go out there and start playing with these commands. Try editing some files from the command line or make some directories and move some files into them for practice. The more you use the commands, the more comfortable you will be with the command line. Don't forget to use man to find out all the options and features of each command. Over time, you might be surprised at how quick and efficient the command line can be compared to the typical graphical interfaces.

Joseph Bales is a writer of fiction and humor, as well as a true computer geek and Linux guru. Visit his blog at josephbales.com.