Learn a new Linux command every day :-)

Bash:
grep -rinw /path/to/folder -e 'SUBSTRING' 2>/dev/null
Find all files containing a substring in their contents (**case-insensitive**)
 
Code:
for file in *.heic; do heif-convert -q 100 $file ${file%.HEIC}.jpg; done

Converts iPhone .heic images to .jpg. I use the KDE Connect app to mass sync my iPhone photos to desktop PC. They come in raw .heic format though, and this command mass converts them to .jpg.
 

# Free your unused RAM


First, check your system's RAM status using the following command:
free -mh

Second, you need to log into a root using the following commands:
sudo su - root

Post that, run the following commands:

Code:
sync; echo 1 > /proc/sys/vm/drop_caches
sync; echo 2 > /proc/sys/vm/drop_caches
sync; echo 3 > /proc/sys/vm/drop_caches

Note: run command: "free -mh" again to see the effect
 

# Work with your machine hostname


1.1) get hostname

Code:
hostname

<OR>

Code:
cat /etc/hostname

1.2) set a hostname

Code:
sudo hostnamectl set-hostname "Your-Server-Name"

1.3) add a new host entry to your remote machine

Suppose you have created a web application which is running on localhost port 8080, and the name of your application is "webapp"
You can access your webapp through http ://127.0.0.1:8080/ or http ://localhost:8080/
But if you want to access that web application by its name, then you need to add a host entry of it into your '/etc/host s' (without space) file.

The below command will help you to achieve that goal

Screenshot from 2024-03-12 11-26-03.png


Note: post that you can access the web application via http ://webapp:8080
I am adding screenshots because /etc/host s can't be added into this forum

1.4) check your host's entries

Screenshot from 2024-03-12 11-26-39.png


<OR> if you want to check all entries, just remove: | grep -i 'webapp' part from the above command

# Know your Network interfaces​


2.1) know public & private IP addresses attached to your remote machine

Code:
hostname -I

2.2) know about network interfaces which are attached to your remote machine
Code:
ip a
 
Last edited:

# Ping, DNS, etc​


1.1) Detailed Domain Name Server(DNS) lookup
Code:
dig google.com A

output:
Screenshot from 2024-03-16 11-48-33.png


1.2) shorter version of Name server lookup

Code:
nslookup google.com

output:
Screenshot from 2024-03-16 11-49-47.png


1.3) Checking if an IP address is live and connectable. And how long it is taking to connect
Code:
ping 8.8.8.8 -c 3

Note: here -c => count
output:
Screenshot from 2024-03-16 11-48-53.png
 
Here are some rare and useful Linux commands that you may find helpful:
  1. sudo !!: This command allows you to re-run the previous command with sudo privileges, which is useful if you forgot to use sudo when running a command that requires it.
  2. python -m SimpleHTTPServer: This command creates a simple web page for the current working directory over port 8000, which is useful for quickly sharing files with others.
  3. tac: This command (reverse of cat) prints the content of a file in reverse order, which can be helpful for reading files in reverse or automating processes.
 
Run && some && commands && one && after && another

Pipe output to a file>thefile.txt

sed -i 's/findthis/replacewiththis/g' file.txt
 

# empty a file content


Code:
> test.txt

Note: If file 'test.txt' has some content on it, the above command will erase/truncate it. But the file itself would not be deleted.

Example:
empty_content.png
 
Another Command I found today to truncate a file content

Code:
truncate -s 0 <file_name>

Advantage of this command over the command I shared earlier is we can use the above command with sudo, e.g.,

Code:
sudo truncate -s 0 <file_name>
 

# Get resource(CPU, RAM) usage of a Process ID(PID):


Code:
ps -up <PID>

Note: In case you wonder what a "PID" could look like. So PID is a integer(numeric) number. And on Linux, every application runs with a unique PID.

So, for example, if I need to know what the PID is for my database(MySQL) instance. I can run the command below to get the PID of my MYSQL.

Code:
ps aux | grep mysql | grep -v grep | awk '{print $2}'

In case you want to merge both commands into a single one, check the below command below:

Code:
ps -up $(ps aux | grep mysql | grep -v grep | awk '{print $2}')

Screenshot:
ps-resource-usage.png
 

# Know your server network usage (Bandwidth)



First, you need to know your network interface name. Execute the command below to get it
Code:
ip a | grep -i BROADCAST

Notice: There could be multiple network interfaces. You need to consider the one which has your Server IP along with
For the sake of this example, let's say it is eth0

Code:
nload eth0


---------
<OR> with shortcut in a single command:

Code:
var=$(ip a | grep -i BROADCAST | awk '{print $2}')
nload ${var//:/}

Note: You need to install 'nload' package first. For Ubuntu users, it can be installed with the following command:
sudo apt update --yes && sudo apt install nload --yes

For Centos users, it can be installed with the following command:
sudo apt update --yes && sudo apt install nload --yes


Screenshot:
Screenshot from 2024-04-18 16-34-39.png


Since the above server has a 1Gbps connection port
And as per the given time, it utilizing only 380 Mbps for outgoing traffic & 5 Mbps for incoming traffic
So, I can rest assure, don't need to upgrade the port to a 10Gbps connection.
 
Last edited:

# Know your System/Server CPU usage


First, for Ubuntu users, you need to install the package. To do that, run the below command:
sudo apt update --yes && sudo apt install sysstat --yes

For Centos users, the package should come as by default.


# for every 5 minutes(300 seconds)
Code:
sar 300

Note: this is the average CPU usage of all Threads/Cores present in your system.

Screenshot:

cpu avg usage.png
 

# Benchmark your CPU


Code:
wget http://cdn.geekbench.com/Geekbench-5.2.3-Linux.tar.gz
tar xvf Geekbench-5.2.3-Linux.tar.gz
cd Geekbench-5.2.3-Linux/ && ./geekbench_x86_64

it will upload all the details into Geekbench website and generate a URL with all reports gathered like below:
https://browser.geekbench.com/v5/cpu/22448255

Screenshot:


Screenshot from 2024-05-03 13-03-06.png
 
If you are a reader buy / download a book called Gebraucht | Linux All-In-One For Dummies. You will learn all commands in a week or two. Cheers!
 
Back
Top