Some useful tips while working under GNU Bash – part 1

ImageGiven below are some typical commands used in bash environment under a variety of circumstances. Check out the listing and I am sure everyone who reads this will be benefited from at least one of the following…
[1] du utility command
You can use the du utility to estimate file space usage. For example,
user@GNULinux:~$ du -sh /home/user/photorec.ses
    40K    /home/user/photorec.ses
    
-s for summarizing and -h for making the displayed size human readable. Look at another example,

user@GNULinux:~$ cd /home/
    user@GNULinux:/home$ du -sh *
    2.1G    user

[2] Freeze a process temporarily using ID
For a GNU/Linux user explanation about the kill command is totally unnecessary. But the kill command is misleading. Only some indications of              the kill command actually terminate the target process. kill -STOP suspends the target process and can be resumed by kill -CONT. This particular          option can be useful in order to conduct some investigation on the occurence of some doubtful process running on the system. Know the process ID by top command.

 user@GNULinux:/home$ kill -STOP 2255 freezes process 2255 and
    user@GNULinux:/home$ kill -CONT 2255 resumes it.

[3] join command
 join command combines lines from two files based on a common field. In the example below we have two files. One containing the first name and         second containing the last name. Both the files contain the same index.

user@GNULinux:~$ cat 1
    100 Tom
    101 Alen
    102 Kevin
    user@GNULinux:~$ cat 2
    100 John
    101 Justin
    102 Paul
    user@GNULinux:~$ join 1 2
    100 Tom John
    101 Alen Justin
    102 Kevin Paul

[4] stat for displaying status/properties
stat command can be used either to check the status/properties of a single file or the file system.

user@GNULinux:~$ stat /etc/fstab
    File: `/etc/fstab’
    Size: 996           Blocks: 8          IO Block: 4096   regular file
    Device: 809h/2057d    Inode: 2359298     Links: 1
    Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2013-06-26 18:28:25.533022860 +0530
    Modify: 2013-06-24 18:15:10.303597639 +0530
    Change: 2013-06-24 18:15:10.303597639 +0530
    Birth: –

Display the status of the file system using option –f

user@GNULinux:~$ stat -f /
    File: “/”
    ID: 2c64964204156b69 Namelen: 255     Type: ext2/ext3
    Block size: 4096       Fundamental block size: 4096
    Blocks: Total: 12302800   Free: 10766735   Available: 10141775
    Inodes: Total: 3129344    Free: 2924549

[5] List the open files
 lsof command stands for ls open files, which will list all the open files in the system. Simply executing lsof may return lot of records as         output which may not be useful. lsof used with wc gives you the count of open files.

user@GNULinux:~$ lsof | wc -l
    4701
    
Use lsof –u option to display all the files opened by a specific user.

 user@GNULinux:~$ lsof -u user

If you like to view all the users who are using a particular file, use lsof as shown below

 user@GNULinux:~$ lsof /bin/nano
    COMMAND  PID  USER  FD   TYPE DEVICE SIZE/OFF    NODE NAME
    nano    3568 user txt    REG    8,9   191960 2883673 /bin/nano

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s