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.
📝📁Files management
Delete
- Delete first line:
sed -i '1d' file.txt
- Delete all empty lines:
sed '/^$/d' file.txt
- Delete first two characters from each line:
cat file.txt | sed 's/^..//'
- Delete comments and empty lines:
sed '/ *#/d; /^$/d' file.txt
- Delete carriage return \r:
cat input.txt|tr -d "\r" > output.txt
Replace
- Replace string1 with string2 sed -d 's/string1/string2/g' file.txt
- Replace space by - :
sed ':a;N;$!ba;s/\n/\'\'\-\'/g''
- Replace replace line break with space:
sed ':a;N;$!ba;s/\n/ /g'
Add
- Add - at the beginning of the line
sed -d s/^/\-/ file.txt
- Add - at the end of the line
sed -d s/$/\-/ file.txt
Filter
- Filter lines starting with a number:
cat file.txt |grep ^[0-9]
- Filter multiple words:
grep -E 'string1|string2' file.txt
- Filter multiple words old shells:
grep -e 'string1' -e 'string2' file.txt
- Print the third and tenth words from file:
cat file.txt | awk '{print $3,$10}'
- Print the third word from file separated by - :
cat file.txt| awk -F"-" '{print $3}'
Misc
- Split a large file every 2000 lines:
split -l 2000 input.txt output.txt
- View only the lines that contain string1:
sed -n '/string1/p' files.txt
- Convert lowercase to uppercase:
cat file.txt | tr '[:lower:]' '[:upper:] '
- Copy permissions from a reference file:
chmod --reference=reference_file new_file
- Copy ownership from a reference file:
chown --reference=reference_file new_file
🧮Let's count
- Files on the directory:
ls | wc -l
- Lines in a file:
wc -l file.txt
- Words in a file:
wc -s file.txt
- Length of the longest line in a file:
wc -L file.txt
- Number of files and directories in a directory:
tree -a
🔍Searching
- Files less than two days old
find -mtime -2
- Files older than 5 days:
find -mtime +5
- Files created today:
find ./ -type f -daystart -ctime -1
- Directory by name:
find / -type d -name
directory_name
- Find files by content:
find ./ -type f -exec grep -H '
content
' {} \;
👨💻👩💻Bash scripting basics
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"
Execution: ./read_file_line_per_line.sh file_to_read.txt
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"
🌐Networking
- Ping from a defined interface:
ping -I
interface_name ip
- Rename interface:
link set
current_name
name
new_name
- Show IP addresses without infconfig :
ip address show
Traffic
- Traffic capture with tshark:
tshark -i
interface_name
.151 -f "host
ip
and port
port_number
"
- Traffic capture with tcpdump:
tcpdump -i
interface_name
dst port port
port_number
-w capture.pcap
- Store traffic output:
tcpdump -ni eth0 -w capture.pcap
- Read traffic capture form the CLI:
tcpdump -ttttnnr capture.pcap
Port scanning 👀
- Check if 5050 port is open:
nc -zvn
ip 5050
- Check if 5050 and 5055 are open:
nc -zvn
ip 5050 5055
- Check if range of ports:
nc -zvn
ip 5050-5055
- Connect to ip on port 5050 and send content of input.in:
nc
ip 5050
< input.in
Listening 👂👂
- Listen for TCP connections on port 5050:
netcat -l 5050
- Listen for UDP connections on port 5050:
netcat -ul 5050
- Listen for TCP connections on port 5050 and store data received in to "received.txt":
netcat -l 5050 > received.txt
- Listen for TCP connections on port 5050 and continue listening after client has disconnected:
netcat -k -l 5050
🗃️Compressed files management
.tar.gz files
Compress:
tar -czvf compressed.tar.gz /direcory/to/compress/
Decompress:
tar -xzvf compressed.tar.gz
.tar files
Compress:
tar -cvf compressed.tar /direcory/to/compress/
Decompress:
tar -xvf compressed.tar
.gz files
Compress:
gzip file.txt
Decompress:
gzip -d file.txt.gz
.zip files
Compress:
zip compressed.zip /direcory/to/compress/
Decompress:
unzip compressed.zip
Bonus 🎉
Read compressed file without decompress:
zcat compressed.gz
Filter compressed file without decompress:
zgrep -n
string
compressed.gz
Split compressed file in parts of 10m size:
split -b 10m file.tar.gz "file.tar.gz.part"
📦Packet management (YUM and DNF)
- What provides a package
yum whatprovides package_name
dnf provides package_name - Package description:
yum info package_name
dnf info package_name - Update security packages:
yum update --security
dnf update --security - List dependencies from a package example httpd:
yum deplist
httpd
dnf repoquery --deplist httpd
- Download all packages and dependencies needed to install something, example FTP:
yumdownloader --resolve FTP
dnfdownload --resolve FTP - Remove orphan packages:
andpackage-cleanup --orphans
andpackage-cleanup --oldkernels
dnf list extrasdnf remove $( dnf repoquery --installonly --latest-limit -1 -q )
- List pending security updates:
yum list-security
dnf updateinfo list --security
ℹ️Server info
- Number of processors:
grep processor /proc/cpuinfo | wc -l
- Number of cores:
dmidecode -t processor | grep "Core Count"
- Number of enabled cores:
dmidecode -t processor | grep "Core Enabled"
- RAM memory:
free -m | head -2 | tail -1 | awk {'print $4'}
- Free space:
df -h | awk {'print $3'} | head -3 | tail -1
- Release version:
cat /etc/*release*
🍕Misc
- Execute a command as another user:
sudo -u
user command
- Clean cache:
echo 3 > /proc/sys/vm/drop_caches
- Create symbolic link:
ln -s
/file_or_directory_path /direct_access_path
- Create FIFO files:
mkfifo
filename
- Know more information about and image from CLI:
identify -verbose
image.png
- Pretty print json files in CLI:
cat file.json | jq
- Print only values of the key my_key from json file:
jq '.[] | .my_key' file.json
Hope this was useful for you. If you want more don't hesitate visit my github