Guide on the usage of the most common commands of the bash shell
command [-options] [arguments]
\” as last character of the rowcommand1; command2;...
All linux commands are documented, and their documentation can be accessed using:
man <command>: gives an extensive description of commandapropos <command>: search a keyword in the manual page descriptionswhatis <command>: gives a short description of commandcommand -h or command -help: gives a description of command optionsThe Unix file system is a hierarchical file system organized in directory:
/ is the symbol that represents the root of the file system. (dot) is the actual directory.. (dot dot) is the father directory~ is the home directory, for example /home/username
A file that starts with “.” (dot) is an hidden file.
Files within a directory can be accessed using:
/dir1/dir2/filesubdir1/subdir2/filecp [-fir] src1 src2 ... dest
rm [-fir] file1 file2 ...
file1, file2, …-r option is used. In this case the entire directory content will be recursively deletedmv [-fi] file1 file2 ... dest
File commands options are:
-f force, no confirmation is required-i: interactive, confirmation is required for each file-r: recursive, the file command works recursively for each subdirectorycd dir
dir directorycd ..: goes into the father directorycd (alone): goes into the home directorypwd
mkdir dir
dir directoryrmdir dir
dir directorydir must be emptyA Symbolic link is a file that contains a reference to another file or directory
ln src alias
src and aliasln -s src alias
src and aliasIn Linux, every file or directory has special protections that allow differential access to the different users of the system
The system users are grouped into three sets:
Files protections are:
Directories protections are:
Change the group of the files:
chgrp [-R] group file1 file2 ...
Change the owner (and potentially the group) of the files:
chown [-R] user[:group] file1 file2 ...
Change the protection of the files:
chmod [-R] protection file1 file2 ...
r, 2 for w and 1 for x respectively for user, group and othersExamples:
chmod 777 file: user has rwx access to the file, group has rwx access and also others (-rwxrwxrwx)chmod 664 file: user has rw- access to the file, group has rw- access and others has r– access (-rw-rw-r–)chmod 754 file: user has rwx access to the file, group has r-x access and others has r– access (-rwxr-xr–)Options:
-R: recursive, the command works recursively for each subdirectoryls [-options] [files]
-a: all, list also hidden files-l: long, long format output-r: reverse, reverse order of output-t: time, files are sorted by modification time-R: recursive, the command works recursively for each subdirectoryuser@pc:~$ ls -al ~/tmp drwxr-xr-x 3 user user 4096 2009-10-13 15:10 ./ drwxr-xr-x 64 user user 12288 2009-10-13 15:10 ../ drwxr-xr-x 2 user user 4096 2009-10-13 15:10 adir/ -rw-r--r-- 1 user user 29 2009-10-13 15:10 afile lrwxrwxrwx 1 user user 5 2009-10-13 15:24 alink -> afile
This command lists the tmp directory which is in the home directory showing also the hidden files with a long output format:
- standard file, d directory, l link)rwxr-xr-x are the protectionsuser is the owner, the second is the group
Using visual editor like gedit, vim (or its oldest version vi) or emacs
With cat command
cat file1 file2 ...
With tail command
tail [-n number] file1 file2 ...
file1 and file2 (default is 10 rows)+number prints the rows from number to the end of file1 and file2 (ex. tail -n +15)
With head command
head [-n number] file1 file2 ...}
tail, but it prints the first number rows of file1 and file2
With more command
more file
With diff command
diff file1 file2
file1 and file2
find <dir> [-opt]: it finds files present in the dir directory and in each subdirectory
Options are:
-name pattern: search pattern. Pattern can be a filename or a part of filename using the symbol \* to denote the rest of the filepattern:abc: the file abc is searched\*ab: files that end with ab are searchedbc\*: files that begin with bc are searched\*dd\*: files that contain dd are searched-type [b c d l]: b=block file, c=character file, d=directory, l=link-exec command \;: for each file found executes command-exec command \{} \;: for each file found executes command file_found. \{} is the file found.grep [-opt] pattern file1 file2 ...
The grep command prints lines matching a pattern found in file1, file2…
Its options are:
-c: prints the number of matching lines-i: case insensitive-l: prints only names of files containing the pattern-n: prints line number of matched lines-v: prints only lines that non match pattern
pattern can be a regular expression:
.: represents any character^: the begin of the row$: the end of the row*: zero or more repetitions+: one or more repetitionstar [options] tarfile [file1 file2 ...] [dir1 dir2 ...]
The tar command is used for the compression or decompression of files and directories
Compression:
c: creation of a new tar filef: file, must be present both in the compression phase, both in the decompression phasev: verbose modez: zipped, the file is compressedExample:
tar cvzf /tmp/file.tgz /home/user/Documents /home/user/a.txt
it creates a verbose, zipped file. The Documents directory and the file a.txt are compressed in the archive /tmp/file.tgz.
Decompression:
x: extraction of files from an archivet: test the content of an archiveExample 1:
tar tvzf /tmp/file.tgz
it tests a verbose zipped file, i.e., it tests the archive previously compressed
Example 2:
tar xvzf /tmp/file.tgz
it extracts a verbose, zipped file, i.e., it extracts in the current directory the archive previously compressed
sort [-opt] file1 file2 ...
it sorts file1, file2, …
Options are:
-b: ignores blanks spaces at the beginning of a line-f: case insensitive-r: reverses the result-n: numeric sort (for numbers)sh}), C-shell (csh), Tahoe C-shell (tcsh) and Bourne again shell (bash)
The focus of this document is on bash
Each shell, when opened, search a configuration script in the home directory and run it
bash this file is .bashrc (it is located in the home directory and can be edited)$PATH).history command to show the history buffer that lists the last executed commands associating them with a number!n executes a command present in the buffer identified with the number n !$ is the last parameter of the command previously performed!* are all the parameter of the command previously performed!string perform the last command that begin with stringcommand1 | command2
A pipe is a connection between the first command stdout and the second command stdin
This linking is performed by the operator |
Examples:
ls | head -n 5cat file | tail -n 25 that does the same thing of tail -n 25 filecat file | sort -n -u | head -n 5comand1; command2, command1 is executed. When command1 is finished command2 is executed.command1 \&; command2, command1 and command2 are executed concurrentlycommand1 is executed in background (symbol &).command2.Summarizing:
& after the command it can be run background (bg)fg or background using the command bg
In the following figure the process states and the transitions between them are illustrated:
A sequence of commands can be written in a file and they can be executed directly calling it
It can be executed indirectly:
source <sciptfile> <args>
It can be executed directly:
#! and with the absolute path of the shell used (example: \#!/bin/tcsh)./scriptfile in the shell promptvariableName=value
variableName, setting it to valuea=10, b=“pippo”$variableName
echo $variableName can be usedb for example:user@pc:~$ echo $b pippo
env: displays shell environment variablesprintenv var: prints the value of var shell environment variablesexport var: makes the variable var available to all the processes launched with the shellHOME: the home directoryPATH: directory where the shell finds shell commandsSHELL: the used shellLD_LIBRARY_PATH: it shows the directories where shared libraries are located
Example: add to the environment variable PATH the directory /home/user/bin:
export PATH=\$PATH:/home/user/bin