gordon39 said:
Heading should be Command Line Basics
Windows and other OS's users take graphical utilities for everything they would ever need to do as granted. There are many Linux graphical utilities as well. Why then should you learn how to use the command line?
In the rest of this chapter, we will be using the words "command" and "program" as equivalent. In Linux, the commands to do the most basic things like moving and deleting files are actual executables, usually placed in the directory "/bin". In Windows, these functions are built into the command interpreter, so "copy", "del", "dir", etc. are not actual programs.
After successfully logging in to a Linux system, or after starting a virtual console program, the shell presents a prompt to the user. The prompt will often be configured to provide some information about the current context and also indicates that the shell is ready to accept commands.
Example prompts:
Example 1:
root@tty2[tmp]#
In example 1, the prompt contains the following information:
Example 2:
fred@0[documents]$
The prompt can be configured to show many things, but this is not in the scope of this lesson. We will usually shorten the prompt to just the last character, $ or #.
The command who will display a list of the users currently logged on to the system (after typing the command, it is necessary to press the <return> key to indicate that the command is complete and should be processed):
Example 3:
fred@0[documents]$ who
root tty2 Nov 14 19:15
fred :0 Nov 14 13:47
fred@0[documents]$
In this example, the user has entered the command who, and the system has responded with a list showing that two users are logged in. The list also shows which terminal they are using and date and time that they logged on.
The command w, on the other hand, has the same use but displays a bit more information than who. Personally, I prefer this one.
Example 3.1:
[renich@introdesk ~]$ wMost commands 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. 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:
$ ps -A
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 less:
$ less "important notice.txt"
$ less 'important notice.txt'
$ 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.
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.
There are a few basic commands that are typically used very frequently when using a shell. Here is a concise list of what you will be using frequently.
TODO: Add sections about these commands (except clear)
To get a list of all of the files and directories that are in the current directory, execute the command ls. For example:
[user@localhost test]$ ls
directory1 directory2 file1 file2 file3
In order to get more information about these files, you can execute the command ls -l and ls will output information in a 'long' format. For example:
[user@localhost test]$ ls -l
total 28
drwxrwxr-x 2 user group 4096 Dec 4 09:25 directory1
drwxrwxr-x 2 user group 4096 Dec 4 09:25 directory2
-rw-rw-r-- 1 user group 1325 Dec 4 09:24 file1
-rw-rw-r-- 1 user group 7283 Dec 4 09:24 file2
-rw-rw-r-- 1 user group 1931 Dec 4 09:24 file3
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.
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@localhost test]$ pwd
/home/user/test
[user@localhost test]$ cd ..
[user@localhost user]$ pwd
/home/user
The command cd is used to change directories. If you are in the directory /home/user and wanted to change to the directory /home/user/test, the command you would enter would be cd test. For example:
[user@localhost user]$ cd test
[user@localhost test]$
In order to change to a directory anywhere on the system, the full path to that directory can be given. So, if you are in the directory /home/user and you want to change to the directory /etc/httpd, you would run the command cd /etc/httpd. For example:
[user@localhost ~]$ cd /etc/httpd
[user@localhost httpd]$
For more information about cd, run the command man cd and you will be presented with cd's manual page.
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 fo '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.
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).
Some files have very long names and typing them is not funny. 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 erase
The shell knows about cd and will not list files when auto-completing the argument to this command.