Originally Published: Tuesday, 8 August 2000 Author: Tom Dominico, Jr.
Published to: learn_articles_firststep/General Page: 1/1 - [Printable]

Shell Skills, Part 3: Essential Commands

In Part 1 of the Shell Skills series, we looked at some of the helpful features of the Bash shell. In Part 2, we learned how to use input, output, and redirection to our advantage. By now, you should have a good idea of how to effectively work with files and directories at the command line. However, that command line won't do you much good unless you know some essential commands that can help to make your life easier.

   Page 1 of 1  

In Part 1 of the Shell Skills series, we looked at some of the helpful features of the Bash shell. In Part 2 we learned how to use input, output, and redirection to our advantage. By now, you should have a good idea of how to effectively work with files and directories at the command line. However, that command line won't do you much good unless you know some essential commands that can help to make your life easier.

First, let's discuss the required terminology. Commands are used in conjunction with "options" and "arguments". Sometimes the same command can be used in different ways with the use of options. Options tell the command exactly what you need it to do. They generally consist of a hyphen followed by a letter, or two hyphens followed by a word. When using multiple options, you can generally use only one hyphen followed by all the options you wish to use, with no spaces. Arguments, on the other hand, are information that you sometimes need to give, or "pass", to a command. So, for example, "tar -xvf foo.tar" means "use the tar command to untar (x) the filename (f) that I have specified as an argument (foo.tar), and give me the verbose output (v)". Simple, right? To find the available options for a command and available arguments, try "<commandname> --help" or "man <commandname>". Please note that in the examples below, I may not include all possible options for a command, just the most common ones. Sounds like a motivation for doing a little more research on your own, doesn't it?

Working With Files

A Linux system has a plethora of commands available to to assist you in working with files. Since most of the system configuration you'll do involves working with files, these commands can help to make life easier for you.

  • cat <filename>

    "cat" can be used as a quick way to display the contents of a file. It stands for "concatenate", and has a variety of other uses. If used with no arguments, it accepts standard input, so you can also use it as a quick and dirty editor, using Ctrl-D when you are finished typing.

    Examples: tom@murdock:~$ cat > somefile.txt Wow! cat can be used for all sorts of things. ^D tom@murdock:~$ less somefile.txt Wow! cat can be used for all sorts of things. somefile.txt (END)

  • less <filename>

    "less" is what is known as a "pager". When using a terminal, sometimes you may wish to view a file that is longer than one screen. "less" allows you to view one page at a time, move back and forth, and even search for specific text. To move ahead one page, use the space bar. To move back, use the "b" key. To search for something, type "/" followed by the string you are searching for. If a command produces too much output, you can also redirect that to less. For example, "tar --help | less".

  • cp [options] <source-file-or-list> <destination-filename-or-directory>

    "cp" copies the source file to the destination.

    Options: -b: Backup any files that would be overwritten -i: Prompt before overwriting a file -p: Preserve file attributes -r: Copy recursively (include contents of subdirectories)

  • gzip [options] <file-list>

    "gzip" compresses and decompresses files. The extension ".gz" often indicates a gzipped file. It is often combined with the "tar" command to produce a file with the extension ".tar.gz", and is known as a "tarball".

    Options: -d: Decompress (same as using the gunzip command) -r: Compresses/decompresses files recursively (includes subdirectories)

  • head [options] <file-list>

    "head" allows you to look at the beginning of the file. This is useful if you have a long file, but you only need information from the very beginning.

    Option: -n: Where n is the number of lines that you wish to display. The default is ten.

  • mv [options] <existing-file> <new-file> mv [options] <existing-file-list> <directory> mv [options] <existing-directory> <new-directory>

    "mv" moves a file to a new location. It can also be used to rename a file.

    Options: -b: Backup any file that might be overwritten -i: Prompt if about to overwrite a file

  • rm [options] <file-list>

    "rm" removes a file or list of files.

    Options: -f: Force the removal of files -i: Asks before removing each file -r: Recursive (deletes the contents of a directory, including its subdirectories, and then removes the directory itself). WARNING: THIS IS DANGEROUS! Be careful with this, especially when combined with the "-f" option.

  • tail [options] <file-list>

    "tail" allows you to look at the end of the file. This is useful if you have a long file, but you only need information from the very end, such as a log file.

    Option: -n: Where n is the number of lines that you wish to display. The default is ten.

  • tar option [modifiers] <file-list>

    "tar" can create and extract archive files. Archiving is a way of combining many files into one large file, and is often used for backups and software distribution. These archives are often compressed to save transfer time.

    Options (use only one): -r: Append the files to the end of the archive -c: Create a new archive, destroying an old archive of the same name, if it exists -x: Extract the contents of an archive -t: List the contents of an archive (does not require a file-list) -u: Update the archive (only add the files if they do not exist, or have been modified)

    Modifiers: -z: Use gzip to perform compression (if creating an archive) or decompression (if extracting from an archive) -f: Filename to decompress, or filename which will hold the archive -v: Be verbose (list each file as it is read/written to/from the archive)

    Examples: In these examples, we'll create a tarball, then move it to a test directory and untar it to make sure it worked.

    tom@murdock:~/articles$ ls article1.txt article2.txt

    tom@murdock:~/articles$ tar -czvf articles.tar.gz article1.txt article2.txt tom@murdock:~/articles$ ls article1.txt article2.txt articles.tar.gz

    tom@murdock:~/articles$ mkdir untar-test tom@murdock:~/articles$ mv articles.tar.gz untar-test ; cd untar-test

    tom@murdock:~/articles/untar-test$ tar -zxvf articles.tar.gz tom@murdock:~/articles/untar-test$ ls article1.txt article2.txt articles.tar.gz

System Administration
  • df

    "df" will give you details about disk usage for your various mounted drives.

  • du [options] <path-list>

    "du" reports how much space is used by a directory.

    Options: -s: Print summarized totals -h: Print values in an easier-to-read format

Working With Directories
  • cd <directory>

    "cd" changes the current working directory. You can specify absolute or relative paths. "cd" without an argument will take you to your home directory. Also, two periods ("..") stand for the directory above your current directory. "~/" is a shortcut for specifying your home directory.

    Examples: tom@murdock:~$ pwd /home/tom tom@murdock:~$ ls articles docs files

    tom@murdock:~$ cd articles tom@murdock:~$ pwd /home/tom/articles

    tom@murdock:~$ cd .. tom@murdock:~$ pwd /home/tom

    tom@murdock:~$ cd ~/docs tom@murdock:~$ pwd /home/tom/docs

    tom@murdock:~$ cd tom@murdock:~$ pwd /home/tom

  • rmdir <directory-list>

    "rmdir" removes an empty directory.

  • mkdir [option] <directory-list>

    "mkdir" allows you to create a directory.

    Option: -p: Stands for "parents". Create any directories that do not exist in the path of the directory you wish to create.

Again, this is by no means a definitive list of commands, but it should help to get you started. Remember, man pages and the "--help" option are your friends! You can learn quite a bit just by reading their output, and experimenting (safely, of course... you don't wan to experiment with the "rm" command, for example). Hopefully, the "Shell Skills" series has helped to make you more comfortable with the Linux command line. You'll find that a good grasp of the command line can make you incredibly productive, as you learn to string commands together to perform advanced tasks. Just keep practicing, and soon it will be second nature. Good luck!

Tom Dominico is the FirstStep Project Manager. If you'd like to volunteer for the FirstStep section, check out http://linux.com/volunteer.





   Page 1 of 1