Jump To Content

LearnHub




CLI Basics

Original source: http://learnhub.com/redirect?u=http%3A%2F%2Fen.wikibooks.org%2Fwiki%2FLinux_For_Newbies%2FCommand_Line

Why Should I Learn The Scary Command Line Anyway?

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?

  • Command-line tools are easier to test and debug. Therefore, they are usually of higher quality and have less bugs than their graphical counterparts.
  • Commands are more or less the same on every Linux system, while the GUIs are very variable. Therefore command-line instructions are more universal, so many fixes for common problems will require "command-line hacking".
  • Command-line tools facilitate automation, because they can be easily called from simple shell scripts. GUI tools are very hard to automate.
  • Some tasks that can be done via the command line don't translate well into any graphical utility.
  • There are still things that can only be done using command-line programs, because no one has written the graphical front ends yet.
  • Well-documented command-line tools are actually easier to use than graphical tools.
  • The output of one command line tool can be used as the input to another. By combining command lines in this way, the savvy Linux user can accomplish some impressive tasks.

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.

Shell Prompt

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:

  • The current user is root
  • The user is using tty terminal 2
  • The current directory is called tmp
  • The user has super-user privileges (shown by the # character)

Example 2:

fred@0[documents]$
  • The current user is fred
  • He is using virtual terminal 0
  • The current directory is documents
  • The user has normal user privileges (shown by the $ character)

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 #.

An Example Commands: who & w

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 ~]$ w
12:11:03 up 24 min, 2 users, load average: 0.27, 0.87, 1.10
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty7 :0 11:47 0.00s 60.68s 0.05s gnome-session
renich pts/1 :0.0 12:10 0.00s 0.10s 0.01s w
[renich@introdesk ~]$

Command Arguments

Most 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.

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.

Basic Commands

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.

  • ls - list files
  • cd - change directory
  • pwd - show present working directory
  • clear - clear the screen
  • cp - copy a file or a directory
  • mv - move and rename files
  • less - view the contents of a file
  • rm - remove (delete) a file
  • mkdir - make (create) a directory
  • nano - a simple text editor
  • locate - find a file
  • sudo or su - execute a command as super-user

TODO: Add sections about these commands (except clear)

ls - List Files

Also see the entry for ls in the Guide to UNIX/Commands wikibook.

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.

pwd - Print Working Directory

Also see the entry for pwd in the Guide to UNIX/Commands wikibook.

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

cd - Change Directory

Also see the entry for cd in the Guide to UNIX/Commands wikibook.

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.

View a File

Also see the chapter on file viewing in the Guide to UNIX/Commands wikibook.

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.

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).

Directory and File Completion

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.


  1. gordon39 saidWed, 20 Aug 2008 10:40:13 -0000 ( Link )

    Heading should be Command Line Basics

    Actions
    Vote
    Current Rating
    0
    Rate Up
    Rate Down
    No Votes

    Post Comments

  2. yenomeerf saidWed, 08 Oct 2008 18:26:27 -0000 ( Link )

    It is the Heading — CLI

    Actions
    Vote
    Current Rating
    0
    Rate Up
    Rate Down
    No Votes

    Post Comments

Your Comment
Textile is Enabled (View Reference)