How to use Shell 3 (Script)


Another howto for beginners. This time I focused on helpful commands for scripts.


The first time I used linux shell, i could not do the simplest operations.
A program that has helped me to survive is Midnight Commander (mc).
The appearance reminds Total Commander (in fact comes from the venerable Norton Commander for DOS).

Installation is simple:

yum install mc 

To run, simply type


To use it, scroll to the desired file (space bar for multiselection) and run the operations required by pressing the keys F1 through F10 (F10 exits the program).
It 'very interesting the file editing function (F3 to read, F4 to change).

To edit files you do not need to go through the mc interface. you can start the editor (similar to the edit DOS) with command mcedit

mcedit /etc/aliases

Customizing the shell

The shell allows different customizations to fit our needs.

alias allows you to create keyboard shortcuts

alias lsdir='ls -la |grep '^d''

If I type lsdir performs the dir of folders

ctrlaltdel sets the function of the combination CTRL+ALT+DEL

ctrlaltdel soft

kbdrate Sets the keyboard repeat rate and its delay time

kbdrate -r 30 -d 250

loadkeys load a keyboard layout

loadkeys it

Set environment variables

ls $pathown

If you enter into a script you must make available at the end of the script with command export
The variables will be called up with the $ symbol.

To always have available these settings you can enter the user’s login script ~/.bash_profile or ~/bashrc


[root@nethservice ~]# more .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc

# User specific environment and startup programs


export PATH

alias lsdir='ls -la|grep '^d''

export pathown

Shell Script

Reading information:

When you create a shell script, especially if unattended, it is often necessary to retrieve information of the machine or of his state.
There are some commands to the aid:

hostname: displays and changes the hostname.
groups: Print the name of a user group.

groups root

dirname: Printing only the directory of a file.

dirname /etc/samba/smb.conf

return /etc/samba

pwd: It returns the path of the current location. It can be assigned to a variable.


(Reverse quotes: pressing alt, type 096)

arch: information about the system architecture.


In my case returns “x86_64”

date: returns the date and time. good to create log file with unique name and so on.

Tue Jul 12 15:20:47 CEST 2016
date +%Y%m%d-%H%M%S

last: return the list of last login
logname: return the login name
users: return the list of logged users

###Returning information:
Output to system logs
logger: write on /var/log/messages.

logger -t MYSCRIPT "Run script"

Output to video

echo "Run script"

clear: clean video

Output to file

echo "Run script" > myfile.log
more myfile.log

column: displays files in columns according to a separator (-s)

find /var/log/ > /tmp/list.txt
column -s"/" -t /tmp/list.txt

Output to mail
mail: simple mail program

echo "Mail text" |mail  -s "Mail Object" -a /tmp/list.txt

###Working on files

Create file
touch: change date and time of a file (if it does not exist, create it).

touch -d 2016-06-01 myfile.txt

other file creation methods:
command > myfile.txt

echo "pippo" > myfile.txt
ls > myfile.txt

###Processing text files
cut: splits into columns the lines of a file according to a separator (-d) and returns required columns (-f)

find /var/log/ > /tmp/list.txt
cut -d "/" -f  4,5 /tmp/list.txt |more

splits the lines according to / and returns columns 4 and 5

find /var/log/ > /tmp/list.txt
cut -c1-3 /tmp/list.txt

Returns, for each line, the characters from 1 to 3

colrm: remove columns from a file.

find /var/log/ > /tmp/list.txt
colrm 1 5 < /tmp/list.txt

basename: Remove directory and suffix from file name.

basename /tmp/myfile.txt       

return myfile.txt

basename /tmp/myfile.txt .txt 

return myfile

sort: Sort lines of text. alphabetic, numeric (-n) reverse (-r)

sort -n myfile.txt
ls -1 | sort -r

split: split a file based on the lines or bytes.

split -l 20 myfile.txt

creates n files with 20 lines

nl: numbers the lines of a file.

nl myfile.txt

todos: convert files from Unix format to MSDOS
diff: Check differences between two files.

diff fileA fileB

diff3: Check 3 file.
Other commands (sed, awk, grep) are available on:

###Rights files
chgrp: change the group of a file.

chgrp root.locals /tmp/myfile.txt

chmod: Change rights of a file.

chmod 644 /tmp/myfile.txt
chmod +x /tmp/myfile.txt

chown: change owner of a file or directory.

chown root /tmp/myfile.txt

All commands accept the -R switch to perform the change recursively Caution!!.

cd /tmp
chmod 644 * -R

Get file

wget: download files from web and ftp;


curl: retrieves content from a html page

curl > /tmp/neth.html

In case of compressed files

gzip : compresses and decompresses .gz file.
Max compression (-9) on a new file (-c):

gzip -9 myfile.txt -c > myfile.txt.gz

To unzip a file

gzip -d myfile.txt.gz

bzip2 : compresses and decompresses .bz2 file

bzip2 -zkv filename


bzip2 -x filename.bz2

zdiff: Like diff for compressed files.
zgrep: Like grep for compressed files.
zcat: Like cat for compressed files.

Loop operations


for [ condition ]


for i in 1 2 3
	echo $i

or in one line

for i in 1 2 3; do echo $i; done


while [ condition ]


while [ $x -le 5 ]
  echo "Value x = $x"
  x=$(( $x + 1 ))

or in one line

x=1; while [ $x -le 5 ]; do   echo "Value x = $x";   x=$(( $x + 1 )); done

sleep: freezes for x Seconds (s) Minutes (m) Hours (h) Days (d).

sleep 5s

Planning script

crontab: starts a process at a certain time.
List of cron jobs

crontab -l

Create a new job

crontab -e

the syntax will be as the following

00 00 * * * /Script/

where the fields are
minute hour day_of_the_month month day_of_the_week Script_to_execute

Script Example

This Script remove mail from trash for all users.
For safety, the command that deletes emails is commented

# variable
mytime=`date +%Y%m%d-%H%M%S`

echo -e "\nStart cleaning Script on $myserver by user $myuser\n" > Log-$mytime
logger -t SCRIPT_CLEAN "Start cleaning Script"

for i in `ls -1 $mail_path` ; do
 # write log event
 logger -t SCRIPT_CLEAN "Clean Trash $i"
 # write log file
 echo -e "Clean trash for user $i in the path $mail_path$i/Maildir/.Trash/cur" >> Log-$mytime
 # write on video
 echo -e "Clean trash for user $i in the path $mail_path$i/Maildir/.Trash/cur..."
 echo -e "...OK"
 # Cleaning command (uncomment at your risk)
 # rm -Rf $mail_path$i/Maildir/.Trash/cur/*
done ;
# Send log 
echo "Cleaning work completed" |mail  -s "Script clean trash  $mytime" -a Log-$mytime $recipient

To run the script every Friday at 20, the cron entry (crontab -e )should be:

00 20 * * 5 /script/


1 Like

Tomorrow, from Venezuela, some friends read this publication. :wink: I refer
Very good contribution.
Thank you :thumbsup:

1 Like

Thank you guys :grin: