Linux CLI Cheatsheet

Below are some short snippets of code to help you perform day-to-day tasks in Linux. If you can't find what you are looking for, make sure to look at the BASH cheatsheet.

Get Time Taken For Command To Run

Sometimes it's useful to know how long a command takes. All you have to do is wrap it like so:

CMD="YOUR COMMAND GOES HERE"
utime="$( TIMEFORMAT='%lU';time ( $CMD ) 2>&1 1>/dev/null )"
echo "$utime"

CentOS - Check If Reboot Required

#!/bin/bash
LAST_KERNEL=$(rpm -q --last kernel | perl -pe 's/^kernel-(\S+).*/$1/' | head -1)
CURRENT_KERNEL=$(uname -r)

test $LAST_KERNEL = $CURRENT_KERNEL || echo REBOOT

Output IPs

hostname -I  

If you want each IP on a newline, use the command below:

hostname -I | xargs | tr [:space:] '\n'  

Generate Random Password

This is my favourite option from How-To-Geek: 10 Ways to Generate a Random Password from the Command Line

openssl rand -base64 32  

Add to Cron (crontab)

Often it's necessary for an installation script to install a cron job, such as to run a script on boot. To do this automatically, just use the following code snippet. Make sure to adjust the third line accordingly.

TMP_CRON_FILE="/tmp/cron_file"
crontab -l > $TMP_CRON_FILE
echo "@reboot /bin/bash /path/to/bash/script.sh" >> $TMP_CRON_FILE
crontab $TMP_CRON_FILE
rm $TMP_CRON_FILE

Filesystem

Get Folder Sizes

If you've ever completely filled a system to the point where you can't even install ncdu to track where all your storage is gone, you can get the size of all the folders at your current level by executing:

du -sh *

However, if you can use ncdu instead, that is a much better tool.

Move all Files of a Type To Own Folder

If you have a jumble of files with no apparent structure, perhaps from a backup, and want to move a ceratain file type (in this example PDFs) into their own directory then you can execute the following:

mkdir pdf
find . -name "*.pdf" -type f -exec /bin/mv {} ./pdf \;

Obviously if you just wanted to copy the files instead, use:

mkdir pdf
find . -name "*.pdf" -type f -exec /bin/cp {} ./pdf \;

If you forget to create the directory and it doesn't already exist then you may lose your data.

Using xargs may be better, please refer here.

List All Files Within All Subdirectories

find . -type f

If you wish to also list the directories:

find .

File Manipulation

Replace Text In Files (Sed)

I use variations of the sed command to manipulate config files for automated scripts

SEARCH="search text here"
REPLACE="replace text here"
FILEPATH="/path/to/file.conf"
sed -i "s;$SEARCH;$REPLACE;" $FILEPATH

If you want to replace an entire line, then just use .* as shown below.

SEARCH="bind-address.*"
REPLACE="bind-address = $PRIVATE_IP
FILEPATH="/etc/mysql/my.cnf"
sudo sed -i "s;$SEARCH;$REPLACE;" $FILEPATH

You can use [[:space:]]\+ when you don't know how many whitespaces or tabs there are. E.g.

SEARCH="datadir[[:space:]]\+= /var/lib/mysql"
REPLACE="datadir = /media/data/mysql"
FILEPATH="/etc/mysql/my.cnf"
sudo sed -i "s;$SEARCH;$REPLACE;" $FILEPATH

To replace an entire line that contains text that you are looking for in the middle of the line, you can use the following:

SEARCH="^.*\bYour pattern or sentence goes here\b.*$"
REPLACE="This will replace the entire line"
FILEPATH="$DIR/irapcms.sql"
sed -i "s;$SEARCH;$REPLACE;" $FILEPATH

source

Write To File

echo "my content here" | tee $FILEPATH  
echo "my content here" > $FILEPATH  
cat << EOF > [filepath]
[script content]
EOF

Write To File With Sudo Privs

echo "my content here" | sudo tee $FILEPATH  
cat << EOF  | sudo tee $FILEPATH
[script content]
EOF
cat << EOF > [filepath]
[script content]
EOF

Append To File

echo "my content here" >>  $FILEPATH  

Append To File (With Sudo Privs)

echo "my content here" | sudo tee -a $FILEPATH  
cat << EOF  | sudo tee -a $FILEPATH
[script content]
EOF

Replace Text In Files (Sed)

I use variations of the sed command to manipulate config files for automated scripts

SEARCH="search text here"
REPLACE="replace text here"
FILEPATH="/path/to/file.conf"
sed -i "s;$SEARCH;$REPLACE;" $FILEPATH

If you want to replace an entire line, then just use .* as shown below.

SEARCH="bind-address.*"
REPLACE="bind-address = $PRIVATE_IP
FILEPATH="/etc/mysql/my.cnf"
sudo sed -i "s;$SEARCH;$REPLACE;" $FILEPATH

You can use [[:space:]]\+ when you don't know how many whitespaces or tabs there are. E.g.

SEARCH="datadir[[:space:]]\+= /var/lib/mysql"
REPLACE="datadir = /media/data/mysql"
FILEPATH="/etc/mysql/my.cnf"
sudo sed -i "s;$SEARCH;$REPLACE;" $FILEPATH

Disks

Get Utilization

df -h  

Or you could install pydf for a simpler interface.

Erase Disk

The best way to erase a disk is to use secure erase. For an SSD, this will reach areas that the other commands won't, and return it to peak performance.

Alternatively, you can use shred

shred -n 1 -vz /dev/sdb

... or use scrub

scrub -p dod /dev/sdb

... or you could use dd.

Check Sata Connection of Drives

dmesg | grep -i  ahci | grep -i --color Gbps  

Check the SATA Connection Of Specific Drive

smartctl -a /dev/sda | grep "^SATA"  

You will need to install the smartctl package if you haven't already.

Get Block Size

sudo blockdev --getbsz /dev/sd[x]

The output will be in bytes, so typically this will be 4096 to represent a 4k block size.

Get Ubuntu Release Name

lsb_release -cs

This should output something like:

xenial  

References

Author

Programster

Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus
We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites. More info.