Over the years I have faced several linux challenges that I have solved by googling or thanks to beautiful people who have shared their knowledge.
This is a brief compilation of useful tricks grouped in different categories: Files management, searching, bash scripting, networking, compressed files management, packet management, server information and miscellaneous.
Delete
sed -i '1d' file.txt
sed '/^$/d' file.txt
cat file.txt | sed 's/^..//'
sed '/ *#/d; /^$/d' file.txt
cat input.txt|tr -d "\r" > output.txt
Replace
sed ':a;N;$!ba;s/\n/\'\'\-\'/g''
sed ':a;N;$!ba;s/\n/ /g'
Add
sed -d s/^/\-/ file.txt
sed -d s/$/\-/ file.txt
Filter
cat file.txt |grep ^[0-9]
grep -E 'string1|string2' file.txt
grep -e 'string1' -e 'string2' file.txt
cat file.txt | awk '{print $3,$10}'
cat file.txt| awk -F"-" '{print $3}'
Misc
split -l 2000 input.txt output.txt
sed -n '/string1/p' files.txt
cat file.txt | tr '[:lower:]' '[:upper:] '
chmod --reference=reference_file new_file
chown --reference=reference_file new_file
ls | wc -l
wc -l file.txt
wc -s file.txt
wc -L file.txt
tree -a
find -mtime -2
find -mtime +5
find ./ -type f -daystart -ctime -1
find / -type d -name
directory_name
find ./ -type f -exec grep -H '
content
' {} \;
For syntax
#!/bin/bash
for i in `seq 1 90` do
echo $i
done
Read file line per line
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "Text read from file: $line"
done < "$1"
Read file word per word
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
for word in $line
do
echo $word
done
done < "$1"
Execution: ./read_file_line_per_line.sh file_to_read.txtCheck if a file exists
#!/bin/bash
if [ -f $path/to/file.txt ]; then
echo "exist"
else
echo "no exist"
fi
Check ip reachability
#!/bin/bash
ping -w1 -c3
$IP
>/dev/null 2>&1 && echo "ON" || echo "OFF"
ping -I
interface_name ip
link set
current_name
name
new_name
ip address show
Traffic
tshark -i
interface_name
.151 -f "host
ip
and port
port_number
"
tcpdump -i
interface_name
dst port port
port_number
-w capture.pcap
tcpdump -ni eth0 -w capture.pcap
tcpdump -ttttnnr capture.pcap
Port scanning 👀
nc -zvn
ip 5050
nc -zvn
ip 5050 5055
nc -zvn
ip 5050-5055
nc
ip 5050
< input.in
Listening 👂👂
netcat -l 5050
netcat -ul 5050
netcat -l 5050 > received.txt
netcat -k -l 5050
.tar.gz files
Compress:
tar -czvf compressed.tar.gz /direcory/to/compress/
tar -xzvf compressed.tar.gz
.tar files
Compress:
tar -cvf compressed.tar /direcory/to/compress/
tar -xvf compressed.tar
.gz files
Compress:
gzip file.txt
gzip -d file.txt.gz
.zip files
Compress:
zip compressed.zip /direcory/to/compress/
unzip compressed.zip
Bonus 🎉
Read compressed file without decompress:
zcat compressed.gz
zgrep -n
string
compressed.gz
split -b 10m file.tar.gz "file.tar.gz.part"
yum whatprovides package_name
dnf provides package_name
yum info package_name
dnf info package_name
yum update --security
dnf update --security
yum deplist
httpd
dnf repoquery --deplist httpd
yumdownloader --resolve FTP
dnfdownload --resolve FTP
package-cleanup --orphans
and package-cleanup --oldkernels
dnf list extras
and dnf remove $( dnf repoquery --installonly --latest-limit -1 -q )
yum list-security
dnf updateinfo list --security
grep processor /proc/cpuinfo | wc -l
dmidecode -t processor | grep "Core Count"
dmidecode -t processor | grep "Core Enabled"
free -m | head -2 | tail -1 | awk {'print $4'}
df -h | awk {'print $3'} | head -3 | tail -1
cat /etc/*release*
🍕Misc
sudo -u
user command
echo 3 > /proc/sys/vm/drop_caches
ln -s
/file_or_directory_path /direct_access_path
mkfifo
filename
identify -verbose
image.png
cat file.json | jq
jq '.[] | .my_key' file.json
Hope this was useful for you. If you want more don't hesitate visit my github