Table of Contents

Bash shell

Guide on the usage of the most common commands of the bash shell

Generic Linux Command Syntax

command [-options] [arguments]
command1; command2;...

Help

All linux commands are documented, and their documentation can be accessed using:

The Unix file system

The Unix file system is a hierarchical file system organized in directory:

A file that starts with “.” (dot) is an hidden file.

Files within a directory can be accessed using:

Files Commands

cp [-fir] src1 src2 ... dest
rm [-fir] file1 file2 ...
mv [-fi] file1 file2 ... dest

File commands options are:

Directories Commands

cd dir
pwd
mkdir dir
rmdir dir

A Symbolic link is a file that contains a reference to another file or directory

ln src alias
ln -s src alias

Files and Directories protection

In 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:

Files and Directories protection Commands

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 ...

Examples:

Options:

The ls Command

ls [-options] [files]

ls command example

user@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:

Viewing a textual file

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 ...

With head command

head [-n number] file1 file2 ...}

With more command

more file

With diff command

diff file1 file2

The find command

find <dir> [-opt]: it finds files present in the dir directory and in each subdirectory

Options are:

The grep command

grep [-opt] pattern file1 file2 ...

The grep command prints lines matching a pattern found in file1, file2

Its options are:

pattern can be a regular expression:

The tar command

tar [options] tarfile [file1 file2 ...] [dir1 dir2 ...]

The tar command is used for the compression or decompression of files and directories

Compression:

Example:

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:

Example 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

The sort command

sort [-opt] file1 file2 ...

it sorts file1, file2, …

Options are:

The bash shell

The focus of this document is on bash

Each shell, when opened, search a configuration script in the home directory and run it

Completion and History

Pipe

command1 | command2

A pipe is a connection between the first command stdout and the second command stdin

This linking is performed by the operator |

Examples:

Process Management

Summarizing:

In the following figure the process states and the transitions between them are illustrated:

Scripts - Shell commands in a File

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:

Shell Variables

variableName=value
$variableName
user@pc:~$ echo $b
pippo

Shell Environment Variables

Useful Environment Variables

Example: add to the environment variable PATH the directory /home/user/bin:

export PATH=\$PATH:/home/user/bin

References

  1. Slides form which this page is derived: bash_shell_intro.pdf (Screen version), bash_shell_intro_printable.pdf (Printable version)
  2. Some examples regarding the bash shell scripting language: Bash language
  3. List of resources regarding the Linux operating system: linux operating system