Comp150-07: Intelligent Robotics
Introduction to Linux Shell Commands

Don't panic!

This simple introductory guide is for those who have only used icon-based operating systems (like Windows). It will introduce simple concepts and commands you can use in a command-line environment under Linux or Unix. Mac OSX is built on top of a Linux operating system (OS), so you can also use these in a Mac OSX Terminal.

The main difference between a command-line and an icon-based interface is that on a command-line interface, you type in commands that are known to and executed by your machine instead of double-clicking on icons. It may seem counter-intuitive at first, but it's a much more powerful way of getting your computer to do exactly what you want. So don't panic!

The terminal, shell, and prompt

The terminal is a program that brings up the command-line interface you will be using. It is running a shell, which is something that takes your commands and interprets them for the computer, which otherwise only understands binary logic. The most popular shell that you are likely using is the bash (which stands for Bourne Again Shell for obscure reasons). You type your commands at the prompt, which may look something like this:

/home/username/:%

I will be putting the % sign in front of any shell commands to indicate that you type at the prompt.

Directories

Programs and documents are stored as files on a computer, which are placed in directories. The directory structure is a way of organizing the large amount of information and code that any computer stores. The root of the directory tree in linux is /. Some useful directories are: /home//, where your personal files reside, and /usr/bin/, where most program executables are found.

To see where in the directory tree you currently are: % pwd

To move into a directory: % cd /path/to/directory

To move to your home directory, no argument to cd is required: % cd

To move up one level in the directory structure: % cd ..

In general, . (single dot) refers to the current directory, and .. (double dot) refers to the directory one level up.

An absolute path to a file always starts at the root: /path/to/file/deep/in/the/tree/file.txt. You can also specify a relative path from your current directory. For example:

% cd /path/to/file/deep/in/the/tree/
% pwd
/path/to/file/deep/in/the/tree
% cd ../../
% pwd
/path/to/file/deep/in
% cd the
% pwd
/path/to/file/deep/in/the

Creating a new directory: % mkdir new_dir_name (absolute or relative path ok).

Moving files around

Listing files in a directory: % ls

Listing files including hidden (system) files: % ls -a

Renaming a file: % mv old_file_name.old new_file_name.new

Moving a file to a different place: % mv old_path/old_file_name.old new_path/new_file_name.new

Copying a file to a different place: % cp old_path/old_file_name.old new_path/new_file_name.new

Recursively copying a directory: % cp -r old_path/directory new_path/

Checking a text file's contents inside the shell: % more filename
You may need to type q (for quit) to get back to the command line.

Permissions

Each file and directory has associated permissions, which indicate who may or may not read, write (modify) or execute this file or directory. Permissions are specified for the owner, the group that the owner belongs to, and all others. Normally, the owner of the file is the user who created it.
To see the permissions associated with files in a directory: % ls -l

To change permissions on a file: % chmod [u|g|a][+|-][r|w|x] filename

The syntax here means that you pick one or more of the letters u (for user), g (for group) or a (for all), one of + (add permissions) or - (remove permissions) and one or more of the letters r (for read), w (for write) and x (for execute). It's wise not to remove your own permissions.

Some things can only be done if you are logged in as root (administrator, superuser), for example, copying executables to /usr/bin. You should really know a lot more than this short introduction before you do things as root, but in general, the command to act as superuser is sudo followed by whatever command you wanted in the first place. This will prompt you for the root password.

Running programs from the shell

When you type a command at the prompt, the shell looks for the corresponding executable file in places specified by the shell environment variable PATH. If your program is installed in /usr/bin or /usr/local/bin, or elsewhere specified in PATH, you can run it by simply typing: % command_name at the prompt.

If you have installed a new program somewhere other than in a place known to PATH, you will need to give the shell the absolute path to the program every time:
% /path/to/command_name
or, if you are currently in the same directory as your program: % ./command_name

Regular expressions

You can use regular expressions in the shell, for example, to select subsets of files to display. A regular expression matches a substring that you specify and file or directory names (and returns those that match). To that purpose, it uses special characters, such as ? to match any one letter or number, and * to match any substring of letters and numbers. For example:

Matches all files (equivalent to simple ls): % ls *

Matches only files starting with "b": % ls b*

Matches only files with a .txt extension: % ls *.txt

To learn more


Paulina Varshavskaya, email: paulina [at] cs.tufts.edu