Skip to main content

Basics of the Command Line

Most use of remote computers, and performing more advanced tasks in Linux systems requires use of the command line, terminal or shell. From the desktop open the Terminal application from the menu.

On the left is the command prompt, usually [user_name]@[machine_name], where the square brackets mean "this information goes here", followed by some symbol(s), >, : $ etc. You should see something like the following image:

Blank desktop with terminal


Note: The default shell on the SCRTP desktop is called bash. You may also use machines which have other variants, such as cshell. Some commands differ significantly. We try to note this in what follows. Some Warwick research groups require that you use cshell.

Getting Started

Your terminal window is showing your Home directory (or folder). This is where all your documents will be stored. Home directories are backed up regularly and have a quota (maximum amount of space you are allowed to use). Storage directories are also available for keeping large files which do not need backup. See here for more information.

At the command prompt, typing

username@godzilla:~> ls

will list the folders you currently have. You will should see a Documents and a Desktop folder among others.

Change to the Documents folder using

username@godzilla:~>cd Documents

and create a folder for this exercise called Example using the command

username@godzilla:~>mkdir Example

Change to this folder (username@godzilla:~>cd Example ) moves into the Example folder. You may notice that your command prompt now looks like

username@godzilla:~/Documents/Example>

showing the folder you are in. This is not always shown. Another way to see what folder you are in is the command username@godzilla:~>pwd. This prints the full path (the entire route, including the home/[group]/[username] part) to the present working directory (current folder).

Some Command Line Tools

Most command line tools take arguments, which tell them what to act on. For example, cd requires a destination folder. Many also have optional arguments in the form of switches or flags to change their behaviour. For instance, the ls command can be given the -l flag to output "long" information, or -h to output file sizes in "human readable" format. These can be given separately or combined, e.g.

username@godzilla:~>ls -lh

username@godzilla:~>ls -l -h

Often there is both a short flag (one '-' and a single letter) and a long variant (two '--' and a long name) which do the same thing, and many commands have far more options than the 62 letters and numbers cover in the short form.

Note: If copy-and-pasting commands including the long variety of flags, check that the hyphens are intact, as some text editors and websites "prettify" these into a single long dash.

username@godzilla:~>ls -a

will list a directory including all "hidden" files and folders. These start with a '.' and can be created and edited like any other file, but often aren't shown in the file explorer. As well as files, the ls -a command also shows '.' and '..' entries. The single dot stands for the current directory, the double dot for the parent. The tilde '~' stands for home.

You can move back up to the parent directory using

username@godzilla:~>cd ..

and up to your home with

username@godzilla:~>cd

or

username@godzilla:~>cd ~

Finally

username@godzilla:~>cd -

will move back to the "previous" directory. This is useful to change between two directories repeatedly.

Text Editors

Linux comes with several text editors. Gedit is similar to Notepad or Wordpad from Windows machines, and can be invoked either from the Menu in the bottom-left of the desktop, or from the command line. Because this opens in a window, it can be impractical to use from a remote machine, such as the CSC cluster machines. Also, a lot of tools use the default editor for the system.

Vim, emacs and nano are the commonest available editors. If you have a favorite, use it. If not, you will probably develop one, but for the present, the default on CSC machines is vim.

We recommend spending 15 minutes with the excellent vim tutor if you have never used a command-line editor before, in particular Lesson 1.2 on how to exit vim. Start the tutor by typing

username@godzilla:~>vimtutor

and work through Lesson 1, which covers enough for this introduction.

More Files and Directories

Now create a file tmp.txt and put some text into it using vim and the commands from the tutor.

Duplicating files uses the cp command. The first argument is the file to move, the second is the destination

username@godzilla:~>cp tmp.txt tmp2.txt

Both source and destination can be given a full file path, which can include the ., .. and ~ as, for example,

username@godzilla:~>cp ./tmp.txt ~/tmp2.txt

The destination can be a directory, in which case the file name will be the same as before.

Files are deleted with rm. Entire directories can be deleted with their contents by using rm -r but be careful, as there is no prompt. The '-i' option to rm will prompt before deleting each file.

**Warning** There is no equivalent of the Recycle bin or Trash for rm. Deleted files can't generally be recovered. If in doubt, use the -i option. Be very very careful with the option -f or --force, which can delete important files.

Tab completion

As file names and paths get more complicated, the <tab> key becomes very useful. Typing part of a command or a filename and pressing <tab> will offer possible completions. If there is one and only one, the command will automatically be completed. Try moving to your home directory (cd ~) and typing, for example, mv Exa<tab>tm<tab>

Finding Things

If you have a lot of files, it can be helpful to list only subsets. For example, list only files starting with the letter s and ending in the extension '.txt' use

username@godzilla:~>ls s*.txt

The '*' here is a wildcard, which matches any number of characters. A '?' matches one and only one character. This is an example of a 'pattern matching rule'. Arbitrarily complex patterns can be used using regular expressions. Reg-exes come in many "flavours" depending on the computer language they are used in, and are mostly beyond the scope of this introduction, but are the tool to investigate if you need to select things based on a common pattern. As a quick example, if we create the files tmpa.txt, tmp0.txt and tmp1.txt, then the command ls tmp[0-9].txt will select only the latter two.

The standard way to find files is the find command. This can be used to, for example, find all files modified less than 1 hour ago with names matching the tmp[0-9].txt pattern

username@godzilla:~>find -mtime -1 -name 'tmp[0-9].txt'

Find is very powerful, so even a brief description of its use could fill many pages. The next page will discuss how to get help for commands and their arguments.

Finding files that contain certain text, and finding that text within them is the final common task for this introduction. This is another powerful command which makes extensive use of regular expressons, so only a brief introduction is given here.

For example, to find the word "Hello" in all files in the current directory use

username@godzilla:~>grep "Hello" ./*

The first argument is the string to find, the second is the list of files to search. In this case, we used the * wildcard to mean "all files in the current directory". This command prints the line containing the given string, and if there's more than one file in the list, the name of the file where it was found. To check files in subdirectories too, use the -r flag with the grep command.

Summary

This page has run through a lot of new information and commands. Even if you do most of your work using the mouse and menus, you are likely to use most, if not all, of these commands regularly, as well as many more.

Find and grep are examples of the power of linux command line tools. The next section of this tutorial will cover how to find the right command, and how to work out how to use it and its arguments.

Next Section

Getting help with commands