A.3 Basic Linux Commands

Complete: 5
Detailed Review status

Goals of this page:

This page is intended to provide some basic linux commands and references to more linux information.



With the commands listed in this topic you can do most basic operations for file management and executing scripts. CMS-specific commands, such as running the CMS software, are discussed in other sections of the WorkBook.

Linux Basics

Linux has a hierarchical filesystem, supporting many levels of directories, with case-sensitive filenames, directory names and commands. Filenames should not be stored with spaces (as this can cause problems for some scripts, and needs special care in any case), and the only punctuation that should be used in filenames is the dot (.), underscores are allowed. Generally filenames in linux are of the form filename.xyz, where xyz is a 2- or 3-letter combination which indicates the file type (e.g. txt for a text file, .C for a C-based macro (usually root), ps for a postscript file, ...).

In Linux the slash (/) is used to separate directories, by contrast with the backslash (\) used in Windows. There are three 'special' directories

  • . indicates the current directory
  • .. refers to the directory above the current directory (the parent directory)
  • ~ is the users 'home' directory - the directory you start in when you login
Each user belongs to one or several groups and when working will be using one of these groups. Each file and directory has an owner and a group associated. In addition to that there are several permission flags which specify if the file(directory) is readable, writable and/or executable. In example:

-rw-r--r-- 1 livio zh 22769 Jan 17 16:26 myfile.txt

permissions are summarized by -rw-r--r-- which is composed by 4 different fields :

  "-"       "rw-"      "r--"      "r--" 

First field "-", specifies if it's a file, a directory or a link. Other fields define the "user", "group" and "others" permissions by a set of 3 characters "rwx" (read, write and execute). In the analyzed example "livio" is the user and "zh" is the goup related to the file. Summarizing the permissions for file "myfile.txt": all the users can read the file but only the user "livio" can modify it.

When working in Linux, you will activate a terminal , and inside the terminal shell will be running, i.e. a program interpreting your commands. Commands are typed inside this shell, and you press ENTER to issue them. For most commands, additional arguments can be issued by using minus signs (where \ signs are used in Windows), e.g. command -a - b -c or command -abc. To find out what options are available for a given command, and what those options do, type command --help, which works in most cases, or try to see more detailed information with the man command.

Filenames can be specified using two types of wildcards:

  • a * represents 0 or any combination of characters, e.g. for* can refer to fore, forest, fort, for etc if such files exist,
  • a ? represents any signal character, i.e. in this example for? can refer to fore or fort, but not forest. nor for

You need to be very careful when deleting files using wildcards to specify filenames (just rm * removes everything!! rm -i * will ask you interactively)

Different shells

Linux comes in 2 different flavours: Bourne shells (sh, ksh, bash,zsh) and C shells (csh,tcsh), which differ only in some small respects (environment variables, redirection,...) but not in the principles. Shells have more or less been developed as given in these lists from left to right, i.e. bash will be more powerful than its ancestor ksh, and tcsh more powerful than csh. Where necessary, both forms will be given in what follows

You can switch from one shell to the other just by typing , e.g., /bin/bash, or /bin/tcsh, which can be useful in case a macro or a documentation comes only in one flavour (you may prefer to do bash --login for instance to properly run login scripts). But attention, in some cases this may screw up your environment!

In order to know which shell is the default for you (as decided by the system administrator), type echo $SHELL.

In order to know which shell you are actually using, type echo $0.

Commonly used Linux Commands

Command Usage
pwd Find out directory you are currently in
cd dir1 Change to directory dir1 below the one you are in
cd /home/user/dir2 Change to directory dir2 from anywhere
cd .. Change to the parent directory of the one you are currently in
cd Change to your home directory
mkdir dir1 Create directory dir1 as a subdirectory of the one you are in
mkdir /home/user/top/dir2 Create directory dir2 from anywhere (requires directory structure above dir2, already exists)
rmdir dir1 Delete directory dir1 below the one you are in. (rmdir only works if the directory is empty.)
rmdir /home/user/top/dir2 Delete directory dir2 from anywhere
ls list the contents (files, symbolic links and directories) of the current directory
ls dir1 List contents of subdirectory dir1 of the directory you are in
ls /home/user/top/dir2 List directory dir2 from anywhere
ls -ltrA Produce a long listing (-l) with the latest modification date (-t) last (-r), showing almost all hidden files (those starting with a dot, except "." and "..") (-A)
cat file.txt Display the contents of a (text) file on the screen
tail file.txt Display the last 10 lines of a (text) file on the screen
more file.txt Display the contents of a (text) file on the screen, one screen at a time
less file.txt Display the contents of a (text) file on the screen, allowing to scroll both upwards and downwards; use less -S file.txt to also scroll sideways
rm file1 Delete file file1 from the current directory
rm /home/user/top/file2 Delete file file2 from anywhere
rm *.txt Delete all the files ending in .txt in the current directory
rm -i *.txt Delete all the files ending in .txt in the current directory after having asked for confirmation
rm -R dir1 Recursively delete all files in all directories below dir1 including dir1 itself
mv oldfile newfile Rename (move) oldfile to newfile in the current directory
mv oldfile /home/user/top/oldfile Move oldfile to the directory /home/user/top
chmod ug+rx file1 Change permissions on file1 for user and group to readable and executable (+rx)
chmod go-r file1 Change permissions on file1 for group and other users to unreadable (-r)

Checking the directory quota

You can check how much space you have available in your directory with the command fs lq (at lxplus).

Environment variables

You can define 'environment variables' which you can use to change the behaviour of your shell scripts or programs. echo $Var will print the value of the environment variable Var, printenv will print all environment variables that are defined in your shell.

Syntax for setting a variable is different for Bourne shells and C shells. To set the environment variable Var to contain the string xyz you will type

  • in Bourne Shells

export Var=xyz

  • in C Shells

setenv Var xyz

Redirection of output

A command in UNIX will very often issue some normal output (called stdout), and some error output (called stderr). You may redirect output to a different destination in various waysusing > (= redirect to) or >> (= append to). For example, if Var1 was set to =xyz, and Var2 to 123, and you type echo $Var1 >myfile, followed by echo $Var2>>myfile, the file myfile will contain the 2 lines xyz and 123.

Redirection to /dev/null will suppress the output.

There are 2 output streams, stdout for normal output, stderr for warnings. Here syntax is different in the 2 shell families:

  • in Bourne Shells
command> myfile will redirect the stdout of command to myfile

command 2> myfile will redirect the stderr of command to myfile

command 1 > myfile 2>&1 will redirect the stdout of command to myfile, and the stderr to the stdout, ie.e to the same file

  • in C Shells
command> myfile will redirect the stdout of command to myfile

command >& myfile will redirect the stderr of command to myfile

command  >& myfile will redirect the stdout and the stderr to myfile

(command >out) >& err will redirect the stdout of command to out, and the stderr to err

You may also redirect output from one command to be input for a second command using a pipe (= |):

ls -l | more

will display the output from ls inside more, i.e. one screen at a time.

Running Commands from a File

The command source myfile (. myfile in C-shells) executes the commands stored in myfile. This would normally be a shell script file.sh or file.csh. You can search the internet for "shell script reference" or a similar string, and find many references to help you get started writing shell scripts.

There are several powerful scripting languages used in Unix, like python or perl.

More information on scripting can be found in the documentation links given below, or by carrying out a basic internet search.


grep is a filter, i.e. searches for strings inside a file:

command | grep txt will show all output lines of command containing the string txt.

command | grep -v txt will show all output lines of command except those containing the string txt.


  • vi
is a text editor which is , in its simplest form, present on all Linux systems.

  • emacs
is a very powerful and popular GNU text editor

  • nano
Very simple on screen text editor

Links to further documentation

Information Sources

SteveLloyd is the original author - see WorkBookAcknowledgements.

Review status

Reviewer/Editor and Date (copy from screen) Comments
MargueriteBeltTonjes - 16 Nov 2020 fix broken links, ensure information up to date
AnneHeavey - 17 May 2006 updates after review by Kati L-P
JennyWilliams - 18 May 2006 updates after review by Kati L-P
UrsulaBerthon - 23 Jan 2008 updates
-- JohnStupak - 18-September-2013 Review

Responsible: SudhirMalik
Last reviewed by: UrsulaBerthon - 23 Feb 2008

Edit | Attach | Watch | Print version | History: r33 < r32 < r31 < r30 < r29 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r33 - 2021-08-16 - MargueriteTonjes



    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    CMSPublic All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2023 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback