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
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
kbdrate Sets the keyboard repeat rate and its delay time
kbdrate -r 30 -d 250
loadkeys load a keyboard layout
Set environment variables
pathown="/var/www/html/owncloud" 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 fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH alias lsdir='ls -la|grep '^d'' pathown=/var/www/html/owncloud export pathown
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.
dirname: Printing only the directory of a file.
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.
date Tue Jul 12 15:20:47 CEST 2016 date +%Y%m%d-%H%M%S 20160712-151649
last: return the list of last login
logname: return the login name
users: return the list of logged users
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 email@example.com
###Working on files
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 .txt
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.
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:
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
wget: download files from web and ftp;
curl: retrieves content from a html page
curl http://community.nethserver.org > /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.
for [ condition ] do command1 .... commandX done
for i in 1 2 3 do echo $i done
or in one line
for i in 1 2 3; do echo $i; done
while [ condition ] do command1 .... commandX done
x=1 while [ $x -le 5 ] do echo "Value x = $x" x=$(( $x + 1 )) done
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).
crontab: starts a process at a certain time.
List of cron jobs
Create a new job
the syntax will be as the following
00 00 * * * /Script/myscript.sh
where the fields are
minute hour day_of_the_month month day_of_the_week Script_to_execute
This Script remove mail from trash for all users.
For safety, the command that deletes emails is commented
# variable mail_path="/var/lib/nethserver/vmail/" mytime=`date +%Y%m%d-%H%M%S` recipient="firstname.lastname@example.org" 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/myscript.sh