Interesting Command Line Tips and Tricks in Linux

Interesting Command Line Tips and Tricks in Linux

In this article we will learn some new tips and tricks in the Linux commands.

History command

Using history command, we can check what are the commands did in past.

[root@linuxhelp ~]# history
    4  vim /etc/sysconfig/network
    5  cd
    6  fdisk -l
    7  fdisk /dev/sdb
    8  fdisk -l
    9  fdisk -l /dev/sdb
   10  fdisk -l /dev/sda
   11  fdisk /dev/sda
   12  fdisk -l /dev/sda
   13  fdisk -s /dev/sda2
   14  fdisk -l /dev/sda
   15  fdisk /dev/sda
   16  cd
   .  .  .   .  .
   .  .  .   .  .
   .  .  .   .  .
   990  dd if=/dev/zero of=/tmp/output.img bs=8k count=256k conv=fdatasync  rm -rf /tmp/output.img
  991  du -hsx * | sort -rh | head -6
  992  stat fedora.iso
  993  cd Desktop/
  994  stat Screenshot222.png
  995  pwd
  996  cd /home/jagannath/
  997  cd Desktop/
  998  pwd
  999  stat Screenshot222.png
 1000  man $(ls /bin | shuf | head -1)
 1001  cd
 1002  clear
 1003  history

If you need the history with time stamp, then execute the below command.

[root@linuxhelp ~]# HISTTIMEFORMAT=" %d/%m/%y %T " 
[root@linuxhelp ~]# history
    6  25/03/16 07:19:52 fdisk -l
    7  25/03/16 07:19:52 fdisk /dev/sdb
    8  25/03/16 07:19:52 fdisk -l
    9  25/03/16 07:19:52 fdisk -l /dev/sdb
   10  25/03/16 07:19:52 fdisk -l /dev/sda
   11  25/03/16 07:19:52 fdisk /dev/sda
   12  25/03/16 07:19:52 fdisk -l /dev/sda
   13  25/03/16 07:19:52 fdisk -s /dev/sda2
   14  25/03/16 07:19:52 fdisk -l /dev/sda
   15  25/03/16 07:19:52 fdisk /dev/sda
   16  25/03/16 07:19:52 cd
   17  25/03/16 07:19:52 vim /etc/sysconfig/network
   .  .  .   .  .
   .  .  .   .  .
   .  .  .   .  .
   990  25/03/16 03:30:35 dd if=/dev/zero of=/tmp/output.img bs=8k count=256k conv=fdatasync  rm -rf /tmp/output.img
  991  25/03/16 03:32:50 du -hsx * | sort -rh | head -6
  992  25/03/16 03:37:46 stat fedora.iso
  993  25/03/16 03:38:33 cd Desktop/
  994  25/03/16 03:40:14 stat Screenshot222.png
  995  25/03/16 03:40:32 pwd
  996  25/03/16 03:40:42 cd /home/jagannath/
  997  25/03/16 03:40:55 cd Desktop/
  998  25/03/16 03:40:58 pwd
  999  25/03/16 03:41:08 stat Screenshot222.png
 1000  25/03/16 03:44:23 man $(ls /bin | shuf | head -1)
 1001  25/03/16 07:19:55 cd
 1002  25/03/16 07:19:58 clear
 1003  25/03/16 07:20:13 history
 1004  25/03/16 07:22:59 HISTTIMEFORMAT=" %d/%m/%y %T " 
 1005  25/03/16 07:23:08 history

Add the below line to ~/.bashrc, If you need to append this change permanently.

[root@linuxhelp ~]# export HISTTIMEFORMAT=" %d/%m/%y %T " 
[root@linuxhelp ~]# source ~/.bashrc

Explanation of commands are,

History denotes as GNU History Library.
HISTIMEFORMAT is the Environmental Variable.
%d denotes as Day.
%m denotes as Month.
%y denotes as Year.
%T denotes the Time Stamp.
source represents in short send the contents of file to shell.
.bashrc denotes a shell script, if it started interactively then BASH start to run.

dd command

Using dd command, we can check disk write speed.

[root@linuxhelp ~]# dd if=/dev/zero of=/tmp/output.img bs=8k count=256k conv=fdatasync  rm -rf /tmp/output.img
262144+0 records in
262144+0 records out
2147483648 bytes (2.1 GB) copied, 20.7229 s, 104 MB/s

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

du command

Using du command, we can check first six files that occupying more space in your system.

[root@linuxhelp ~]# du -hsx * | sort -rh | head -6
287M    gitlab-7.7.1_omnibus.5.4.1.ci-1.el6.x86_64.rpm
11M    j
11M    home
4.4M    gitlab-workhorse
3.3M    mantisbt-1.2.19.zip
116K    install.log

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

stat command

Using stat command, we can get the statistics related to a file.

stat filename_ext (viz., stat abc.pdf)

[root@linuxhelp ~]# cd /tmp
[root@linuxhelp tmp]# stat csf.tgz
File: `csf.tgz' 
Size: 688544 Blocks: 1352 IO Block: 4096 regular file
Device: 802h/2050d Inode: 693045 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-03-17 10:09:01.858006913 +0530
Modify: 2016-02-25 23:23:17.000000000 +0530
Change: 2016-03-17 09:59:49.626007416 +0530

Another Example:

[root@linuxhelp tmp]# stat autoinstaller3.log
File: `autoinstaller3.log' 
Size: 12020 Blocks: 24 IO Block: 4096 regular file
Device: 802h/2050d Inode: 656910 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-03-19 07:45:51.854643390 +0530
Modify: 2016-03-19 08:35:23.938004452 +0530
Change: 2016-03-19 08:35:23.938004452 +0530

man command

Using man command, we can generate random man pages and that helps to learn new about linux.

[root@linuxhelp tmp]# man $(ls /bin | shuf | head -1)
OSTNAME(1) Linux Programmer’ s Manual HOSTNAME(1)

NAME
hostname - show or set the system’ s host name
domainname - show or set the system’ s NIS/YP domain name
dnsdomainname - show the system’ s DNS domain name
nisdomainname - show or set system’ s NIS/YP domain name
ypdomainname - show or set the system’ s NIS/YP domain name

SYNOPSIS
hostname [-v] [-a] [--alias] [-d] [--domain] [-f] [--fqdn] [-A] [--all-
fqdns] [-i] [--ip-address] [-I] [--all-ip-addresses] [--long] [-s]
[--short] [-y] [--yp] [--nis]

hostname [-v] [-F filename] [--file filename] [hostname]

domainname [-v] [-F filename] [--file filename] [name]

nodename [-v] [-F filename] [--file filename] [name]

hostname [-v] [-h] [--help] [-V] [--version]

dnsdomainname [-v]
nisdomainname [-v]
ypdomainname [-v]

:q----> to quit

Explanation of commands are,

man command is used to display Linux Man pages.
ls used to Linux Listing Commands.
/bin denotes System Binary file Location.
shuf used to Generate Random Permutation.
head used to Output first n line of file.

FAQ
Q
What is the command to view the statics of the log?
A
The command used to view the statics of the log is
Syntax: "stat autoinstaller3.log"
Q
How to check the first six files occupying space in your system?
A
By using this command we can get
du -hsx * | sort -rh | head -6
Q
How can we generate random man pages?
A
By using this command we can get
man $(ls /bin | shuf | head -1)
Q
How to execute the history with timestamp command?
A
By using this command we can get
HISTTIMEFORMAT=" %d/%m/%y %T "
Q
What is the command to view the statics of the log?
A
The command used to view the statics of the log is
Syntax: "stat autoinstaller3.log"