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.
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 Command Logs
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
Explanation of commands and switches.
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.
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)

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)
Explanation of commands and switches.
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.
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
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
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 tecmint.com 4 hostnamectl tecmint.com 5 man hostnamectl 6 hostnamectl –set-hostname tecmint.com 7 hostnamectl -set-hostname tecmint.com 8 hostnamectl set-hostname tecmint.com 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
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:
$ sudo apt-get install mc [On Debian 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:
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.
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.
You can use a simple command to list your Linux system users and some basic information about them. Simply use:
# lslogins
This should bring you the following output:
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 …
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.
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 new_folder/{folder_1,folder_2,folder_3,folder_4,folder_5}
In the end you should have 5 folders located in new_folder:
# ls new_folder/ folder_1 folder_2 folder_3 folder_4 folder_5
File copying is usually performed with the cp command. Copying a file usually looks like this:
# cp /path-to-file/my_file.txt /path-to-new-directory/
Now imagine that you need to copy that file in multiple directories:
# cp /home/user/my_file.txt /home/user/1
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/my_file.txt
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_file.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.
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.
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:
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.
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.
root@lxu.io ~/Linux-Tricks $ dir BACKUP master.zip crossroads-stable.tar.gz num.txt EDWARD-MAYA-2011-2012-NEW-REMIX.mp3 reggea.xspf Linux-Security-Optimization-Book.gif tmp-link
root@lxu.io ~/Linux-Tricks $ file BACKUP/ BACKUP/: directory
root@lxu.io ~/Linux-Tricks $ file master.zip master.zip: Zip archive data, at least v1.0 to extract
root@lxu.io ~/Linux-Tricks $ file crossroads-stable.tar.gz crossroads-stable.tar.gz: gzip compressed data, from Unix, last modified: Tue Apr 5 15:15:20 2011
root@lxu.io ~/Linux-Tricks $ file Linux-Security-Optimization-Book.gif Linux-Security-Optimization-Book.gif: GIF image data, version 89a, 200 x 259
root@lxu.io ~/Linux-Tricks $ file EDWARD-MAYA-2011-2012-NEW-REMIX.mp3 EDWARD-MAYA-2011-2012-NEW-REMIX.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo
root@lxu.io ~/Linux-Tricks $ file /dev/sda1 /dev/sda1: block special
root@lxu.io ~/Linux-Tricks $ file /dev/tty1 /dev/tty1: character special
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.
root@lxu.io ~/Linux-Tricks $ ls -l total 6908 drwxr-xr-x 2 php-faq php-faq 4096 Sep 9 11:46 BACKUP -rw-r–r– 1 tecmint tecmint 1075620 Sep 9 11:47 crossroads-stable.tar.gz -rwxr—– 1 tecmint tecmint 5916085 Sep 9 11:49 EDWARD-MAYA-2011-2012-NEW-REMIX.mp3 -rw-r–r– 1 tecmint tecmint 42122 Sep 9 11:49 Linux-Security-Optimization-Book.gif -rw-r–r– 1 tecmint tecmint 17627 Sep 9 11:46 master.zip -rw-r–r– 1 tecmint tecmint 5 Sep 9 11:48 num.txt -rw-r–r– 1 tecmint tecmint 0 Sep 9 11:46 reggea.xspf -rw-r–r– 1 tecmint tecmint 5 Sep 9 11:47 tmp-link
Using ls -l to determine block and character files.
root@lxu.io ~/Linux-Tricks $ ls -l /dev/sda1 brw-rw—- 1 root disk 8, 1 Sep 9 10:53 /dev/sda1
root@lxu.io ~/Linux-Tricks $ ls -l /dev/tty1 crw-rw—- 1 root tty 4, 1 Sep 9 10:54 /dev/tty1
Using dir -l to determine the type of a file.
root@lxu.io ~/Linux-Tricks $ dir -l total 6908 drwxr-xr-x 2 tecmint tecmint 4096 Sep 9 11:46 BACKUP -rw-r–r– 1 tecmint tecmint 1075620 Sep 9 11:47 crossroads-stable.tar.gz -rwxr—– 1 tecmint tecmint 5916085 Sep 9 11:49 EDWARD-MAYA-2011-2012-NEW-REMIX.mp3 -rw-r–r– 1 tecmint tecmint 42122 Sep 9 11:49 Linux-Security-Optimization-Book.gif -rw-r–r– 1 tecmint tecmint 17627 Sep 9 11:46 master.zip -rw-r–r– 1 tecmint tecmint 5 Sep 9 11:48 num.txt -rw-r–r– 1 tecmint tecmint 0 Sep 9 11:46 reggea.xspf -rw-r–r– 1 tecmint tecmint 5 Sep 9 11:47 tmp-link
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.
In Linux, regular files are represented by the – symbol.
root@lxu.io ~/Linux-Tricks $ ls -l | grep ^- | wc -l 7
In Linux, directories are represented by the d symbol.
root@lxu.io ~/Linux-Tricks $ ls -l | grep ^d | wc -l 1
In Linux, symblic and hard links are represented by the l symbol.
root@lxu.io ~/Linux-Tricks $ ls -l | grep ^l | wc -l 0
In Linux, block and character files are represented by the b and c symbols respectively.
root@lxu.io ~/Linux-Tricks $ ls -l /dev | grep ^b | wc -l 37 root@lxu.io ~/Linux-Tricks $ ls -l /dev | grep ^c | wc -l 159
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.
In the output below, I am trying to locate the Samba server configuration for my system.
root@lxu.io ~/Linux-Tricks $ locate samba.conf /usr/lib/tmpfiles.d/samba.conf /var/lib/dpkg/info/samba.conffiles
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.
root@lxu.io ~/Linux-Tricks $ whatis bash bash (1) - GNU Bourne-Again SHell
root@lxu.io ~/Linux-Tricks $ whatis find find (1) - search for files in a directory hierarchy
root@lxu.io ~/Linux-Tricks $ whatis ls ls (1) - list directory contents
The which command is used to locate commands on the filesystem.
root@lxu.io ~/Linux-Tricks $ which mkdir /bin/mkdir
root@lxu.io ~/Linux-Tricks $ which bash /bin/bash
root@lxu.io ~/Linux-Tricks $ which find /usr/bin/find
root@lxu.io ~/Linux-Tricks $ $ which ls /bin/ls
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.
root@lxu.io ~/Linux-Tricks $ date Wed Sep 9 12:25:40 IST 2015
Set your system time using date -s or date –set=”STRING” as follows.
root@lxu.io ~/Linux-Tricks $ sudo date -s “12:27:00” Wed Sep 9 12:27:00 IST 2015
root@lxu.io ~/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.
root@lxu.io ~/Linux-Tricks $ sudo date 090912302015 Wed Sep 9 12:30:00 IST 2015
Viewing current date from a calendar using cal command.
root@lxu.io ~/Linux-Tricks $ cal September 2015 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.
root@lxu.io ~/Linux-Tricks $ sudo hwclock Wednesday 09 September 2015 06:02:58 PM IST -0.200081 seconds
To set the hardware clock time, use hwclock –set –date=”STRING” as follows.
root@lxu.io ~/Linux-Tricks $ sudo hwclock –set –date=“09/09/2015 12:33:00”
root@lxu.io ~/Linux-Tricks $ sudo hwclock Wednesday 09 September 2015 12:33: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.
root@lxu.io ~/Linux-Tricks $ uptime 12:36:27 up 1:43, 2 users, load average: 1.39, 1.34, 1.45
root@lxu.io ~/Linux-Tricks $ uptime -p up 1 hour, 43 minutes
root@lxu.io ~/Linux-Tricks $ uptime -s 2015-09-09 10:52:47