IT and Library Services

Basic Linux Commands

A list of basic instructions to help you in the HPC environment.

Information on this page provides basic commands that you should be familiar with when working within a Linux command line interface (CLI), such as the HPC.

In the examples below, the commands themselves are highlighted in yellow and are followed by the output they produce. Select a topic for details.

ls : list files in a directory
pwd : show full path to current directory
mkdir : make directories
cd : change directory
cp : copy files and directories
rm : remove files
rm -rf : remove a directory and everything in it
man : show usage manual for a command
* ? and other wildcards
~ : shortcut to your home directory

ls : list files in a directory

Simple file listing: [abc123@seldon abc123]$ ls

figure1.fig test.m test.sh

Display extra details (file size, type, date, permissions) with the "long" (-l) option: [abc123@seldon abc123]$ ls -l

total 12 -rw------- 1 abc123 users   1121 Oct 30 16:15 figure1.fig


-rw------- 1 abc123 users     69 Oct 30 16:15 test.m

-rwx------ 1 abc123 users     40 Oct 30 16:10 test.sh

You can use wildcards to select only some of the files. [abc123@seldon abc123]$ ls -l test*

-rw------- 1 abc123 users     69 Oct 30 16:15 test.m -rwx------ 1 abc123 users  40 Oct 30 16:10 test.sh

pwd : show full path to current directory

Print working directory path: [abc123@seldon abc123]$ pwd

/sscc/home/a/abc123

mkdir : make directories

Create a new subdirectory called subdir in the current directory: [abc123@seldon abc123]$ mkdir subdir

[abc123@seldon abc123]$ ls -l total 16


-rw------- 1 abc123 users   1121 Oct 30 16:15 figure1.fig

drwx------ 2 abc123 users   4096 Nov 4 13:55 subdir -rw------- 1 abc123 users  69 Oct 30 16:15 test.m

-rwx------ 1 abc123 users     40 Oct 30 16:10 test.sh

cd : change directory

Switch to a subdirectory called subdir in your current directory: [abc123@seldon abc123]$ cd subdir

[abc123@seldon subdir]$

Two dots .. denote one directory up in your path: [abc123@seldon subdir]$ cd ..

[abc123@seldon abc123]$

To use the full path start with a slash: [abc123@seldon abc123]$ cd /datalib/cps

[abc123@seldon cps]$ pwd /datalib/cps

cp : copy files and directories

Syntax: cp [options]... Source Destination

cp [options]... Source... Directory

Options:

-i : prompt before overwriting files
-r : copy recursively (copy subdirectories and their contents)
-u : overwrite destination files only when the source file is newer
-v : show what is being done

Make a copy of a file called make.do with a different name: [abc123@seldon abc123]$ cp -v make.do make2.do

`make.do' -> `make2.do'

Copy one or more files to another directory: [abc123@seldon abc123]$ cp -v *.do subdir/

`make.do' -> `subdir/make.do' `make2.do' -> `subdir/make2.do'

Copy the contents of a directory and its subdirectories to another place: [abc123@seldon abc123]$ cp -vR subdir/* subdir2/

`subdir/make.do' -> `subdir2/make.do' `subdir/make2.do' -> `subdir2/make2.do'


`subdir/data/file.dat' -> `subdir2/data/file.dat'

rm : remove files

Remove a file called make.do : [abc123@seldon abc123]$ rm make.do

To remove a file with spaces in its name, use quotes: [abc123@seldon abc123]$ rm "file name.txt"

To remove multiple files (or to save time typing long file names) use wildcards. Be careful - wildcards are powerful and could match more files than you intended. Use the ls command first to check what files will be deleted. [abc123@seldon abc123]$ rm *.txt

rm -rf : remove a directory and everything in it

Delete a subdirectory called subdir and all of its contents (including its subdirectories). [abc123@seldon abc123]$ rm -rf subdir

man : show usage manual for a command

Display a more detailed manual for a command:

Press <Space bar> to view the next page
Press q to exit

[ate533@seldon ate533]$ man cp
CP(1)                                 FSF
CP(1)

NAME
cp - copy files and directories

SYNOPSIS
cp [OPTION]... SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... --target-directory=DIRECTORY SOURCE...

DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
. . .

* ? and other wildcards

You can use these powerful wildcards with any command that takes filenames as an argument. For example prog??.* will match all files with names that start with prog followed by any two characters, a dot, and anything (or nothing) following the dot.

Wildcard Matches
* zero or more characters
? exactly one character
[abxy] exactly one character from the list (a, b, x or y)
[a-e] exactly one character in the given range (a, b, c, d or e)
[!abxy] any character that is not listed
[!a-e] any character that is not in the given range
{beef,fish,tofu} exactly one entire word from the list in curly brackets (beef, fish or tofu)

~ : shortcut to your home directory

You can reference your home directory in your commands and programs with the character tilde ~ shortcut. If your home directory is

/sscc/home/a/abc123

then instead of writing out the full path to the file

/sscc/home/a/abc123/data/input.dat

you can just write

~/data/input.dat


Further Information

The software installed upon our HPC runs under the Linux/Unix operating system.

Ther is an excellent beginners' guide to the basic commands , which you may need, written by M. Stonebank at the University of Surrey. It is worth putting in some effort here as your research productivity will be really enhanced by learning Linux.

Tutorials are shared under a Creative Commons License.