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 wWindows allows a limited number of operations. The Linux shell allows you to perform advanced scriptingoperations .
All commands can be chained using some special characters (“|” “>” and “<”) as needed.
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 -s m
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 mounted7
Display file contents
syntax:
more filename
cat filename
example:
cat /etc/passwd
A particularly interesting feature is the ability to view variations of a real-time 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 for all the files in /etc including subfolders the string “root”. 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
table 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
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:
sed /test/d file.log
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 ‘2,30d’ file.log
Delete rows from 2 to 30
sed ‘2,$d’ file.log
Delete rows from 2 onwards
sed ‘$d’ file.log
Delete the last line
sed -n ‘/test/p’ file.log
Print only lines with the word test. If you add -i you will change the file
sed ‘/ ^$/d’ install.log
Deletes empty line
sed ‘s /^/text/’ install.log
Add “text” at beginning of every line
sed ‘s/$/text/’
Adds “text” at the end of each line
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