20 Linux CLI Tips

24.04.2021

You making most out of the Linux? There are lots of helpful features which appears to be Tips and Tricks for many of Linux Users. Sometimes Tips and Tricks become the need. It helps you get productive with the same set of commands yet with enhanced functionality. Here we are starting a new series, where we will be writing some tips and tricks and will try to yield as more as we can in small time.

0x01. history

To audit the commands we’d run in past, we use history command. Here is a sample output of history command.

# history

Obvious from output, the history command do not output the time stamp with the log of last executed commands. Any solution for this? Yeah! Run the below command.

HISTTIMEFORMAT="%d/%m/%y %T "  # history

If you want to permanently append this change, add the below line to ~/.bashrc.

export HISTTIMEFORMAT="%d/%m/%y %T "

and then, from terminal run,

source ~/.bashrc

Explanation of commands and switches.

  • history – GNU History Library
  • HISTIMEFORMAT – Environmental Variable
  • %d – Day
  • %m – Month
  • %y – Year
  • %T – Time Stamp
  • source – in short send the contents of file to shell
  • .bashrc – is a shell script that BASH runs whenever it is started interactively.

0x02. How to check disk write

The next gem in the list is – how to check disk write speed? Well one liner dd command script serves the purpose.

dd if=/dev/zero of=/tmp/output.img bs=8k count=256k conv=fdatasync; rm -rf /tmp/output.img

dd – Convert and Copy a file

  • if=/dev/zero – Read the file and not stdin
  • of=/tmp/output.img – Write to file and not stdout
  • bs – Read and Write maximum upto M bytes, at one time
  • count – Copy N input block
  • conv – Convert the file as per comma separated symbol list.
  • rm – Removes files and folder
  • -rf – (-r) removes directories and contents recursively and (-f) Force the removal without prompt.

 

0x03. Top six files that are eating out your space

How will you check the top six files that are eating out your space? A simple one liner script made from du command, which is primarily used as file space usages.

du -hsx * | sort -rh | head -6

Explanation of commands and switches.

  • du – Estimate file space usages
  • -hsx – (-h) Human Readable Format, (-s) Summaries Output, (-x) One File Format, skip directories on other file format.
  • sort – Sort text file lines
  • -rh – (-r) Reverse the result of comparison, (-h) for compare human readable format.
  • head – output first n lines of file.

 

The next step involves statistics in terminal of a file of every kind. We can output the statistics related to a file with the help of stat (output file/fileSystem status) command.

stat filename.ext  (viz., stat abc.pdf)

0x05. The below one liner will generate random man pages

The next and last but not the least, this one line script is for those, who are newbies. If you are an experienced user you probably don’t need it, unless you want some fun out of it. Well newbies are Linux-command-line phobic and the below one liner will generate random man pages. The benefit is as a newbie you always get something to learn and never get bored.

man $(ls /bin | shuf | head -1)

  • man – Linux Man pages
  • ls – Linux Listing Commands
  • /bin – System Binary file Location
  • shuf – Generate Random Permutation
  • head – Output first n line of file.

0x06. Find the right command

Executing the right command can be vital for your system. However in Linux there are so many different command lines that they are often hard to remember. So how do you search for the right command you need? The answer is apropos. All you need to run is:

apropos <description>

Where you should change the “description” with the actual description of the command you are looking for. Here is a good example:

apropos "list directory"
	dir (1) - list directory contents 
	ls (1) - list directory contents 
	ntfsls (8) - list directory contents on an NTFS filesystem 
	vdir (1) - list directory contents

0x07. Execute Previous Command

Many times you will need to execute the same command over and over again. While you can repeatedly press the Up key on your keyboard, you can use the history command instead. This command will list all commands you entered since you launched the terminal:

history
    1  fdisk -l
    2  apt-get install gnome-paint
    3  hostname lxu.io
    4  hostnamectl lxu.io
    5  man hostnamectl
    6  hostnamectl --set-hostname lxu.io
    7  hostnamectl -set-hostname lxu.io
    8  hostnamectl set-hostname lxu.io
    9  mount -t "ntfs" -o
   10  fdisk -l
   11  mount -t ntfs-3g /dev/sda5 /mnt
   12  mount -t rw ntfs-3g /dev/sda5 /mnt
   13  mount -t -rw ntfs-3g /dev/sda5 /mnt
   14  mount -t ntfs-3g /dev/sda5 /mnt
   15  mount man
   16  man mount
   17  mount -t -o ntfs-3g /dev/sda5 /mnt
   18  mount -o ntfs-3g /dev/sda5 /mnt
   19  mount -ro ntfs-3g /dev/sda5 /mnt
   20  cd /mnt
   ...

As you will see from the output above, you will receive a list of all commands that you have ran. On each line you have number indicating the row in which you have entered the command. You can recall that command by using:

!#

Where # should be changed with the actual number of the command. For better understanding, see the below example:

!501

Is equivalent to:

history

0x08. Use midnight Commander

If you are not used to using commands such cd, cp, mv, rm than you can use the midnight command. It is an easy to use visual shell in which you can also use mouse:

Thanks to the F1 – F12 keys, you can easy perform different tasks. Simply check the legend at the bottom. To select a file or folder click the “Insert” button. In short the midnight command is called “mc“. To install mc on your system simply run:

apt-get install mc        [On Debian based systems]
yum install mc                 [On Fedora based systems]

Here is a simple example of using midnight commander. Open mc by simply typing:

mc

Now use the TAB button to switch between windows – left and right. I have a LibreOffice file that I will move to “Software” folder:

Once confirmed, the file will be moved in the new destination directory.

0x09. Shutdown Computer at Specific Time

Sometimes you will need to shutdown your computer some hours after your work hours have ended. You can configure your computer to shut down at specific time by using:

sudo shutdown 21:00

This will tell your computer to shut down at the specific time you have provided. You can also tell the system to shutdown after specific amount of minutes:

sudo shutdown +15

That way the system will shut down in 15 minutes.

0x10. Show Information about Known Users

You can use a simple command to list your Linux system users and some basic information about them. This should bring you the following output:

lslogins

UID USER PWD-LOCK PWD-DENY LAST-LOGIN GECOS
0 root 0 0 Apr29/11:35 root
1 bin 0 1 bin
2 daemon 0 1 daemon
3 adm 0 1 adm
4 lp 0 1 lp
5 sync 0 1 sync
6 shutdown 0 1 Jul19/10:04 shutdown
7 halt 0 1 halt
8 mail 0 1 mail
10 uucp 0 1 uucp
11 operator 0 1 operator
12 games 0 1 games
13 gopher 0 1 gopher
14 ftp 0 1 FTP User
23 squid 0 1
25 named 0 1 Named
27 mysql 0 1 MySQL Server
47 mailnull 0 1
48 apache 0 1 Apache
...

0x11. Search for Files

Searching for files can sometimes be not as easy as you think. A good example for searching for files is:

find /home/user -type f

This command will search for all files located in /home/user. The find command is extremely powerful one and you can pass more options to it to make your search even more detailed. If you want to search for files larger than given size, you can use:

find . -type f -size 10M

The above command will search from current directory for all files that are larger than 10 MB. Make sure not to run the command from the root directory of your Linux system as this may cause high I/O on your machine. One of the most frequently used combinations that I use find with is “exec” option, which basically allows you to run some actions on the results of the find command. For example, lets say that we want to find all files in a directory and change their permissions. This can be easily done with:

find /home/user/files/ -type f -exec chmod 644 {} \\;

The above command will search for all files in the specified directory recursively and will executed chmod command on the found files. I am sure you will find many more uses on this command in future.

0x12. Build Directory Trees with one Command

You probably know that you can create new directories by using the mkdir command. So if you want to create a new folder you will run something like this:

mkdir new_folder

But what, if you want to create 5 subfolders within that folder? Running mkdir 5 times in a row is not a good solution. Instead you can use -p option like that:

mkdir -p newfolder/{subfolder1,subfolder2,subfolder3,subfolder4,subfolder5}

In the end you should have 5 folders located in new_folder:

ls newfolder/
subfolder1 subfolder2 subfolder3 subfolder4 subfolder5

0x13. Copy File into Multiple Directories

File copying is usually performed with the cp command. Copying a file usually looks like this:

cp /path-to-file/myfile.txt /path-to-new-directory/

Now imagine that you need to copy that file in multiple directories:

cp /home/user/myfile.txt /home/user/1
cp /home/user/myfile.txt /home/user/2
cp /home/user/myfile.txt /home/user/3

This is a bit absurd. Instead you can solve the problem with a simple one line command:

echo /home/user/1/ /home/user/2/ /home/user/3/ | xargs -n 1  cp /home/user/myfile.txt

0x14. Deleting Larger Files

Sometimes files can grow extremely large. I have seen cases where a single log file went over 250 GB large due to poor administrating skills. Removing the file with rm utility might not be sufficient in such cases due to the fact that there is extremely large amount of data that needs to be removed. The operation will be a “heavy” one and should be avoided. Instead, you can go with a really simple solution:

> /path-to-file/huge-bigfile.log

Where of course you will need to change the path and the file names with the exact ones to match your case. The above command will simply write an empty output to the file. In more simpler words it will empty the file without causing high I/O on your system.

0x15. Run Same Command on Multiple Linux Servers

Recently one of our readers asked in our Linux forum, how to execute single command to multiple Linux boxes at once using SSH. He had his machines IP addresses looking like this:

10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 10.0.0.5

So here is a simple solution of this issue. Collect the IP addresses of the servers in a one file called list.txt one under other just as shown above. Then you can run:

for in $i(cat list.txt); do ssh user@$i 'bash command'; done

In the above example you will need to change “user” with the actual user with which you will be logging and “bash command” with the actual bash command you wish to execute. The method is better working when you are using passwordless authentication with SSH key to your machines as that way you will not need to enter the password for your user over and over again. Note that you may need to pass some additional parameters to the SSH command depending on your Linux boxes setup.

File Types in Linux

In Linux, everything is considered as a file, your devices, directories and regular files are all considered as files. There are different types of files in a Linux system:

  • Regular files which may include commands, documents, music files, movies, images, archives and so on.
  • Device files: which are used by the system to access your hardware components.

There are two types of device files block files that represent storage devices such as harddisks, they read data in blocks and character files read data in a character by character manner.

  • Hardlinks and softlinks: they are used to access files from any where on a Linux filesystem.
  • Named pipes and sockets: allow different processes to communicate with each other.

 

0x16. Determining the type of a file using ‘file’ command

You can determine the type of a file by using the file command as follows. The screenshot below shows different examples of using the file command to determine the types of different files.

[email protected] ~/Linux-Tricks $ dir  BACKUP
    master.zip  arch-linux-stable.tar.gz       num.txt  minimal.mp3
    reggea.xspf  Linux-Security-Book.gif  tmp-link

[email protected] ~/Linux-Tricks $ file BACKUP/
 	BACKUP/: directory

[email protected] ~/Linux-Tricks $ file master.zip
 	master.zip: Zip archive data, at least v1.0 to extract 

[email protected] ~/Linux-Tricks $ file crossroads-stable.tar.gz
 	arch-linux-stable.tar.gz: gzip compressed data, from Unix, last modified: Tue Apr 5 15:15:20 2021 

[email protected] ~/Linux-Tricks $ file Linux-Security-Book.gif
 	Linux-Security-Optimization-Book.gif: GIF image data, version 99a, 200 x 350 

[email protected] ~/Linux-Tricks $ file minimal.mp3
 	minimal.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo 

[email protected] ~/Linux-Tricks $ file /dev/sda1
 	/dev/sda1: block special 

[email protected] ~/Linux-Tricks $ file /dev/tty1
 	/dev/tty1: character special 

0x17. Determining the file type using ‘ls’ and ‘dir’ commands

Another way of determining the type of a file is by performing a long listing using the ls and dir commands. Using ls -l to determine the type of a file. When you view the file permissions, the first character shows the file type and the other charcters show the file permissions.

[email protected] ~/Linux-Tricks $ ls -l
    total 6908  drwxr-xr-x 2 php-faq php-faq    4096 Sep  9 11:46 BACKUP
    -rw-r--r-- 1 root root 1073610 Feb  4 11:47 arch-linux-stable.tar.gz
    -rwxr----- 1 root root 5926785 Sep  9 11:49 minimal.mp3
    -rw-r--r-- 1 root root   42122 Apr  3 11:49 Linux-Security-Book.gif
    -rw-r--r-- 1 root root   17627 Sep  9 11:46 master.zip
    -rw-r--r-- 1 root root       5 Sep  8 11:48 num.txt
    -rw-r--r-- 1 root root       0 Jan  9 11:46 reggea.xspf
    -rw-r--r-- 1 root root       5 Sep  3 11:47 tmp-link  

Using ls -l to determine block and character files.

[email protected] ~/Linux-Tricks $ ls -l /dev/sda1
    brw-rw---- 1 root disk 8, 1 Jan  4 00:33 /dev/sda1

[email protected] ~/Linux-Tricks $ ls -l /dev/tty1
     crw-rw---- 1 root tty 4, 1 Dez 7 00:57 /dev/tty1 

Using dir -l to determine the type of a file.

[email protected] ~/Linux-Tricks $ dir -l
total 6908  drwxr-xr-x 2 root root    4096 Sep  7 01:46 BACKUP
    -rw-r--r-- 1 root root 1075620 Jul  4 11:47 arch-linux--stable.tar.gz
    -rwxr----- 1 root root 5916085 Sep  3 11:49 minimal.mp3
    -rw-r--r-- 1 root root   42122 Feb  9 11:49 Linux-Security-Book.gif
    -rw-r--r-- 1 root root   17627 Sep  1 11:46 master.zip
    -rw-r--r-- 1 root root       5 Sep  3 11:48 num.txt
    -rw-r--r-- 1 root root       0 Dez  9 11:46 reggea.xspf
    -rw-r--r-- 1 root root       5 Jan  9 11:47 tmp-link

 

0x18. Counting number of files of a specific type

Next we shall look at tips on counting number of files of a specific type in a given directory using the ls, grep and wc commands. Communication between the commands is achieved through named piping.

  • grep – command to search according to a given pattern or regular expression.
  • wc – command to count lines, words and characters.

Counting number of regular files

In Linux, regular files are represented by the – symbol.

[email protected] ~/Linux-Tricks $ ls -l | grep ^- | wc -l  7

Counting number of directories

In Linux, directories are represented by the d symbol.

[email protected] ~/Linux-Tricks $ ls -l | grep ^d | wc -l  1

In Linux, symblic and hard links are represented by the l symbol.

[email protected] ~/Linux-Tricks $ ls -l | grep ^l | wc -l  0  

Counting number of block and character files

In Linux, block and character files are represented by the b and c symbols respectively.

[email protected] ~/Linux-Tricks $ ls -l /dev | grep ^b | wc -l  37
[email protected] ~/Linux-Tricks $ ls -l /dev | grep ^c | wc -l  159

 

0x19. Finding files on a Linux system

Next we shall look at some commands one can use to find files on a Linux system, these include the locate, find, whatis and which commands.

Using the locate command to find files

In the output below, I am trying to locate the Samba server configuration for my system.

[email protected] ~/Linux-Tricks $ locate samba.conf
	/usr/lib/tmpfiles.d/samba.conf
	/var/lib/dpkg/info/samba.conffiles

 

Using the whatis command to locate commands

The whatis command is mostly used to locate commands and it is special because it gives information about a command, it also finds configurations files and manual entries for a command.

[email protected] ~/Linux-Tricks $ whatis bash
	bash (1)             - GNU Bourne-Again SHell

[email protected] ~/Linux-Tricks $ whatis find
 	find (1) - search for files in a directory hierarchy

[email protected] ~/Linux-Tricks $ whatis ls
 	ls (1) - list directory contents

Using which command to locate commands

The which command is used to locate commands on the filesystem.

[email protected] ~/Linux-Tricks $ which mkdir
	/bin/mkdir

[email protected] ~/Linux-Tricks $ which bash
 	/bin/bash

[email protected] ~/Linux-Tricks $ which find
 	/usr/bin/find

[email protected] ~/Linux-Tricks $ $ which ls
 	/bin/ls

 

0x20. Working with time on your Linux system

When working in a networked environment, it is a good practice to keep the correct time on your Linux system. There are certain services on Linux systems that require correct time to work efficiently on a network. We shall look at commands you can use to manage time on your machine. In Linux, time is managed in two ways: system time and hardware time. The system time is managed by a system clock and the hardware time is managed by a hardware clock. To view your system time, date and timezone, use the date command as follows.

[email protected] ~/Linux-Tricks $ date
	Sun Oct 19 14:25:00 IST 2021

Set your system time using date -s or date –set=”STRING” as follows.

[email protected] ~/Linux-Tricks $ sudo date -s "14:25:00"
	Mon Nov  7 14:25:00 IST 2021

[email protected] ~/Linux-Tricks $ sudo date --set="12:27:00" 
	Mon Nov  7 14:25:00 IST 2021 

You can also set time and date as follows.

[email protected] ~/Linux-Tricks $ sudo date 080912302021
	Wed Sep  8 14:25:00 IST 2021

Viewing current date from a calendar using cal command.

[email protected] ~/Linux-Tricks $ cal
    September 2021
    Su Mo Tu We Th Fr Sa
    1  2  3  4  5
    6  7  8  9 10 11 12
    13 14 15 16 17 18 19
    20 21 22 23 24 25 26
    27 28 29 30

View hardware clock time using the hwclock command.

[email protected] ~/Linux-Tricks $ sudo hwclock
	Wednesday 10 September 2021 06:02:58 PM IST  -0.200081 seconds

To set the hardware clock time, use hwclock –set –date=”STRING” as follows.

[email protected] ~/Linux-Tricks $ sudo hwclock --set --date="10/09/2021 12:30:00"

[email protected] ~/Linux-Tricks $ sudo hwclock
 	Wednesday 10 September 2021 12:30:11 PM IST -0.891163 seconds

The system time is set by the hardware clock during booting and when the system is shutting down, the hardware time is reset to the system time. Therefore when you view system time and hardware time, they are the same unless when you change the system time. Your hardware time may be incorrect when the CMOS battery is weak. You can also set your system time using time from the hardware clock as follows.

sudo hwclock --hctosys

It is also possible to set hardware clock time using the system clock time as follows.

sudo hwclock --systohc

To view how long your Linux system has been running, use the uptime command.

[email protected] ~/Linux-Tricks $ uptime
	12:30:27 up  2:40,  2 users,  load average: 1.39, 1.34, 1.45

[email protected] ~/Linux-Tricks $ uptime -p
 	up 2 hour, 40 minutes

[email protected] ~/Linux-Tricks $ uptime -s
 	2021-10-09 11:50:00
comments powered by Disqus