Welcome to the command line
Moving around the FileSystem
Commands for moving around the file system include the following:
- pwd: The pwd command will allow you to know the directory in which you're located (pwd stands for "print working 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. For example, cd ~/Desktop will move you to your desktop directory.
- To navigate into the root directory, use cd /
- To navigate to your home directory, use 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, use cd / var/www for example, which will take you directly to the /www subdirectory of /var/.
Manipulating Files and Folders
You can manipulate files and folders using the following commands:
- cp: The cp command will make a copy of a file for you. For example, cp file foo will make an exact copy of file and name it "foo," but the file "file" will still be there. When you use mv, that file would no longer exist, but when you use cp the file stays and a new copy is made.
- 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.
Note: If you are using mv with sudo you will not be able to use the ~ shortcut. Instead, you will have to use the full pathnames to your files.
- rm: Use this command to remove or delete a file in your directory. It will not work on directories in which there are files.
- ls: The ls command will show you the files in your current directory. Used with certain options, you can see file sizes, when files were created, and file permissions. For example, ls ~ will show you the files that are in your home directory.
- mkdir: The mkdir command will allow you to create directories. For example, mkdir music will create a music directory.
- chmod: The chmod command will change the permissions on the files listed.
Permissions are based on a fairly simple model. You can set permissions for user, group, and world, and you can set whether each can read, write, and execute the file. For an example, if a file had permission to allow everybody to read but only the user could write, the permissions would read rwxr-r-. To add or remove a permission, you append a + or a - in front of the specific permission. For example, to add the capability for the group to edit in the previous example, you could type chmod g+x file.
- chown: The chown command allows the user to change the user and group ownerships of a file. For example, chown jim file would change the ownership of the file to"Jim."
System Information Commands
System information commands include the following:
- df: The df command displays filesystem disk space usage for all partitions. The command df-h is probably the most useful. It uses megabytes (M) and gigabytes (G) instead of blocks to report. (-h means "human-readable.")
- free: The free command displays the amount of free and used memory in the system. For example, free -m will give the information using megabytes, which is probably most useful for current computers.
- top: The top command displays information on your Linux system, running processes, and system resources, including the CPU, RAM, swap usage, and total number of tasks being run. To exit top, press Q.
- uname -a: The uname command with the -a option prints all system information, including machine name, kernel name, version, and a few other details. Most useful for checking which kernel you're using.
- lsb_release -a: The lsb_release command with the -a option prints version information for the Linux release you're running. For example:
- user@computer:~$ lsb_release -a
- LSB Version: n/a
- Distributor ID: Ubuntu
- Description: Ubuntu (The Breezy Badger Release)
- Release:
- Codename: breezy
- ifconfig: This reports on your system's network interfaces.
- iwconfig: The command iwconfig will show you any wireless network adapters and the wireless specific information from them, such as speed and network connected.
- ps: The command ps allows you to view all the processes running on the machine.
The following commands list the hardware on your computer, either a specific type or with a specific method. They are most useful for debugging when a piece of hardware does not function correctly.
- lspci: The command lspci lists all PCI buses and devices connected to them. This commonly includes network cards and sound cards.
- lsusb: The command lsusb lists all USB buses and any connected USB devices, such as printers and thumb drives.
- lshal: The command lshal lists all devices the hardware abstraction layer (HAL) knows about, which should be most hardware on your system.
- lshw: The command lshw lists hardware on your system, including maker, type, and where it is connected.
Searching and Editing Text Files
Search and edit text files using the following commands:
- grep: The command grep allows you to search inside a number of files for a particular search pattern and then print matching lines. For example, grep blah file will search for the text "blah" in the file and then print any matching lines.
- sed: The sed (or Stream EDitor) command allows search and replace of a particular string in a file. For example, if you wanted to find the string "cat" and replace it with "dog" in a file named pets, you would type sed s/cat/dog/g.
- cat: The cat command, short for concatenate, is useful for viewing and adding to text files. The simple cat FILENAME will display the contents of the file. Using cat filename file adds the contents of the first file to the second.
- nano: Nano is a simple text editor for the command line. To open a file, use nano filename. Commands listed at the bottom of the screen are accessed via ctrl+letter name.
- less: The less command is used for viewing text files as well as standard output. A common usage is to pipe another command through less to be able to see all the output, such as ls | less.
Dealing with Users and Groups
You can use the following commands to administer users and groups:
- adduser: The adduser command will create a new user. To simply create a new user, type sudo adduser $loginname. This will create the user's home directory and default group. It will prompt for a user password and then further details about the user.
- passwd: The passwd command will change the user's password. If simply run by a regular user, it will change his password. If run using sudo, it can change any user's password. For example, sudo passwd joe will change Joe's password.
- who: The who command will tell you who is currently logged into the machine.
- addgroup: The addgroup command will add a new group. To create a new group, type sudo addgroup $groupname.
- deluser: The deluser command will remove a user from the system. To remove their files and home directory, you need to add the -remove-home option
- delgroup: The delgroup command will remove a group from the system. You cannot remove a group that is the primary group of any users.
Getting Help on the Command Line
This section will provide you with some tips on getting help on the command line. The commands -help and man are the two most important tools at the command line.
Virtually all commands understand the -h (or -help) option which will produce a short usage description of the command and its options, then exit back to the command prompt. Try man -h or man -help to see this in action.
Every command and nearly every 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.
Some helpful tips for using the man command include:
- arrow keys: Move up and down the man file using the arrow keys.
- q: Quit back to the command prompt by typing q.
- man man: man man will bring up the manual entry for the man command, which is a good place to start!
- man intro: 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.
Using Wildcards
Sometimes you need to look at or use multiple files at the same time. For instance, you might want to delete all .rar files or move all .odt files to another directory. Thankfully, there are series of wildcards you can use to acomplish this.
- * will match any number of characters. For example, *.rar will match any file with the ending of .rar
- * ? will match any single character. For example, ?.rar will match a.rar but not ab.rar
- * [characters] will match any of the character within the brackets. For example, [ab].rar will match a.rar and b.rar but not c.rar
- * [!characters] will match any characters that are not listed. For example, [!ab].rar will match c.rar but not a.rar or b.rar.
Executing Multiple Commands
Often you may want to execute several commands together, either one after another or by passing output from one to another.
Run Sequentially
If you need to execute multiple commands in sequence, but don't need to pass output between them, you can run them using ; between each command. Each command will be executed, and the following command will be run. If you want to make the running of the second command conditional on the successful completion of the first command, separate the commands with &&.
If you need to execute multiple commands in sequence, but don't need to pass output between them, there are two options based on whether or not you want the subsequent commands to run only if the previous commands succeed or not. If you want the commands to run one after the other regardless of whether or not preceding commands succeed, place a ; between the commands. For example, if you want to get information about your hardware, you could run lspci ; lsusb, which would output information on your PCI buses and USB devices in sequence.
However, if you need to conditionally run the commands based on whether the previous command has succeeded, insert && between commands. An example of this is building a program from source, which is traditionally done with ./configure, make, and make install. The commands make and make install require that the previous commands have completed successfully, so you would use ./configure && make && make install.
Passing Output
If you need to pass the output of one command so that it goes to the input of the next, you need something called piping after the character used between the commands, | , which looks like a vertical bar or pipe.
To use the pipe, insert the | between each command. For example, using the | in the command ls | less allows you to view the contents of the ls more easily.