Table of Contents

Basic Linux Commands

Introduction

In Linux operating systems there is more incentive to learn and use the command line than in Windows, especially as Microsoft has relegated almost all functions to the GUI. For some tasks it may be more efficient to use the command-line interface (CLI), and for Linux purists and experts, that is often the preferred method. In any case, it is to your advantage to familiarize yourself with the CLI.

This page gives you a basic overview of the commands. For a tutorial you can also go to http://linuxcommand.org/.

Shells and Shell Scripting

A shell is a command-line interpreter: it takes commands and executes them. To do that, it implements a programming language. As you can probably guess, the CLI can be a powerful tool. You can write your own programs, called shell scripts, that can do a variety of things. However, we will focus on basic commands in this guide.

Bash (Bourne-again shell) is the default shell for most current Linux distributions (and Mac OS X) and can be run on most Unix-like operating systems. We make the assumption that you are running Bash, though some commands will work in other shells. To check what shell you are currently using, open the console, type echo $SHELL, and press Enter. It should return a pathname ending with /bash.

In a shell <tab> is your friend. When using the CLI pressing tab once or twice produces very desirable results. One press will complete your command if there is only one possible filename matching what you're typing; pressing twice produces a list of possible filenames.

Commands, Arguments, and Man Pages

Most commands can be modified with arguments, which are specified after the command, that change the behavior of the basic command. Arguments can be fundamental to the command (e.g., a file is the argument to a delete command) or optional. In the latter case, arguments are preceded by a dash (-) in Linux or a forward slash (/) in DOS/Windows.

To see what arguments are available for a certain command, you can search the internet or, conveniently, read the man pages (short for “manual pages”) for that command. Man pages are self-contained documents that can help you more fully utilize the potential of the command line. Simply type man before the command name (the argument), and press Enter. Press q to leave a man page and return to the terminal.

Su ("Switch User") and Sudo ("Switch User Do")

Some commands require administrative access. If you are not already logged in as the root user, you must run these commands with the root user's privileges. You can do this either by typing sudo at the beginning of the command or, alternatively, by running the su command before executing any such command. In the case of sudo, you will be asked for your (user) password. su, on the other hand, requires the use of the root password. (Note: The characters you type do not appear on screen. This is normal.)

Using the command sudo bash or sudo su, you can use bash as root, which can be useful when performing a series of administrative tasks. However, once you have finished, you should revert to your user account, using the command exit (or the shortcut Ctrl-D).

Piping and Appending

Sometimes when asking for help with a problem, other users might ask you to give them the output of some commands (lspci, lsusb, ifconfig, and dmesg are a few faves in this category). To avoid long outputs that go past the screen, you can use the pipe symbol (”|”, the key right above Enter, or <Shift> + <Backslash>) to redirect the output of the command into a different program. Take this example:

sudo lsusb -vvv -s 1:02
sudo lsusb -vvv -s 1:02 | less

In the commands above, the first line will spit out everything the machine can tell you about one USB device, which will eat up several pages! By sending it to the 'less' text viewer, it displays it in sort of a reader, that lets you use up/down/pageup/pagedown to browse freely (press 'Q' to exit). But what if someone on the forums asked for the output? That's easy too… you can send it all directly into a file like this:

sudo lsusb -vvv -s 1:02 >> lsusb_output.txt

That command will dump the output into a file in your current folder, named 'lsusb_output.txt'. If there was already a file by that name, the » symbol will just add it to the end of the file.

Manual Pages ("manpages")

For more information on a command, you can check the manual page. These are built into the system for each command that's installed (even the simple ones!). To open one, type:

man command_name_here
man lsusb

That's it! The manpage will open up and show all kinds of information about the command. (Probably more than you ever wanted to know.) A sample manpage (for lsusb) can be seen here.

Common Commands

Bash command DOS command Function
cat type Display file contents
cd dir cd/chdir dir Change directory to dir
pwd cd (by itself) Displays the current directory
chmod attrib Set file permissions
clear cls Clear screen
cp copy Copy file(s)
date date, time Set/display date and time
diff fc Compare two files
df Display free disk space
echo echo Display message on screen
exit exit Exit shell
free mem Display memory usage
fsck chkdsk, scandisk File system check
grep find Search for file(s) with given string
some_command | grep Search the output of the command for string
history ?? Displays a list of your most recent commands
less more view a long text file or use with |
ls dir Display directory contents
ls -l Like ls, but in a more readable format
find -name filename dir /s filename Search for file called filename
mkdir md/mkdir Make directory
mv move, ren/rename Move or rename files or directories
rm del, deltree Remove file(s)
rmdir rd/rmdir Remove directory
sort sort Sort files
uname ver Show system information
nano edit Launches a basic text editor
kwrite Launches a GUI text editor
XandrosFileManager Launches the Xandros file manager
ip addr Reports your IP address
sudo ifconfig | grep "inet addr" Reports your IP address