Basic Linux
The Linux command line is a text interface to a computer. It is also named shell, terminal, console or command prompt.
The Linux command line is a computer program intended to interpret commands.
It allows users to execute commands by manually typing at the terminal or
has the ability to automatically execute commands grouped in Shell Scripts
.
In the following, you will learn about the most common and useful commands for the terminal.
For more details please visit the Unix/Linux Command Reference, or follow the interactive tutorial from linux survival.
Try it by yourself
The best way to start getting comfortable using a tool, is to actually try it!
Feel free to play around and experiment. Except for rm
(the delete command),
most commands, if used carefully, should be safe to run.
Basic Linux commands
In a Linux shell, commands, files and directory names are case-sensitive meaning that typing pwd
will print the current working directory and typing PWD
will return an error similar to -bash: PWD: command not found
ls
To LiSt the content of a directory, type ls
in the terminal.
This command will return a list of the content of a directory. Running ls $HOME
will show all contents stored in your $HOME
directory.
Without any argument ls
will show the content of the current directory. Two of the most useful options for the ls
command are:
- -l: will display a long list with additional useful information like permissions, file size and last modification time.
- -a: will show hidden files or subdirectories
$HOME
$HOME
is an environment variable. An environment variable allows to alleviate the pain of typing its value every time. It also provides an abstraction, making tools more generic.
On most Linux systems, $HOME is set to be your user folder, /home/my_username/
.
pwd
The pwd
command stands for Print Working Directory and will show you the name of your current working directory.
For instance after logging on the system, it should give you:
pwd # print working directory
# /home/your_username
cd
With the cd
command, which stands for Change Directory, you can change your working directory.
For example, if you are currently working in the directory /home/your_username
and you want to switch to /etc/
, you just can change your working directory using the command cd /etc
.
cd /home/my_directory # Move to /home/my_directory directory
pwd # print working directory
# /home/my_directory
cd /etc # switch current directory to /etc
pwd # will allow you to check the change of directory has been made
rm
The command rm
is used for deleting (ReMoving) files or directories. For example, rm ./foo.txt
will delete the foo.txt
file in your current directory. To delete directories, use the -r
option, which stands for recursive. An example would be rm -r ./test
. This will delete the directory named test
with all files and subdirectories in it.
rm foo.txt # delete the foo file in your current directory
# delete the directory "my_directory" with all files and sub-directories in it
rm -r my_directory
Deleting is permanent
Using rm
permanently deletes your files. You will not be able to retrieve them unless a backup has been set up.
clear
The clear
command will clear the terminal screen. This command can be useful if you worked for quite a while and are getting lost with what is displayed in your terminal. Just run clear
– this lets you “wipe out” all words in the terminal.
mv
You can MoVe
your files or directories to somewhere else with the command mv
. For example, mv test /tmp
will move the file test from the current working directory to the /tmp
directory. mv
can also be used to rename a file: mv test foo
will rename the file originally named test
to foo
.
# will move the file test from the current working directory to the /var director
mv test /tmp
ls /tmp # check that the file 'test' has been moved
# will rename the file originally named “test” to “foo”
mv test foo
ls # test that the file 'test' is now called 'foo'
mkdir
The command mkdir
means make directory and allows for the creation of new directories.
Something like mkdir /home/my_username/test_dir
will create a new directory named test_dir under the /home/my_username
directory. There is also a useful option -p
. This means “make parent directories as needed”. Imagine having an empty directory /home/my_username
, then you want to create a directory called workspace
and a directory docs
under workspace. You can create the directories one by one in these steps:
mkdir workspace # Create a directory "workspace"
cd ./workspace # Move to the directory "workspace"
mkdir docs # Create a directory "docs” under "workspace"
But using the -p
option is more efficient: mkdir -p workspace/docs
will create the docs
directory, and as the parent directory workspace does not yet exist, mkdir
will create it as well within this same command.
# Create the following folders hierarchy "workspace/docs"
mkdir -p workspace/docs
cp
cp
is a command used to copy files or directories. Imagine you have a file named my_file.c
in the current directory, and you want to copy it to the directory /tmp
. In this case, the cp
command can help you by copying the file with cp ./my_file.c /tmp
. If you want to copy a directory, the -r
option can help you together with the cp
command. As an example, cp -r ./test /tmp
will copy the directory named test to /tmp
with all files and subdirectories in it. After that, you will see a directory /tmp/test
, which has the exact same contents as the original test directory. You can also easily rename a directory or file when executing cp
. For example, cp ./test /var/foo
will first copy your test directory to /tmp
, but it will then rename it to a new directory name foo under the /var
directory.
cp my_file.c /tmp # Copy "my_file.c" to the "/tmp" directory
# Copy "my_folder" and all files and sub-directories in it to the "/tmp" directory
cp -r my_folder /tmp
man
The man
command displays the manual of the command you provided. As an example, man ls
opens the manual of the command ls
. To exit the manual, just press q for “quit”. You can use man for every command mentioned in this article to display the respective manual.
man ls
vi / vim
vi
is an interactive text file editor which has been available since the early days of Unix.
vim
for vi IMproved
is an improved clone of vi
. For more detailed on vi
commands please visit the Vim cheat sheet.
nano
nano
is also a classic terminal file editor. For more detailed on nano
commands please visit the Nano editor guide
Streams
One of the most important feature under Linux/Unix system is stream (or I/O) redirection. This command line's feature allows to redirect the input and/or output of commands from and/or to files.
All the commands run produce two kinds of output:
- Command result: data the program is designed to produce, and;
- Program status and error messages: that informs a user of the program execution details.
In Linux systems, there are three default files named below which are also identified by the shell using file descriptor numbers:
- 0 or stdin: it is connected to the keyboard, most programs read input from this file.
- 1 or stdout: it is attached to the screen, and all programs send their results to this file and
- 2 or stderr: programs send status/error messages to this file which is also attached to the screen.
Therefore, I/O redirection allows you to alter the input source of a command as well as where its output and error messages are sent to. And this is made possible by the <
and >
redirection operators.
Overwrite operators
Commands with a single bracket (<
or >
) overwrite the destination’s existing contents.
Command | Description |
---|---|
> | standard output |
< | standard input |
2> | standard error |
Append operators
Commands with a double bracket (<<
or >>
) do not overwrite the destination’s existing contents.
Command | Description |
---|---|
>> | standard output |
<< | standard input |
2>> | standard error |
Redirect Standard Output to File in Linux
Redirecting standard output to a file is helpful to store the output of a command for later inspection. For instance:
echo "Hello world" > output_redirection.log
To view the contents of output_redirection.log
file use the cat
command:
cat output_redirection.log
Output
Hello world
To append the output of a command, use the >>
operator.
For instance to append the output of the command above in the output_redirection.log
file the double bracket can be used:
echo "Hello world" >> output_redirection.log
Then to view the contents of output_redirection.log
file the cat command:
cat output_redirection.log
Output
Hello world
Hello world
Redirect Standard Error to File in Linux
To redirect the standard error of a command, the file descriptor number needs to be explicitly specified, for instance, 2
for the shell to understand what you are trying to do.
For instance the ls
command below will produce an error when executed by a normal system user without root privileges:
ls /root/
You can redirect the standard error to a file as below:
ls /root/ 2> output-error.log
To view the contents of output_redirection.log
file use the cat
command:
cat output-error.log
Output
ls: cannot open directory `/root/`: Permission denied
In order to append the standard error, use the command below:
ls /root/ 2>> output-error.log
Output
ls: cannot open directory `/root/`: Permission denied
ls: cannot open directory `/root/`: Permission denied
Redirect Standard Output/ Error To One File
It is also possible to capture all the output of a command (both standard output and standard error) into a single file. This can be done in two possible ways by specifying the file descriptor numbers:
- The first and indirect method works as follows:
ls /root/ > output-error.log 2>&1
The command above means the shell will first send the output of the ls
command to the file output-error.log
(using > output-error.log
), and then writes all error messages to the file descriptor 2
(standard output) which has been redirected to the file output-error.log
(using 2>&1
). Implying that standard error is also sent to the same file as standard output.
- The second and direct method is:
ls /root/ &> output-error.log
You can as well append standard output and standard error to a single file like so:
ls /root/ &>> output-error.log
Redirect Standard Input to File
Most, if not all commands get their input from standard input, and by default standard input is attached to the keyboard.
To redirect standard input from a file other than the keyboard, use the <
operator as below:
# Create an input fie
echo "Make the HPC great again with MeluXina" > input_file.txt
# Redirect standard input from a file
cat < input_file.txt
Output
Make the HPC great again with MeluXina
Basic shell scripting
Shell or Bash scripting is a way automate repetitive tasks. Bash scripts execute within a Bash shell interpreter terminal. All commands you can run in a terminal can be run within a Bash script.
By convention and to ensure that the system is able to find and execute the Bash scripts. The beginning of your script file should start with #!/bin/bash
on its first line. This tells the system which type of interpreter to use for the script.
#!/bin/bash
echo "My first bash script !"
The script files also need to have the execute (x
) permission to allow them to be run. To add this permission to a file with filename, the chmod command is used:
chmod +x my_first_bash.sh
Variables
Within bash scripts, variables are simply declared by setting the variable name equal to another value. For instance, to set the variable person
to “John Doe”, you would use the following syntax:
#!/bin/bash
person="John Doe"
Note
Note that there is no space within the variable name or surrounding the the equals sign.
To access the value of a variable, we use the variable name pre-pended with a dollar sign ($). In the previous example, if we want to print the variable value to the screen, we use the following syntax:
echo $person
Conditionals
When bash scripting, conditionals are used to control which set of commands within the script run. Use if to start the conditional, followed by the condition in square brackets ([ ]). then begins the code or commands that will run if the condition is met. else begins the code or commands that will run if the condition is not met. Lastly, the conditional is closed with a backwards if, fi.
A complete conditional in a bash script uses the following syntax:
#!/bin/bash
index= 2
if [ $index -lt 5 ]
then
echo $index
else
echo 5
fi
Bash scripts use a specific list of operators for comparison. Above we used -lt which means “less than”. The result of this conditional is that if $index is less than 5, it will print to the screen. If it is 5 or greater, “5” will be printed on the screen.
Here is the list of comparison operators for numbers:
Description | Operator |
---|---|
Equal | -eq |
Not equal | -ne |
Less than or equal | -le |
Less than | -lt |
Greater than or equal | -ge |
Greater than | -gt |
Is null | -z |
Note
When comparing strings, it is best to surround the variable with quotes ("). This prevents errors if the variable is null or contains spaces. The common operators for comparing strings are:
Description | Operator |
---|---|
Equal | == |
Not equal | != |
For example, to compare if the variables foo
and bar
contain the same string:
if [ "$foo" == "$bar"]
Inputs
To make bash scripts more useful, we need to be able to access data external to the bash script file itself. The first way to do this is by prompting the user for input. For this, we use the read syntax.
We can access external files to our script. You can assign a set of files to a variable name using standard bash pattern matching using regular expressions. For example, to get all files in a directory, you can use the *
(wildcard) character:
files=/temporary/directory/*
You can then iterate through each file and do something. Here, lets just print the full path and filename:
for file in $files
do
echo $file
done
Loops
Loops are one of the fundamental concepts of programming languages. Bash loops are very handy. Loops allow to run a series of commands and keep re-running them until a particular situation is reached. They are useful for automating repetitive tasks.
There are 3 basic loop structures in Bash scripting, while
, until
and for
While Loops
The while loop is used to perform a given set of commands an unknown number of times as long as the given condition evaluates to true.
The condition is evaluated before executing the commands. If the condition evaluates to true, commands are executed
#!/bin/bash
i=0
while [ $i -le 2 ]
do
echo Number: $i
((i++))
done
Note that if the variable i
is not increased (((i++))
), the loop will never exit.
Until Loops
The until loop is used to execute a given set of commands as long as the given condition evaluates to false
#!/bin/bash
counter=0
until [ $counter -gt 5 ]
do
echo Counter: $counter
((counter++))
done
The loop iterates as long as the counter variable has a value greater than four. The script will produce the following output:
Warning
- The while executes a piece of code if the control expression is true, and only stops when it is false (or an explicit break is found within the executed code).
- The until loop is almost equal to the while loop, except that the code is executed while the control expression evaluates to false.
For Loops
The for loop iterates over a list of items and performs the given set of commands. The list can be a series of strings separated by spaces, a range of numbers, output of a command, an array, and so on.
#!/bin/bash
PERSON=('John Doe' 'Alfred Hitchcock' 'Harry Potter')
for person in "${PERSON[@]}"; do
echo "Person: $person"
done