20 Linux CLI Tips


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.

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.

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

and then, from terminal run,

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.

history Command Logs

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.

Explanation of commands and switches.

  • 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.

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.


0x04. Statistics related to a file with the help of stat

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.

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.

Explanation of commands and switches.

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

That’s all for now. If you know any such tips and tricks you may share with us and we will post the same in your words on our reputed Tecmint.com website. If you want to share any tips and
tricks that you cannot make into article you may share it at tecmint[dot]com[at]gmail[dot]com and we will include it in our article. Don’t forget to provide us with your valuable feedback in the
comments below. Keep connected. Like and share us and help us get spread.

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:

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

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:

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:

Is equivalent to:

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:

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

Now use the TAB button to switch between windows – left and right. I have a LibreOffice file that I will move to “Software” folder: To move the file in the new directory press F6 button on your keyboard. MC will now ask you
for confirmation: 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:

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:

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. Simply use:

This should bring you the following output:

0x11. Search for Files

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

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:

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:

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

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:

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:

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

0x13. Copy File into Multiple Directories

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

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

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

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:

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:

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:

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.

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.

Using ls -l to determine block and character files.

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


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.

Counting number of directories

In Linux, directories are represented by the d symbol.

Counting number of symbolic and hard links

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

Counting number of block and character files

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


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.


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.

Using which command to locate commands

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


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.

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

root@php-faq.eu ~/Linux-Tricks $ sudo date –set=“12:27:00″ Wed Sep 9 12:27:00 IST 2015 You can also set time and date as follows.

Viewing current date from a calendar using cal command.

View hardware clock time using the hwclock command.

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

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.

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

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