If you are new to using the command line, this is the place to find out about what it is, what you can do with it, and where best to begin learning about this powerful interface.
The command line/command line interface (CLI) is just one of many names people use to refer to a text-based user interface where you enter commands. Another popular name is the "terminal".
To quickly get going with using the command line, open a terminal by hitting Ctrl+Alt+t.
Now to find out where you are in your file system's directory structure, type this command:
This basically prints (i.e. displays) the current working directory. Below is a screenshot of what things should look like, assuming you're using the default gnome terminal. Some of you may be now thinking: "Hang on, what actually happened here? how did entering "pwd" result in my home directory getting displayed?". That's a good question. What actually happened is this: the text you entered got interpreted by a shell (a piece of software who's job is to run system commands). In this case, the shell checks that there is a program or command called "pwd" and runs it on your behalf. So at a basic level the shell's job is to actually execute programs and commands for you. The shell is another name that people sometimes use to refer to the CLI or a terminal. To learn more about how the BASH shell works, read this post. Yet another command line interface is a virtual console. For now we won't get into too much detail about these. However, if you would like to know a bit more about the differences between terminals, shells, and virtual consoles then read this post.
Throughout this web site, we'll use the terms "command line" and terminal interchangeably, sometimes we'll throw shell in to the fray also! For example, if we say "open a shell", we just mean get to the command line... it doesn't matter whether it's a terminal or virtual console! At a guess, most of you will enter commands on the command line of gnome terminal running the BASH shell, simply because they are the defaults in a lot of linux desktops including Ubuntu. OK, with that out of the way, lets get started ...
The command line can be used for and as a means to do many things. These include but are not limited to these sorts of tasks:
- Manipulating files and directories
- Manipulating disks
- Controling user access and permissions
- Editing files
- Configuration of the operating system (e.g. optimisations and performance tuning via the /proc file system)
- Configuration of software (e.g. by editing ".conf" configuration files)
- Installation/updating/removal of software (e.g. via the apt-get command)
- Executing commands and utilities
- Scripting/programming (e.g. via bash scripting or another interpreter such as python/perl/lua/ruby, or even building binaries for compiled languages such as C and C++ via compilers such as gcc)
- Desktop/Server security hardening e.g. Firewall configuration and periodic file integrity checking
- Various types of system resource monitoring e.g. network, disk io, cpu, and memory usage monitoring.
There's not much that cannot be done from the command line.
Beginners will want to know how they can find out more about a particular command or utility. The way to do this is to use the man or info utility to view the "manpage" or info page. Sometimes both of these informational resources are available for a given piece of software, although the coverage and the level of detail can vary significantly (Also, sometimes they point to the same piece of information!). To use these, simply precede the command in question by either man or info and hit return (press q to exit). For example, to find out more about the "ls" command, you can use:
To quit from either, just hit the q key on your keyboard. Sometimes these can contain quite a lot of information. For example check out bash:
(Note that for BASH both "man" and "info" point to the same underlying information, however in a lot of cases the content is usually different so its worth having a quick read of both).
If you have some sort of idea what you are looking for, you can use the apropos command to search through the man page names and their descriptions: Say you wanted to find out how to remove files, you could try searching with the string "remove":
apropos -s 1 "remove"
which should point you toward a list of commands which include the "rm" and "rmdir" commands which we'll explain more about shortly.
For simple viewing of text files when no editing is required, use the more command or the less command. To use the "more" command simply run it followed by the name of the file who's contents you wish to view:
More is quite primitive, only allowing you to display text as you move down through the file. To move down one line at a time, hit space. Alternatively, hit Enter to move down one page at a time. To exit from more, hit Ctrl+c or just q.
To use the "less" command, run it like so:
The less command is much more suited to most peoples viewing needs because it allows you to scroll anywhere using the arrow keys or the pageup/pagedown keys. To perform a simple forward search (down the page), hit /, then type the text you want to search for, then hit Enter. To perform a simple reverse search (up the page), hit ?, then type the text as before, and hit Enter. For either search direction, hitting n will go to the next search match. To exit from less, just hit the q key.
To add an empty file, you can use the touch command. For example to create a file called "newfile", use:
Otherwise to create a file, just open a new file in whatever software you are using, then save that. Taking nano as an example, you could open a file called someNewFile.txt using:
Then type something if you like, then hit Ctrl+O to save the file. You'll be prompted about what name to use (in case you've changed your mind about the filename). Alternatively, you could hit Ctrl+X to exit, which would promt you about saving the file before exit.
To create a new directory you would use the mkdir command, for example:
To remove files and directories, you can use the rm command. For a file, use:
For a directory, use:
rm -r dirname
If you would like to be prompted before files (and/or dirs) get removed, you should use the "-i" option. An example of this when removing a directory is:
rm -ir dirname
Another way to remove directories (if they are empty) is with the rmdir command.
In order to carry out your work at the command line, a user will need to know how to navigate the system using a shell. A good starting point is getting to grips with the pwd, ls, and cd commands.
As mentioned already, to find out where you're currently at in the directory structure of the file system, use the pwd command (which basically prints the working directory). e.g.
To change from the current working directory to another, use the cd command. For example to change to our Downloads directory, we'd use:
To find out what files are present in a directory, use the ls command. e.g To list the directory contents of our home directory, we'd use:
To operate commands in relation to your home directory, you can use the "~" key as a shortcut. So instead of typing /home/username, just use ~ instead. For example to cd to your home directory, just enter:
And to list the files in your home directory, enter:
The Ubuntu Software Centre GUI can be used to install and manage your software. Alternatively, or along side this, you can use the apt-get command line utility to manage the software installed on your Ubuntu system. There are also other options like synaptic and aptitude. They all use the same underlying dpkg backend.
In the following sections, we'll give you a flavour of what's available for command line usage (but bear in mind we're barely touching the surface of what software you can install on your Ubuntu system!).
If you want to get productive in the terminal regardless of whether your using a server or desktop installation, you'll need to choose a non gui based editor and become familiar with it (ie. one that doesn't involve a graphical user interface). Some of the options available for you to choose are vi, vim and nano. Note that emacs lovers can use a non gui version e.g. running emacs -nw ... however, you have more chance of finding vi or vim pre-installed on a box You'll be able to use this non gui based editor on servers and desktops where there's no gui editor available, so it's a handy skill to learn because you'll almost always be able to use it for your editing needs.
Tools for monitoring the resource usage of your desktop, laptop or server can prove to be very useful for troubleshooting and optimisation related purposes. You can carry out various types of resource monitoring via the terminal including network/disk/cpu/memory usage monitoring. Example utilities include: Network Monitoring: netstat, nethogs, iptraf, iftop Disk I/O: iotop. CPU/Memory: top, ps, htop, atop.
There are lots of network related programs available for use via the command line. For example you can use these to connect different devices: netcat, cryptcat, socat, ssh and sftp. You can check on the status of your network cards using ifconfig or the more complete ip utility. You can also "shape" network traffic and interact with the routing and neighbor (arp) tables using the ip utility.
You should always use a firewall to help keep unauthorised traffic away from your system. The iptables program and its more beginner friendly wrapper called ufw (Ubuntu Firewall) are available for configuring your firewall.
You can schedule commands and utilities to run at specific predetermined times using utilities like cron. This can be really handy for running backup scripts and other jobs that need to run on a regular basis which are best automated.
You can list the hardware configuration of your system using lshw. It's also possible to get a dump of the BIOS information from your box using biosdecode or the more detailed dmidecode. To display info about USB buses and devices, you can use lsusb. You can also get some information about PCI buses and the devices connected to them by using lspci.
There are many tools available for zipping/compressing files to save space. Options include gzip, zip, and bzip2. Their decompression counterparts are gunzip, unzip, and bunzip2 respectively. The tar utility can be used to archive serveral compressed files into one "package" (and it can also carry out the compression/decompression stages).
There are also encryption related tools such as openssl and gpg available via the terminal. These will allow you to keep sensitive data private via encryption and to verify that data came from a specified source via digital signatures.
For more complete control of your system, a good understanding of using the CLI/terminal (and thus the bash shell) is a manditory skill to have. It's a skill set that may take time to develop but is well worth it. We suggest you get familiar with the grep and find commands, along with a few bash shortcuts.