Why use the command-line?
"Under
Linux there are GUIs (graphical user interfaces), where you can point
and click and drag, and hopefully get work done without first
reading lots of documentation. The traditional Unix environment is a
CLI (command line interface), where you type commands to tell the
computer what to do. That is faster and more powerful, but requires
finding out what the commands are."
-- from man intro(1)
There are many varieties of Linux, but almost all of them use similar commands that can be entered from a command-line interface terminal.
There are also many graphical user interfaces (GUIs), but each of them works somewhat differently. Experienced users therefore find it easier to learn commands that can be used in all Linux distributions. Typed commands harness the full expressive power of a user directing a computer, whereas a GUI is limited by the number of elements that can fit on a screen.
For the novice, commands-line interface commands can appear daunting:
sudo awk blah_blah -w -t -f aWkward/ComBination/Of/mixedCase/underscores_strokes/and.dots
However, it is important to note that even experienced users often cut and paste commands (from a guide or manual) into the command-line terminal; they do not memorize all the details. Perhaps more important to the neophyte seeking help is that powerful commands can easily be sent over email or found in online forums, whereas the sequence of GUI actions to accomplish the same task may be nearly impossible to convey.
It is important, of course, to know how to use the command-line terminal - and anyone who can manage typing, backspacing, and cutting and pasting can manage the command-line terminal (it is not more difficult than that).
This page will outline a few crafty shortcuts which can make using a command-line interface easier.
Using this page
All command names will be in bold.
Commands needing to be typed will be in "bold with quotes".
- All of the commands on this page are to be issued from a command prompt in a terminal.
Note that command and file names are case sensitive. User, user, and USER are all different to Linux.
Starting a Terminal
You should be able to see a Terminal icon on the desktop panel or the desktop itself - it will usually look like a blank screen monitor. Otherwise, you can find it somewhere in the main menus, but the menu organization varies by the GUI desktop; perhaps at one of:
In Gnome (Ubuntu)
Applications menu -> Accessories -> Terminal.
In Xfce (Xubuntu)
Applications menu -> System -> Terminal.
In KDE (Kubuntu)
KMenu -> System -> Terminal Program (Konsole).
In LXDE (Lubuntu)
Menu -> Accessories -> LXTerminal.
Commands
File & Directory Commands
The tilde (~) symbol stands for your home directory. If you are user, then the tilde (~) stands for /home/user
pwd: The pwd command will allow you to know in which directory you're located (pwd stands for "print working directory"). Example: "pwd" in the Desktop directory will show "~/Desktop". Note that the Gnome Terminal also displays this information in the title bar of its window. A useful gnemonic is "present working directory."
ls: The ls command will show you ('list') the files in your current directory. Used with certain options, you can see sizes of files, when files were made, and permissions of files. Example: "ls ~" will show you the files that are in your home directory.
cd: The cd command will allow you to change directories. When you open a terminal you will be in your home directory. To move around the file system you will use cd. Examples:
To navigate into the root directory, use "cd /"
To navigate to your home directory, use "cd" or "cd ~"
To navigate up one directory level, use "cd .."
To navigate to the previous directory (or back), use "cd -"
To navigate through multiple levels of directory at once, specify the full directory path that you want to go to. For example, use, "cd /var/www" to go directly to the /www subdirectory of /var/. As another example, "cd ~/Desktop" will move you to the Desktop subdirectory inside your home directory.
cp: The cp command will make a copy of a file for you. Example: "cp file foo" will make an exact copy of "file" and name it "foo", but the file "file" will still be there. If you are copying a directory and its contents, you must use "cp -r directory foo" (copy recursively). (To understand what "recursively" means, think of it this way: to copy the directory and all its files and subdirectories and all their files and subdirectories of the subdirectories and all their files, and on and on, "recursively")
mv: The mv command will "move" a file to a different location or will rename a file. Examples are as follows: "mv file foo" will rename the file "file" to "foo". "mv foo ~/Desktop" will move the file "foo" to your Desktop directory but will not rename it. You must specify a new file name to rename a file.
- To save on typing, you can substitute '~' in place of the home directory.
rm: Use this command to delete or "remove" files.
rmdir: The rmdir command will delete an empty directory. To delete a directory and all of its contents recursively, use rm -r instead.
mkdir: Create or make directories. Example: "mkdir music" will create a directory called "music" inside your current directory (see cd and pwd above).
ssh: Connecting to Other Computers
ssh: The ssh command opens a secure shell to another computer - an encrypted connection to a command-line interface. For example, after typing "ssh somebody@another.computer" and entering the password for user somebody, you will continue to operate logged on to another.computer.
ssh -Y: Use the -Y option to permit the other computer to open windows on your display.
scp: Securely copy files between computers: for example,
"scp -r ~/Documents/thesis you@your.computer:Documents/thesis"
recursively copies the contents of a thesis dirctory. Note the ":" character!
Editing Files
Editing plain text files is one of the most common jobs to perform, but there is no one standard text editor -- it is a highly personal choice. You will not find a command "edit", and the editor ed is a primitive thing best avoided. Try the editors gedit, kate, mousepad, nedit, emacs, vim, or others.
sudo: Executing Commands with Elevated Privileges
On Linux most accounts are regular users without permission or priveledge to administer the system or alter files in the system directories or in the directories of other users. Therefore, some commands will need to be prefaced with the sudo command which elevates privileges to the root-user administrative level temporarily. When using sudo you will be prompted for your own password (not root user's). Only users with sudo (administrative) privileges will be able to use this command. (Please see RootSudo for more information on using sudo.)
System Information Commands
df: The df command displays filesystem disk space usage for all mounted partitions. "df -h" is probably the most useful - it uses megabytes (M) and gigabytes (G) instead of blocks to report. (-h means "human-readable")
du: The du command displays the disk usage for a directory. It can either display the space used for all subdirectories or the total for the directory you run it on. The option -s means "Summary" and -h means "Human Readable".
top: The top command displays information on your Linux system, running processes and system resources, including CPU, RAM & swap usage and total number of tasks being run. To exit top, press "q".
Adding A New User
"adduser newuser" command will create a new general user called "newuser" on your system, and to assign a password for the newuser account use "passwd newuser".
Options
The default behaviour for a command may usually be modified by adding a --option to the command. The ls command for example has an -s option so that "ls -s" will include file sizes in the listing. There is also a -h option to get those sizes in a "human readable" format.
Options can be grouped in clusters so "ls -sh" is exactly the same command as "ls -s -h". Most options have a long version, prefixed with two dashes instead of one, so even "ls --size --human-readable" is the same command.
"Man" and getting help
Nearly every command and application in Linux will have a man (manual) file, so finding them is as simple as typing "man "command"" to bring up a longer manual entry for the specified command. For example, "man mv" will bring up the mv (Move) manual.
Move up and down the man file with the arrow keys, and quit back to the command prompt with "q".
"man man" will bring up the manual entry for the man command, which is a good place to start!
"man intro" is especially useful - it displays the "Introduction to user commands" which is a well-written, fairly brief introduction to the Linux command line.
There are also info pages, which are generally more in-depth than man pages. Try "info info" for the introduction to info pages.
Some software developers prefer info to man (for instance, GNU developers), so if you find a very widely used command or app that doesn't have a man page, it's worth checking for an info page.
Virtually all commands understand the -h (or --help) option which will produce a short usage description of the command and it's options, then exit back to the command prompt. Try "man -h" or "man --help" to see this in action.
Caveat: It's possible (but rare) that a program doesn't understand the -h option to mean help. For this reason, check for a man or info page first, and try the long option --help before -h.
Searching for man files
If you aren't sure which command or application you need to use, you can try searching the man files.
man -k foo will search the man files for foo. Try "man -k nautilus" to see how this works.
Note that this is the same as doing apropos command.
man -f foo searches only the titles of your system's man files. Try "man -f gnome", for example.
Note that this is the same as doing whatis command.
Prettier Manual Pages
Users will be pleased to find they can read and search man pages in a web browser.
Other Useful Things
Pasting in commands
Often, you will be referred to instructions that require commands to be pasted into the terminal. You might be wondering why the text you've copied from a web page using ctrl+C won't paste in with ctrl+V. Surely you don't have to type in all those nasty commands and filenames? Relax. ctrl+shift+V pastes into a Gnome terminal; you can also do Middle Button Click on your mouse (both buttons simultaneously on a two-button mouse) or Right Click and select Paste from the menu. However, if you want to avoid the mouse and yet paste it, use "Shift+Insert", to paste the command. If you have to copy it from another terminal / webpage, you can use "Ctrl+Insert" to copy.
Save on typing
Up Arrow or ctrl+p |
Scrolls through the commands you've entered previously. |
|
Down Arrow or ctrl+n |
Takes you back to a more recent command. |
|
Enter |
When you have the command you want. |
|
tab |
A very useful feature. It autocompletes any commands or filenames, if there's only one option, or else gives you a list of options. |
|
ctrl+r |
Searches for commands you've already typed. When you have entered a very long, complex command and need to repeat it, using this key combination and then typing a portion of the command will search through your command history. When you find it, simply press Enter. |
|
History |
The history command shows a very long list of commands that you have typed. Each command is displayed next to a number. You can type !x to execute a previously typed command from the list (replace the X with a number). If you history output is too long, then use history | less for a scrollable list. |
|
Change the text
Use the Left/Right arrow keys to move around the line - the mouse won't change the typing-cursor position.
When the cursor is where you want it in the line, typing inserts text - it doesn't overtype what's already there.
ctrl+a or Home |
Moves the cursor to the start of a line. |
|
ctrl+e or End |
Moves the cursor to the end of a line. |
|
ctrl+b |
Moves to the beginning of the previous or current word. |
|
ctrl+k |
Deletes from the current cursor position to the end of the line. |
|
ctrl+u |
Deletes the whole of the current line. |
|
ctrl+w |
Deletes the word before the cursor. |
|
More Information
AptGetHowto - using apt-get to install packages from the command line.
Commandline Repository Editing - adding the Universe/Multiverse repositories through the command line.
grep Howto - grep is a powerful command line search tool.
find Howto - locate files on the command line.
CommandlineHowto - longer and more complete than this basic guide, but still unfinished.
HowToReadline - information on some more advanced customization for the command line.
For more detailed tutorials on the Linux command line, please see:
http://mywiki.wooledge.org/BashGuide - Bash guide suitable for beginners. One of the few to also teach good practice.
http://linuxcommand.org/ - basic BASH tutorials, including BASH scripting
http://linuxsurvival.com/index.php - Java-based tutorials
http://rute.2038bug.com/index.html.gz - a massive online book about system administration, almost all from the command line.
http://www.ss64.com/bash/ - a good list a commands.
http://tinyurl.com/ycyg4mk - Listing of 3 helpful sites from beginner to advanced scripting
Ubuntuguide -- concise and up-to-date information for both command-line and GUI users of Ubuntu
Kubuntuguide -- concise and up-to-date information for both command-line and GUI users of Kubuntu.
This page is closely based on one from the Ubuntu Community Wiki.