Monday 24 October 2016

Advanced Linux Commands

Here are a few linux commands that are pretty useful. However no one every bothers teaching you these commands. However, here at Programing Wonders we never leave out such awesome stuff. Like how to create this:

whiptail to create password box








df

       df - report file system disk space usage
There are a large number of options such as

-h Human readable

-i  Inodes display

For a complete list type man df or info df

Here is the output of df without any arguments


Filesystem     1K-blocks      Used Available Use% Mounted on
udev             1266876         4   1266872   1% /dev
tmpfs             255600      1300    254300   1% /run
/dev/sda7       27694268  22386200   3878212  86% /
none                   4         0         4   0% /sys/fs/cgroup
none                5120         0      5120   0% /run/lock
none             1277992       152   1277840   1% /run/shm
none              102400        36    102364   1% /run/user
/dev/sdb5      976728060 890227100  86500960  92% /media/hulk/Abhishek


This clearly shows the used space on various devices.However this output is hard to understand. Thus we use the -h option to get the following output


Filesystem      Size  Used Avail Use% Mounted on
udev            1.3G  4.0K  1.3G   1% /dev
tmpfs           250M  1.3M  249M   1% /run
/dev/sda7        27G   22G  3.7G  86% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none            1.3G  156K  1.3G   1% /run/shm
none            100M   36K  100M   1% /run/user
/dev/sdb5       932G  849G   83G  92% /media/hulk/Abhishek
 




Sometimes we have space on the disk however we run out of inodes. You could encounter this problem when you have a very large number of small files. Like 40-50 android studio projects. This command then comes handy. You can find out the number of inodes used using the -i option

Filesystem       Inodes  IUsed    IFree IUse% Mounted on
udev             316719    557   316162    1% /dev
tmpfs            319498    584   318914    1% /run
/dev/sda7       1769472 634325  1135147   36% /
none             319498      2   319496    1% /sys/fs/cgroup
none             319498      5   319493    1% /run/lock
none             319498      6   319492    1% /run/shm
none             319498     25   319473    1% /run/user
/dev/sdb5      86599264  19049 86580215    1% /media/hulk/Abhishek



File 

file - it can easily identify the file type.

Here's a sample output

hulk@unknown:~$ touch python.py
hulk@unknown:~$ file python.py
python.py: empty  


The best part is that this command identifies the fact that the file is empty. So you could easily write a script to delete all empty file. Simple pipe the output of file command to cut command and pipe it to rm. All empty files deleted.

hulk@unknown:~$ cat >> python.py
print "Hello"
^Z
[1]+  Stopped                 cat >> python.py
hulk@unknown:~$ cat python.py
print "Hello"
hulk@unknown:~$ file python.py
python.py: ASCII text


After writing some text it identifies that the file is ascii text. Just add the #!/bin/python file and it will identify the fact that it is a python executable file

hulk@unknown:~$ cat > python.py
#!/bin/python
^Z
[2]+  Stopped                 cat > python.py
hulk@unknown:~$ cat python.py
#!/bin/python
hulk@unknown:~$ file python.py
python.py: a /bin/python script, ASCII text executable



It works great with executable files also

hulk@unknown:~$ ll r
-rwxrwxr-x 1 hulk hulk 9130 Oct 19 23:55 r*
hulk@unknown:~$ file r
r: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=254054e1e9484ccbce683b315e3875f6d426841e, not stripped





File command is one of the most awesome tools in linux. You can write tons of scripts to make your work simpler using the pipe command.


whiptail


Beautify your shell scripts with this awesome utility. It allows to create yes/no options or display a simple message box. There are tons of option, it simply makes your scripts more user friendly and awesome.

I could write an entire article about whiptail. Try it out next time you write a shell script. Here is a small demonstration of the same.

whiptail --passwordbox "Please enter password to proceed" 25 25

How to create password field using whiptail
whiptail-password box



Doesn't it  look great.  Here's another example.

whiptail --msgbox "The author feels that whiptail is cool" 25 25




How to create msg box using whiptail
Whiptail-msgbox





true & false

Yes these are commands in linux.
The man page for true states that do nothing successfully. Wow !.
For false the it does nothing unsuccessfully. 

I don't know when it comes handy. Just good to know.

Here is a sample output for the same

hulk@unknown:/bin$ true && echo hi
hi


hulk@unknown:/bin$ false && echo hi || echo bye
bye

dir


This command list the directories contents. I personally prefer ls. But there this alternative. The output for ls looks better has it is color coded and better formatted .

ss

It is a utility to investigate sockets. Next time you use a port number in the registered port range 1024-49151 to bother using this utility. Or better still use private port range 49152+.

I am not attaching the output since it will give out my ip address. Do execute and check out the output.







No comments:

Post a Comment