Introduction to Linux and Bash
Dr. Alima Dajuma, PhD
University Peleforo GON COULIBALY (Côte d’Ivoire)
alima.dajuma@aims.ac.rw/alima.dajuma@yahoo.com
Introduction to Linux
Command Line interface &
Scripting – The basics
Linux
• Operating system prompt
• Distributions (software
collection + package
management system)
• Ubuntu
• Fedora User name
• Suse host
• Free and open-source; Current directory
powerful for research data
centers
• Personal for Desktop or
Community.
Console
Bash: Bourne Again Shell
• Shell: a program that interpret a command given by user and execute it.
• Bash: popular UNIX shell: command line interface and scripting language.
• Default shell (many others including sh, ksh, tcsh…)
• easy to create and very useful to perform some tasks when dealing with
climate data.
Why do we use Linux and bash?
• Shell (Bash Script)
• Command-line interface • Powerful scripting language
(CLI): most powerful and (compiled several commands
flexible way to interact with and run it at once)
the computer • Reproducibility
• Commands tell the the • Save time
computer what to do • Portable and almost installed
• Combined commands (i.e. get everywhere
faster) • Managing jobs
• managing external programs
Some applications
• Getting information
• Manipulating files and working with directories
• Printing file and string contents
• Networking
• Running a bash script (be descriptive and organized)
Command Line Interface
alima@aimsit:~$ ls –l Desktop
Prompt alima@aimsit:~$
• command line ls
• option –l
• argument Desktop
• cursor
Getting information & display command: echo
Commands:
whoami $ echo Hello AIMS
hostname Hello AIMS
date $ echo Hello AIMS
pwd Hello AIMS
cal
Task do all examples by yourself and tell me the difference
(tips: Using up-arrow for the last command)
echo “Hello AIMS-CMR”
echo ‘Hello AIMS-CMR‘
echo –e “Hello \nAIMS-CMR”
Effects of single and double quotes
There are two types of variables:
• Environment or system variables (i.e. $HOME, $SHELL)
• User variables
• Each variable begins with $.
echo My home is $HOME
Task: Try out the different effects of quoting by print out the variable $HOME.
Getting out of trouble
Some troubles could be:
○ Unfinished typing of a command.
○ long and endless running
Some solutions: Ctrl C (simultaneously)
Examples:
$ echo “ Hello
$ cat
Getting help: man
man command is a command which displays on-line manual pages
e.g. $ man echo will open a manual page on the command echo.
use arrow key to navigate (up & down)
Use q to quit the manual
/write will search for write in the file
Whenever you need help with a command type “man” and the
command name: e.g. man ls
Result of man ls
Manipulating files : create file
create an empty file
touch aims.txt
write information to the file:
• operator > (overwrites files)
• operator >> (append )
echo “AIMS stands for African Institute for Mathematical Sciences ” >
aims.txt
echo “This course is about Linux basis .” >> aims.txt
cat aims.txt
use the tab key (make like easier)
Manipulating files (2) : list, rename, copy, delete
• list files and directories
• ls
• Task: What does ls -l do? Try also ls –lrt.
• rename a file
• mv aims.txt limbe.txt
• copy a file
• cp aims.txt test.txt
• delete a file
• rm test.txt
• Some tips:
• Ctrl+ C Cancel
• Enter run command
• Up arrow display history
• Left/right arrows command for editing
Working with directories
• create a directory
• mkdir climate
• mkdir –p /home/alima/Desktop/climate
• move file to directories
• mv aims.txt climate
• Task: copy aims.txt to the folder climate located in the Desktop
• list file within a directory
• ls climate/
• change name of a directory
• mv climate/ climate_lab
Working with directories (2)
• changing directories (navigation)
• cd Desktop
• cd or $ cd ~ or cd $HOME
• cd .. (goes one directory up)
• delete a directory
• rm –r climate_data
• rmdir climate_data
• Some important rules
• avoid spaces when giving file names;
• Dot is not advisable in file naming
• Do not use weird characters (i.e. ) in file naming
Print file contents and search for files
• display a file content
• cat aims.txt
• head aims.txt (display first few lines of the file, 10 by default)
• tail -20 aims.txt (display last 20 lines of the file)
• less aims.txt
• more aims.txt
• find files by name, by type
• find . -name ’’*txt ’’ (. is the current directory)
• find /home/alima/ -name “aims.txt”
• find . –type d
• find . –type f
Display some files without editing
• cat: display a file content
• less: displays a file, allowing forward/backward movement within it
return scrolls forward one line, space one page use “/” to search for a string and
press q to quit
• head : displays the first few lines on top of a file (by default it shows the first
10 lines; -n option allows you to change the default e.g. “head -n50 file.txt” )
• tail: displays the last few lines at the bottom of a file
• less: allow forward and backward movement by using up and down bottom.
Press q to quit when you reach the end of the file
• more: use to scroll down the content of a file by pressing enter or space (it
shows the percentage of the file read and close automatically when it reaches
the end of the file without having to press a button)
Filters: grep, awk, seq
• grep: global regular expression pattern. It searches for words or string in a
file in a directory.
• Print line matching pattern (case sensitive)
• grep AIMS aims.txt
• grep –v AIMS aims.txt
• awk: used also as a filter
• get 3rd element from each line from a file (aims.txt)
• awk '{print $3}’ aims.txt
• get 2nd element from each line from a file, if first equal echo)
• awk '{ if ($1 == “echo”) print $2}’ aims
• seq: used also as a filter
• Print all numbers between 10 and 20
• seq 10 20
Compressing and archiving
• archive a set of files
• tar -rf file.tar file1 file2 file3
• compress a set of files
• zip file.zip file1 file2 file3
• unzip file.zip
Networking
• download a file at an URL
• wget https://download.linux.org/rocky-8.4-x86_64.iso)
Pipes, special characters, quoting
• pipe: chain filter commands
• ls | sort
• ls | sort –r
• seq 1000 9999| grep 55
• ls –l |less
• special characters
# comments
; separate command
“ ” interpret text literally but except some special characters (e.g. ${var})
‘’ preserves all special characters within
\ Escape: quoting next character (e.g. echo \#)
var=42
echo "\${var} = ${var}"
echo '\${var} = ${var}'
Bash: special characters (2)
❑ $( ) Command substitution $ echo "today = $(date)"
old version: ` ` (backticks) today = Fri 02 Jun 2023 07:11:17 PM
CAT
❑ ( ) Group command in subshell a=$ (ls -l; date)
❑ (( )) Double-parentheses construct for arithmetic evaluation
$ echo $((1 + 3))
4
❑ [] part of test builtin or array element (e.g. if [ 2 -gt 0 ] ; then echo yes ; fi)
Bash: Wildcards
* match any multiple characters in a file (directory) name
e.g. list all files starting with output
$ ls output*
e.g. list all files ending with txt
$ ls *txt
e.g. list all files starting with n or p
$ ls [n,p]*
? match any single character
$ ls /bin/?ash
^ to negate the wildcard match
e.g. list all files not starting with a ls [^a]*
Let do some practices
• Task 1: create a directory called computing_lab in your Desktop.
create a file within this directory, name it test.sh and write this text: ‘#!/bin/bash’
to the file ; rename this file to (new.sh) and display the content of this file.
• Task 2: explains this command
ls *pdf | cat | sort –r
• Task 3: explains these commands
cd; cd . ; cd .. ; cd ~ ; ls p*; ls *p* ; ls *.sh
How many arguments do these commands take: ls, cp, mv…
Bash script format
❑Hash-bang = '#!' (= activates interpreter directive)
→ at head of file = 1 line only! #!/bin/bash
❑ Line ends with no special character!
❑But multiple statements in one line to be separated by: ; Semicolon
e.g. echo hello; echo World
Bash variables
var1=AIMS
Variable name Variable content
No space between them “=”
❑ Use brackets to protect your variables
❑ try this…. and tell the difference var2=01_${var}_cmr
var3=01_$var_cmr
❑ Values can be generated by commands: var4= $(date +”%Y%m%d00”)
❑ ${#var} print string length (e.g. $ A='abcdef_abcd'; echo ${#A})
Bash arrays
array1=(CMR LIMBE AIMS)
• array1+= (CLIMATE)
add more variable to array1
• ${array1[0]} first array element
• echo ${#array1[@]} print all the element of array1
• (e.g. $ dt=( $(date) ); echo ${#dt[@]})
• ${array1[@]:p1:p2} print element from p1 to p2
• (e.g. echo ${dt[@]:1:2})
Bash: File permissions
• $ ls –l (associated with the access rights)
• -rwxrw-r—-: code giving the access rights: for this example: r (read only ), w
(write) x (execute) for the owner (alima); read write for the group (AIMS-
staff); and read only for other.
• 700: size of the file;
• 2008-07-05 20:20: the date and time the file was created;
• output.txt : file name
• For the directories: r (list files in directories); w (delete or move files) ; x (read
individual files in the directories).
❑ Bash: File permissions (2)
file
group other
directory user
Bash: File permissions (3)
• Task:
• -rwxrwxrwx : What does this code stands for?
• drw- ------:???
• Symbols meanings: u: user; g: group; o: other; r: read; w: write; x: execute;
+ add permission; - take away permission.
• chmod [options] file (change access rights for named file)
• chmod [user/group/others/all]+[permission] [file]
• chmod go+rwx list (give read, write and execute permissions on the file
called list to group and other)
• Code meaning:
a : all; u: user; g: group; o: other; + add; - remove
Some quick moves
• Use tab completion or see possible list of files in a directory
• Recall commands with up arrow
• Use man command name to know more about any command
• clear
Run a program
• Make sure the program has executable permissions
• Use “./” to run the program (e.g. ./myscript.sh)
• bash myscript.sh
File editors
• Basic editors are gedit, nano/pico, emacs, vim
• nano aims.txt (ctrl x to exit)
• gedit aims.txt
• vim aims.txt (write and quit :wq or :q! to exit; press ESC to go back
to the normal mode)
for Loops
for argument in list
do
command
done
• Iterates command(s) until all arguments of list are passed
example:
#!/bin/bash
## Example : Loop over generated integer sequence
counter=1
for i in {1..10} ; do
echo "loop no. ${counter}: ${i}"
let counter+=1
done
while Loops
while condition in list
do
command
done
• Iterates command(s) as long as condition is true
example:
#!/bin/bash
## Purpose: Loop until max is reached
max=10
i=1
while (( ${max} >= ${i} )) ; do
echo "${i}"
let i+=1
done
Functions
function my_name () • Stores a series of commands for later or
{ repetitive execution
commands • Functions are called by writing the name
}
example
#!/bin/bash
## Purpose: Demonstrating features of functions
## pass today’s date
Function today ()
{
echo " $(date) "
}
today
Exercises
• Write a bash script named exercise1.sh which print years from 1900 to 2000
using a for loops.
• Do the same as in exercise1.sh using a while loops (print the years by step
of 10) and call the script exercise2.sh .
• Write a script that download today’s forecast from Global Forecasting (GFS)
data for by step of 6 hours and save output to your Desktop. Name the script
exercise3.sh (link to the data:
http://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs/)
• Write a bash script (exercise4.sh )which using a function to compute the
surface area of a cube with a face equaled to 3 feet using two different
methods.
Questions?