Linux Command-Line Cheat Sheet

This article is reprinted from The Official Ubuntu Book, 2nd Edition, by Benjamin Mako Hill and Jono Bacon, with permission of publisher Prentice Hall Professional, copyright 2007, all rights reserved. While instructions are specific for Ubuntu Linux, most commands will work with other Linux distributions.

Moving Around the Filesystem

Commands for moving around the filesystem include the following.

  • pwd: The pwd command allows you to know the directory in which you're located (pwd stands for "print working directory"). For example, pwd in the desktop directory will show ~/Desktop. Note that the GNOME terminal also displays this information in the title bar of its window.
  • cd: The cd command allows you to change directories. When you open a terminal, you will be in your home directory. To move around the filesystem, use cd.

      •  To navigate to your desktop directory, use cd ~/Desktop

      •  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 directories 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 by using the following commands.

System Information Commands

System information commands include the following.

 

 

 

The following commands list the hardware on your computer, either of a specific type or with a specific method. They are most useful for debugging when a piece of hardware does not function correctly.

 

 

 

Searching and Editing Text Files

Search and edit text files by using the following commands.

 

Both grep and sed are extremely powerful programs. There are many excellent tutorials available on using them, but here are a few good Web sites to get you started:

 

Three other commands are useful for dealing with text.

    • cat: The cat command, short for concatenate, is useful for viewing and adding to text files. The simple command cat FILENAME displays 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 pressing Ctrl followed by the letter.

 

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

 

 

 

 

 


Getting Help on the Command Line

This section provides you with some tips for 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 produces a short usage description of the command and its options, then exits back to the command prompt. Try man -h or man --help to see this in action.
Every command and nearly every application in Linux has a man (manual) file, so finding such a file is as simple as typing man command to bring up a longer manual entry for the specified command. For example, man mv brings up the mv (move) manual.
Some helpful tips for using the man command include the following.

 

 

 



There are also info pages, which are generally more in-depth than man pages. Try info info for the introduction to info pages.

Searching for Man Files

If you aren't sure which command or application you need to use, you can try searching the man files.

 



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, you can use a series of wildcards to accomplish such tasks.

 

 

 



Executing Multiple Commands

Often you may want to execute several commands together, either by running one after another or by passing output from one to another.
Running Sequentially
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, after the character used between the commands, you need something called a pipe, 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.

Moving to More Advanced Uses of the Command Line

There are a great number of good books out there for working the command line. In addition, because most of the command line has not changed in many years, a large body of information is available on the Internet. If you need help with something, often simply searching for the command will turn up what you need.
To get you started, here are some recommendations.

 

    • LinuxCommand.org is an excellent Web site designed to help people new to using the command line.

 



This content was excerpted from the new second edition of The Official Ubuntu Book, authored by Benjamin Mako Hill and Jono Bacon, with Ivan Krstic, David Murphy, Jonathan Jesse, Peter Savage and Corey Burger, published by Prentice Hall Professional, July 2007. Copyright 2007 Canonical Ltd., all rights reserved.

7 questions to ask your EMM provider about GDPR compliance
  
Shop Tech Products at Amazon