How to use linux shell
Subtitle: Basic shell commands
Version / revision: V1.0 / R 0.0.
For Nethserver all version.
Skill: Novice / Beginner
Date of presentation: Released on 2016-05-27
Target
Often in the community using the shell.
I thought to write some commands that might be useful for beginners.
Premise:
The dos prompt in Windows allows a limited number of operations. The Linux shell allows you to perform advanced scripting operations .
All commands can be chained using some special characters (| > and <) as needed.
Commands:
Moving between folders:
Syntax:
cd path
Example:
cd /var/lib cd nethserver
where path is the path to the folder where you want to go. It can be relative or absolute
List files in a folder
Syntax:
ls path
Example:
ls /var/lib ls nethservice
lists files and folders in path specified (relative or absolute).
The command takes several parameters:
ls -la
detailed list of files and folders including hiden files .
Size folders
Syntax:
du path
Example:
du /var/lib
the command takes several parameters.
du -sm
calculates the overall size of a folder rather than in mega bytes.
Check disk space
Example:
df -m
It returns information about the space of partitions mounted.
Display file contents
Syntax:
more filename cat filename
Example:
cat /etc/passwd
A particularly interesting feature is the ability to view real-time variations of a file. This function is used for displaying the log file.
cat /var/log/samba/smb.log | less
Another command to see the last (10) lines of a file
tail filename
Also this command takes several parameters
tail -20 /etc/passwd
Show the last 20 lines
tail /var/log/nomelog | less
Filter content
Syntax:
grep -r "string" files
Search “string” in files.
It accepts several parameters and is useful combined with other commands:
grep -r --color "root" / etc / password
Search in /etc/passwd the string “root” and highlights it in red
grep -r --color "root" /etc> list.txt
Search the string “root” in all files in /etc including subfolders . The output is written on file list.txt
File List
Syntax:
find path
Example:
find /var/lib/nethservice
Lists all files in path and subfolder.
This command is often used in combination with other:
find /var/lib/nethserver/ | grep mail | grep admin> list.txt
Lists files in /var/lib/nethserver/ extracting only those that contain in path and name the string “email” and “admin” and stores them in list.txt
Network
Check presence host
Syntax:
ping host
Example:
ping 192.168.1.10 ping nethserver.domain.lan
Search host
Syntax:
nmap [option]
Example:
nmap -v -sn 172.16.1.0/24
Scanning the 172.16.1.x network and lists the status of each address.
The command accepts a variety of parameters:
nmap -v -A nethservice.domain.lan
Lists all open doors for host nethservice.domain.lan
Text editor
Syntax:
sed [options] file
Example:
sed s/Installing/Removing/ install.log
It replaceswords “Installing” with “Removing” in install.log file.
It's a very powerful editor. Here are some example:
Delete all lines that contain the word test
sed /test/d file.log
Delete all lines that do not contain the word test
sed /!test/d file.log
Delete all lines that begin with the word test
sed /^test/d file.log
Delete rows from 2 to 30
sed '2,30d' file.log
Delete rows from 2 to the end
sed '2,$d' file.log
Delete the last line
sed '$d' file.log
Print only lines with the word test. If you add -i you will change the file
sed -n '/test/p' file.log
Deletes empty line
sed '/^$/d' install.log
Add “text” at beginning of every line
sed 's/^/text/' install.log
Adds “text” at the end of each line
sed 's/$/text/'
You can run multiple instructions separated by -e
sed -e 's/Installing/Removing/' -e 's/$/text/' install.log
or placing them in a file (eg. list.txt)
s/Installing/Removing/ s/$/text/
and pass through the -f switch
sed -f install.log list.txt
Script
Once the sequence of commands you can place them in a script.
The script usually has the extension .sh (script.sh) and must be executable
chmod +x script.sh
Example:
for i in $(ls); do echo item: $i done
Example:
cat /etc/passwd | while read line do echo $line |grep apache| gawk -F: '{print $1"\t My home is:"$7}' done