Linux For Newbies/Command Line
||A Wikibookian suggests that this book or chapter be merged into Linux Guide/Using the shell.
Please discuss whether or not this merge should happen on the discussion page.
- 1 Why Should I Learn The Command Line, Anyway?
- 2 Shell Prompt
- 3 Some Useful Terminal Commands
- 3.1 Summary of Common Commands
- 3.2 Command Arguments
- 3.3 Using Common Commands
- 3.3.1 Using ls - List
- 3.3.2 Using cd - Change Directory
- 3.3.3 Using pwd - Print Working Directory
- 3.3.4 Using mkdir - Make a Directory
- 3.3.5 Using rmdir - Remove a Directory
- 3.3.6 Using rm - Remove Files
- 3.3.7 Using locate - find a file
- 3.3.8 Using su/sudo - run commands with Super User privileges
- 4 Getting Help on Commands - Man Pages
- 5 Directory and File Completion
Why Should I Learn The Command Line, Anyway?
Simply put, the command line is powerful; an army of tools exist that can take what would be a tedious job in a graphical program and turn it into a task that takes just a few seconds. Removing the last four lines in every row of a large file would be a lengthy process in a graphical application, but can become trivial and automated on the command line.
Flexibility aside, it's also important to note that some Linux systems lack a graphical interface at all and that some systems may become damaged in such a way as to make it impossible to bring up anything other than the command prompt. In these cases it's important to be able to navigate on the command line with enough proficiency to perform whatever tasks need to be done from backing up some files to disabling a dying piece of hardware.
The commands used at the command line may seem a little cryptic due to their tendency to be very short. This is because the roots of the Linux command line are from systems where a single letter entry could take a significant amount of time to travel from a terminal, to a central server and back to the terminal where it was printed onto a roll of paper. In those old systems, the shorter the input was, the better, as it meant less time waiting to issue your command and receive output. The best thing you can do to remember what commands stand for is to find out what word the command is an abbreviation for. This can go a long way to remembering the command later.
For every Linux distribution the command line prompt will look a little different. For example, on one system you might see your username, the '@' symbol, the machine name, your current directory and some kind of symbol to indicate the end of the prompt (here, a dollar sign).
This is a very common prompt. You may also see your username, a space, the fully qualified domain name of the computer, the full path to your present working directory followed by a symbol to indicate the end of the prompt (a dollar sign).
user linux.box.com /home/user#
The prompt varies from system to system based on a number of things. For example, it may be the default configuration set by the creators of your particular Linux distribution. It could also have been configured by the person who administers the computer. Maybe you, yourself, configured how you wanted your prompt to look. The way you configure the look of your command prompt depends on which shell you use.
The shell is the piece that most people commonly refer to as, "the command line" when, in reality, it is simply a piece of software that provides an interface to the services of a kernel. The distinction between a 'shell' and the 'command line' is simply that that a shell refers to a specific piece of software (e.g BASH, Tch, ksh, etc) that provides a command line interface. Most modern Linux systems use BASH (Bourne Again SHell) as the default shell.
Some Useful Terminal Commands
Summary of Common Commands
- ls - This command 'lists' the contents of your present working directory.
- pwd - Shows you what your present working directory is.
- cd - Lets you change directories.
- rm - Removes one or more files.
- rmdir - Remove an empty directory.
- mkdir - Make a directory.
- ps - Provides a list of currently running processes.
- cp - Copy a file.
- mv - Move a file (this is also used to rename a file. "Moving" it from one file name to another.)
- grep - The global regular expression print program lets you search through a file or output of another program.
- find - Find a file on the filesystem (100% accurate, but not fast).
- locate - Find a file on the filesystem from a cached list of files (Fast, but not 100% accurate).
- man - Displays the manual for most commands (including 'man').
- clear - clear the screen
- less - view the contents of a file
- nano - a simple text editor
- sudo - execute a command as another user (default as root)
- su - switch user (default to root)
- view - Start in read-only mode. You will be protected from writing the files. Can also be done with the "-R" argument.
Most ( tar cvf tarFile * ) ( gzip ) ( gzip -d & tar xvf )commands ( cp ) mv [filename} [location] )will need some extra information in order to do something useful. For example, the copy command will need to know what file you want to copy. The extra information is given in the form of arguments. ( while read names __ do __ ### __ done < filename )Arguments are written as extra words after the command name and separated by spaces. If you want to invoke the command ps with the argument -A, you would write:
user@linux$ ps -A
- The words argument, parameter and flag are synonyms, while a "switch" is actually a type of argument that indicates an on or off status. Arguments often need additional information (such as a filename) while a switch will never require additional information except maybe "on/off", "true/false", "yes/no".
Most Linux commands use the GNU fashion of arguments: each option is either a letter preceded by a hyphen, like -o, -W or -L, or a word or phrase preceded by two hyphens, like --symbolic, --no-target-directory etc. Some commands also take file names. Each file has to be supplied as a single argument, so if your filename contains spaces, you have to either enclose it in double or single quotes, or precede the spaces with backslashes. These three commands open the same file with
user@linux$ less "important notice.txt" user@linux$ less 'important notice.txt' user@linux$ less important\ notice.txt
Some characters are recognized by the shell and used for special tasks. If your file name contains the characters: <>&$\"'; and you experience problems, you should put backslashes before these characters. Note that in Linux, filenames can contain any characters except /, which is used as the directory separator, and the null character, which can't be entered from the keyboard anyway.
Using Common Commands
Using ls - List
user@linux ~/$ ls Afilez Report.doc aFilea finances.xls myPic.jpg report1.doc vacation pictures
You may notice from the example, default output of 'ls' that the files are not in strict alphabetical order. This is because the output is sorted on the the numeric ASCII value of each letter. Because upper case letters have a lower ASCII value than lowercase letters, the returned result will always be files starting with upper case letters first. For example, if we add the file AZTRIP to the directory we will get 'AZTRIP' as the file listed first followed by 'Afilez' because the ASCII value of 'Z' is numerically lower than the ASCII value of 'f'.
If the ASCII values of letters has you confused, the important thing to remember here is that uppercase letters trump lowercase letters and will appear first in listings. The result is that files are first sorted by case and sorted alphabetically second.
The default output of ls isn't as useful as it could be. For example, given the output in the example, we don't know what are files and what are directories. We can likely guess that "Afilez", "aFilea" and "vacation pictures" are all directories as they don't have an extension after the file name. To confirm this, we can pass the 'l' parameter to 'ls' to get "long listing format" output.
user@linux ~/$ ls -l -rw-r--r-- 1 adrong mkgroup-l-d 163328 Jul 24 11:42 AZTRIP -rw-r--r-- 1 adrong mkgroup-l-d 11264 Jul 24 11:42 Afilez -rw-r--r-- 1 adrong mkgroup-l-d 22528 Jul 24 11:43 Report.doc -rw-r--r-- 1 adrong mkgroup-l-d 24576 Jul 24 11:42 aFilea -rw-r--r-- 1 adrong mkgroup-l-d 300032 Jul 24 11:43 finances.xls -rw-r--r-- 1 adrong mkgroup-l-d 47104 Jul 24 11:41 myPic.jpg -rw-r--r-- 1 adrong mkgroup-l-d 17920 Jul 24 11:43 report1.doc drwxr-xr-x 1 adrong mkgroup-l-d 0 Jul 24 11:24 vacation pictures
The first character in the output above indicates the file type; where a dash indicates a file and the letter 'd' indicates a directory. From this output we see that the only directory is "vacation pictures"
- Most systems have an alias on the ls command to include the '--color' flag so that the command highlights different file types with different colors. Directories are commonly shown in blue. If the output of your 'ls' command has everything as the same color, try "ls --color".
- Another common parameter to the ls command is 'a' which shows all files including any hidden files and folders that start with a period. Arguments may also combined together in a single block of letters. For example, "ls -l -a" can also be expressed as "ls -la".
The command ls -l outputs information in the following order. The first column of data lists the permissions that apply to that file or directory. The next column of information lists the number of 'hard links' to that file. If you're not sure what that means, then don't worry about it. For more information about hard links, read the manual page for ln by running man ln. The next column lists the name of the user that owns the file or directory, followed by the name of the group that owns the file or directory. The next column lists the size of the file or directory. In the case of directories, it does not list the size of everything within that directory. In the example above, it lists the size of both directories as 4096 bytes. That is because that's how much space having a directory takes up, it does not mean that there are 4096 bytes of stuff within that directory. The next column lists the date that the file or directory was last modified. Finally, the name of the file or directory is listed. To find out more about the command ls, run the command man ls and you will be presented with ls's manual page.
Using cd - Change Directory
The cd command lets you navigate from directory to directory on your system. The first thing you may need to know when using the the cd command is that, unlike windows, Linux does not use the concept of drive letters. Instead, everything is somewhere within the root directory, '/', including other disk drives.
There's a couple ways to use cd to navigate to the directory you want. These are known as "full path" and "relative path".
Full path always begins with with the root directory '/'. For example, if we're in our home directory '/home/user' and we want to navigate to the 'vacation pictures' directory, we would issue the following command.
user@linux ~/$ cd /home/user/vacation\ pictures user@linux vacation pictures/$
You may note that our prompt changed a little to indicate the directory we're currently in. This will be the case on most systems, but as previously discussed, this depends on how your shell is configured. You may also note the backslash between "vacation" and " pictures". This backslash is known as the "escape character" and tells the cd command that the space is part of the directory name. If we omitted the backslash, the cd command would error and tell us that it could not find the directory "vacation". If you don't like typing backslash or have a directory name with many spaces in it, you can use quotation marks around the path.
Relative path can start a number of ways, but always describes how to get to a specific directory from the directory you're currently in. To do this, we need to know how to move to directories that immediately surround the directory we're currently in. These directories are parent directories and sub-directories
A sub-directory is any directory that is contained within the directory we're currently. Given the output below of "ls -l", we see that chicago, florida and "new york" are all sub-directories to the "vacation pictures" directory.
user@linux vacation pictures/$ ls -l drwxr-xr-x 1 adrong mkgroup-l-d 0 Jul 24 12:44 chicago drwxr-xr-x 1 adrong mkgroup-l-d 0 Jul 24 12:44 florida drwxr-xr-x 1 adrong mkgroup-l-d 0 Jul 24 12:44 new york
A parent directory is the directory that contains the folder that we are in. This makes the "vacation pictures" folder the parent of the "chicago", "florida" and "new york" directories.
To cd to a sub-directory, you can start the path name with "./" which indicates the directory we're already in, or you can immediately start with the name of the sub-directory. For example, both of the following commands will change you to the "florida" directory.
user@linux vacation pictures/$ cd ./florida user@linux florida/$
user@linux vacation pictures/$ cd florida user@linux florida/$
To cd to a parent directory, you always specify "../". The following command will return us to the "vacation pictures" directory.
user@linux florida/$ cd ../ user@linux vacation pictures/$
If we are already in the "chicago" directory and want to cd to "new york", we must first tell the system we want to go up one level to the parent directory and then to the "new york" directory. We can do this by saying "cd ../" and then "cd new\ york" or we can do it as one step using the command below.
user@linux chicago/$ cd "../new york" user@linux new york/$
- Notice how we used quotation marks around the path to cd to the "new york" directory. We could have, instead used the backslash before the space and omitted the quotes to get the same result.
Similar to "./" and "../", there's also "~/" which indicates the home directory of the user we're logged in as. If we wanted to cd to the "vacation pictures" folder while in the "/usr/lib" directory, we could do this with any of the following commands.
user@linux lib/$ cd ~/vacation\ pictures user@linux lib/$ cd "/home/user/vacation pictures" user@linux lib/$ cd ../../home/user/vacation\ pictures
- Using the cd command without specifying a directory will return you to your home directory.
echo -n "Please Enter your name" read name useradd $name
Using pwd - Print Working Directory
To display the full path that you are in at any time, simply run the command pwd (print working directory), and it will print the path out for you. For example:
user@linux test$ pwd /home/user/test user@linux test$ cd .. user@linux ~/$ pwd /home/user
Using mkdir - Make a Directory
The mkdir command allows you to create new directories.
user@linux ~/$ ls Afilez Report.doc aFilea finances.xls myPic.jpg report1.doc vacation pictures user@linux ~/$ mkdir playground user@linux ~/$ mkdir ./playground/test1 user@linux ~/$ mkdir /home/user/playground/test2 user@linux ~/$ ls Afilez Report.doc aFilea finances.xls myPic.jpg playground report1.doc vacation pictures user@linux ~/$
- Like we've previously seen with the cd command, we can create new directories by specifying either relative and absolute paths. You'll find you can do this with all command line programs what work with files and folders.
- You can pass the -p parameter to mkdir to make it create all directories to make that path valid. For example, in place of the first and second mkdir commands, we could instead run mkdir -p playground/test1 to create both the playground and test1 directories in a single step.
Using rmdir - Remove a Directory
The rmdir command allows you to delete empty directories.
user@linux ~/$ ls playground/ test1 test2 user@linux ~/$ rmdir playground/test1 user@linux ~/$ ls playground/ test2 user@linux ~/$
Like Windows, Linux has the concept of hidden files. This is important to note because there may be times that you try to delete a seemingly empty directory only to be presented with an error.
user@linux ~/$ cd playground/ user@linux playground/$ rmdir test2 rmdir: failed to remove `test2': Directory not empty user@linux playground/$ ls test2/ user@linux playground/$
To see hidden files and folders, pass the -a (all files and folders) parameter to the ls command.
user@linux playground/$ ls -a test2/ . .. .hiddenfile user@linux playground/$
All hidden files in Linux start with a period. The '.' and '..' files we see listed are the system folders that allow us to specify relative paths.
- If you're tired of seeing the '.' and '..' folders every time you ls -a, you can instead run ls -A for "almost all" files. This gives you all the files and hidden files excluding '.' and '..'
Using rm - Remove Files
The rm command will delete a specific or group of files. Be a little cautious when using this command as there's no 'recycle-bin' for the command line!
To delete the file .hiddenfile in the previous example we simply specify the file after the rm command.
user@linux playground/$ rm test2/.hiddenfile user@linux playground/$ ls -a test2/ . .. user@linux playground/$ rmdir test2 user@linux playground/$
- In addition to having several parameters, the rm command also supports the use of asterisk (*) to represent any set of characters. This can be useful when you want to delete all of specific type of file. For example rm *.jpg will remove all the files ending in '.jpg' residing in your current directory.
- Until you feel comfortable with the rm command (especially when used in conjunction with an asterisk), you might want to specify the -i parameter to have rm confirm the deletion before it happens.
Using locate - find a file
The locate utility is very useful even when working in a graphical environment. On systems where it is installed, it periodically indexes the whole filesystem and stores the names of files in an internal database. This makes finding files by name very fast (a search for a particular name takes under a second). Here is an example:
tweenk@tc1100:~$ locate stock_3dsound /usr/share/icons/gnome/16x16/stock/media/stock_3dsound.png /usr/share/icons/gnome/24x24/stock/media/stock_3dsound.png tweenk@tc1100:~$
This example searches the hard drives for all files which have "stock_3dsound" anywhere in their path. You can also use wildcards * and ? in the pattern.
Since the filenames are kept in a database, locate will not be able to find recently created files, or might find files which have been deleted. While the database is periodically updated by a script, you might want to force an update. To do this, use the command locate -u, run as root (in Ubuntu, sudo locate -u; in other distributions, first su, then locate -u).
Using su/sudo - run commands with Super User privileges
The super user, more commonly known as "root", has complete access to everything on the system. The root user can do silly things like treat input from the mouse as audio to things catastrophic to the system like delete every file on the filesystem. It's largely because of this last reason that the command line has a reputation as being something to be feared.
- If you've been instructed to run the rm command in conjunction with asterisk (*), question it seriously before running it. There are many pranksters trolling Linux support forums hoping to trick you into deleting every file on your filesystem. Other variations have you mv your files to /dev/null which has the same effect. Other variations also involve rm -r -f / as root which deletes every file on your filesystem.
The function of the `su` (super user) command allows you to switch to a different user on the system. Typically it is used to switch to the 'root' user on the system. To become root on most systems, give the su command with no parameters. You will then be prompted for the root password.
user@linux ~/$ su Password: root@linux user/#
Note the changes to the prompt. These are the typical changes you can expect to see. The username changed from 'user' to 'root' to reflect the change in which user we're operating as. The directory changed because the home directory for 'user' (e.g. /home/user) is not the same as the home directory for root (typically /root). The last change is character at the end of the prompt. The pound sign (#) is the typical symbol used to indicate you are now running commands as root.
To switch to a different user, use the su command followed by the username you want to switch to.
user@linux ~/$ su fred Password: firstname.lastname@example.org /home/user/$
Here we've successfully switched to the user 'fred'. Fred is not the root user, so the end symbol remains a dollar sign. However, Fred has made custom changes to how his prompt looks. It provides the fully qualified domain of the machine followed by the full path the user is currently in.
- The password needed in this example is Fred's user password. The root password will not work.
Once you su to the root user, you can su to any other user without needing their password. To su to 'fred' without needing his password, we first su to root as an intermediary step.
user@linux ~/$ su Password: root@linux user/# su fred email@example.com /home/user$
The sudo command is similar to su in that it lets you run commands with super user privileges. However, the philosophy on how super user privileges are give are quite a bit different.
|Run once and all subsequent commands will be ran as root.||Must be ran every time as a prefix to the command you wish to run as root|
|Gives total access to all commands and files on the system||Which commands (and subsequently, files) a user has access to can be limited.|
|One password that must be shared with users you wish to give root access to. Note that the act of sharing a password increases the chance that the password will be intercepted.||Password used for root access is the same as the users' password. Note that the greater the number of people with root access, the more likely one of these passwords can be guessed or phished from a user.|
|User must remember to log out of root when done.||User must wait for the command to time-out before it will prompt for a password again. User can potentially become root as though they ran su and subsequently may still need to remember to log out of root when done.|
To become the user fred using the sudo command looks a bit redundant.
user@linux ~/$ sudo su fred Password: firstname.lastname@example.org /home/user$
- The password needed in this example is your own user password.
- You can still use the command su fred to become the fred user, but again, you'll need Fred's password to do this.
Think of the sudo command as meaning something like, "Become root and run the following command." This makes the above command the sudo equivalent to the su example when we became root as an intermediary step before running su to become the user fred.
There are time when you may need to run more than one command as the root user. This can make using the sudo tedious and redundant, having to retype 'sudo' each time as illustrated in the following example.
user@linux ~/$ sudo ls /home/fred Travel.doc building budget.doc dev user@linux ~/$ sudo ls /home/lisa Work funny.jpg g1tmp.odf user@linux ~/$ sudo ls /home/joe Backup Desktop java.jar user@linux ~/$
We can cut down on the amount of redundant typing by using sudo on bash to become root, first.
user@linux ~/$ sudo bash Password: root@linux user/# sudo ls /home/fred Travel.doc building budget.doc dev root@linux user/# sudo ls /home/lisa Work funny.jpg g1tmp.odf root@linux user/# sudo ls /home/joe Backup Desktop java.jar root@linux user/# exit user@linux ~/$
- sudo and bash weren't made with the expectation they'd be used this way. This is why it's important to exit back to our own user account when we're done.
- To become root, you can also use the command sudo -s.
Getting Help on Commands - Man Pages
Many shell commands will accept various options. To get some information about a command and a list of the available options, use the man (short for 'manual') command, giving the name of the command you want to find more about as its argument. After entering the command man who, the following output is displayed:
WHO(1) User Commands WHO(1) NAME who - show who is logged on SYNOPSIS who [OPTION]... [ FILE | ARG1 ARG2 ] DESCRIPTION -a, --all same as -b -d --login -p -r -t -T -u -b, --boot time of last system boot -d, --dead print dead processes -H, --heading print line of column headings -i, --idle add idle time as HOURS:MINUTES, . or old (deprecated, use -u) -l, --login print system login processes --lookup attempt to canonicalize hostnames via DNS -m only hostname and user associated with stdin Manual page who(1) line 1
Most manuals are several screens long. Use the arrows or Page Up / Page Down keys to navigate in the text. Space shows the next line of text. Pressing q quits the manual and returns to the shell.
We can see from the man page that we can use the -a option to display more information about the currently active users sessions:
fred@3[fred]$ who -a Nov 14 13:47 10 id=si term=0 exit=0 system boot Nov 14 13:47 run-level 5 Nov 14 13:47 last=S Nov 14 13:47 835 id=l5 term=0 exit=0 LOGIN tty1 Nov 14 13:47 958 id=1 root - tty2 Nov 14 19:15 00:19 959 fred - tty3 Nov 14 19:28 00:06 960 LOGIN tty4 Nov 14 13:47 961 id=4 LOGIN tty5 Nov 14 13:47 962 id=5 LOGIN tty6 Nov 14 13:47 963 id=6 fred ? :0 Nov 14 13:47 ? 965 pts/3 Nov 14 16:29 1746 id=/3 term=0 exit=0 fred@3[fred]$
Besides reading help files for programs, man can also be used to get information on shells, functions, and libraries.
TODO: Add sections about these commands (except clear)
View a File
There are many ways to view and edit files on Linux. This section will only cover viewing files (not editing). In order to simply print out the contents of a text file, the program cat is normally used. (The name cat is a an abbreviation for 'concatenate'). For example:
[user@localhost test]$ cat file1 this is the contents of the file named file1
This works well for short files, but if the file is longer than your terminal the beginning of the file will be past the top of your terminal and you won't be able to read it. Luckily, there is a program called less that will help us here. Running the program less with the command less file1 will allow you to scroll up and down through the contents of the file by using the up and down arrow keys. In order to quit less hit the 'Q' key. If you want to use less to search for a word in the file, type the '/' key, enter the word you want to search for and hit the <ENTER> key. You can then hit the 'N' key to jump to each occurrence of the word you searched for. For more information about less or cat, run the commands man less and man cat respectively to read each program's manual page.
Directory and File Completion
Some files have very long names and typing them is not fun. Because of this, the standard Linux shell has an auto-completion feature. Try typing in the first few letters of the name of a file that is in your current directory and then pressing Tab. The name will be automatically completed. If there is more than one file beginning with those letters, the shell will complete the name only partially, and hitting Tab twice will give you a listing of all matching files. If the list is very long, the shell will ask for confirmation. Here is an example:
$ cd /usr/share $ cd gnome <-- Here, I hit Tab twice gnome/ gnome-mount/ gnome-2.0/ gnome-netstatus/ gnome-about/ gnome-nettool/ gnome-app-install/ gnome-panel/ gnome-applets/ gnome-photo-printer/ gnome-art/ gnome-pilot/ gnome-background-properties/ gnome-power-manager/ gnome-btdownload/ gnome-screensaver/ gnome-common/ gnome-screenshot/ gnome-compiz-manager/ gnome-sound-recorder/ gnome-control-center/ gnome-spell-1.0.7/ gnome-dictionary/ gnome-splashscreen-manager/ gnome-doc-utils/ gnome-sudoku/ gnome-games/ gnome-system-log/ gnome-games-common/ gnome-system-tools/ gnome-keyring-manager/ gnome-terminal/ gnome-mag/ gnome-utils/ gnome-media/ gnome-volume-manager/ gnome-menus/ gnome-vpn-properties/ $ cd gnome <-- My partially-typed command is not erased
The shell knows about cd and will not list files when auto-completing the argument to this command.