fswatch – Monitors Files and Directory Changes or Modifications in Linux

fswatch is a cross-platform, file change monitor that gets notification alerts when the contents of the specified files or directories are altered or modified.

It executes four types of monitors on different operating systems such as:

  1. A monitor build on the File System Events API of Apple OS X.
  2. A monitor based on kqueue, a notification interface present in FreeBSD 4.1 also supported on many *BSD systems, OS X inclusive.
  3. A monitor based on File Events Notification API of the Solaris kernel plus its spin-offs.
  4. A monitor based on inotify, a kernel subsystem that shows file system modifications to apps.
  5. A monitor based on ReadDirectoryChangesW, a Windows API that records alters to a directory.
  6. A monitor that regularly check that status of file system, keeps file modification times in memory, and manually determine file system changes (which works anywhere, where stat can be used).

Features of fswatch

  1. Supports several OS-specific APIs
  2. Allows recursive directory monitoring
  3. Performs path filtering using including and excluding regular expressions
  4. Supports customizable record format
  5. Additionally, it supports periodic idle events

How To Install fswatch in Linux Systems

Unfortunately, fswatch package is not available to install from the default system repositories in any Linux distributions. The only way to install the latest version of fswatch is to build from source tarball as show in the following installation instructions.

First grab the latest fswatch tarball using following wget command and install it as shown:

$ wget https://github.com/emcrisostomo/fswatch/releases/download/1.9.3/fswatch-1.9.3.tar.gz
$ tar -xvzf fswatch-1.9.3.tar.gz
$ cd fswatch-1.9.3
$ ./configure
$ make
$ sudo make install

Important: Make sure you’ve GNU GCC (C and C++ Compiler) and Development Tools (build-essential on Debian/Ubuntu) installed on the system, before you compile fswatch from source. If not, install it using following command on your respective Linux distributions..

# yum group install 'Development Tools'		[On CentOS/RHEL]
# dnf group install 'Development Tools'		[On Fedora 22+ Versions]
$ sudo apt-get install build-essential          [On Debian/Ubuntu Versions]

On Debian/Ubuntu distributions, you might get following error while executing fswatch command..

fswatch: error while loading shared libraries: libfswatch.so.6: cannot open shared object file: No such file or directory

To fix it, you need to execute the command below, this will help refresh the links and cache to the dynamic libraries before you can start using fswatch.

$ sudo ldconfig

How do I use fswatch on Linux?

The general syntax for running fswatch is:

$ fswatch [option] [path]

On Linux, it is recommended that you use the default inotify monitor, you can list available monitors by employing the -M or - list-monitors option:

$ fswatch -M
$ fswatch --list-monitors

fswatch - List Monitors

fswatch – List Monitors

The command below enables you to watch the changes in the current directory (/home/tecmint), with events being delivered to standard output every 4 seconds.

The -l or –-latency option allows you to set the latency in seconds, the default being 1 second.

$ fswatch -l 4 . 

fswatch - Monitor Home Directory Changes

The next command monitors changes to the /var/log/auth.log file every 5 seconds:

$ fswatch -l 5 /var/log/auth.log

Using -t or --timestamp option prints the time stamp for every event, to print the time in UTC format, employ -u or --utf-time option. You can as well format time using -f or --format-time format option:

$ fswatch --timestamp /var/log/auth.log

Next, -x or --event-flags tells fswatch to print the event flags along side the event path. You can use –event-field-seperator option to print events using the particular separator.

$ fswatch --events-flags ~ /var/log/auth.log

To print the numeric value of an event indicating changes in your home directory and /var/log/auth.log file, use -n or --numeric option as below:

$ fswatch --numeric ~ /var/log/auth.log 

Perhaps you can look through the fswatch man page for detailed usage options and information:

$ man fswatch

For more information and usage, visit fswatch Github repository: https://github.com/emcrisostomo/fswatch

In this post, we covered a simple command line utility to help Linux users get notified when the contents of specified files or directory hierarchies are modified.

I hope all went well with the installation, if that is not the case for you, make an effort to reach us via the feedback form below. In addition, in case you have used it before, you may want to offer us some thoughts about your experience with fswatch.

Source

The Power of Linux “History Command” in Bash Shell

We use history command frequently in our daily routine jobs to check history of command or to get info about command executed by user. In this post, we will see how we can use history command effectively to extract the command which was executed by users in Bash shell. This may be useful for audit purpose or to find out what command is executed at what date and time.

By default date and timestamp won’t be seen while executing history command. However, bash shell provides CLI tools for editing user’s command history. Let’s see some handy tips and tricks and power of historycommand.

linux history command

history command examples

1. List Last/All Executed Commands in Linux

Executing simple history command from terminal will show you a complete list of last executed commands with line numbers.

[narad@tecmint ~]$ history

    1  PS1='\e[1;35m[\u@\h \w]$ \e[m '
    2  PS1="\e[0;32m[\u@\h \W]$ \e[m "
    3  PS1="\u@\h:\w [\j]$ "
    4  ping google.com
    5  echo $PS1
    6   tail -f /var/log/messages
    7  tail -f /var/log/messages
    8  exit
    9  clear
   10  history
   11  clear
   12  history

2. List All Commands with Date and Timestamp

How to find date and timestamp against command? With ‘export’ command with variable will display history command with corresponding timestamp when the command was executed.

[narad@tecmint ~]$ export HISTTIMEFORMAT='%F %T  '

      1  2013-06-09 10:40:12   cat /etc/issue
      2  2013-06-09 10:40:12   clear
      3  2013-06-09 10:40:12   find /etc -name *.conf
      4  2013-06-09 10:40:12   clear
      5  2013-06-09 10:40:12   history
      6  2013-06-09 10:40:12   PS1='\e[1;35m[\u@\h \w]$ \e[m '
      7  2013-06-09 10:40:12   PS1="\e[0;32m[\u@\h \W]$ \e[m "
      8  2013-06-09 10:40:12   PS1="\u@\h:\w [\j]$ "
      9  2013-06-09 10:40:12   ping google.com
     10  2013-06-09 10:40:12   echo $PS1
Meaning of HISTTIMEFORMAT variables
%F Equivalent to %Y - %m - %d
%T Replaced by the time ( %H : %M : %S )

As we can see same command is being repeated number of times in above output. How to filter simple or non destructive commands in history?. Use the following ‘export‘ command by specifying command in HISTIGNORE=’ls -l:pwd:date:’ will not saved by system and not be shown in history command.

[narad@tecmint ~]$ export HISTIGNORE='ls -l:pwd:date:'

4. Ignore Duplicate Commands in History

With the below command will help us to ignore duplicate commands entry made by user. Only single entry will be shown in history, if a user execute a same command multiple times in a Bash Prompt.

[narad@tecmint ~]$ export HISTCONTROL=ignoredups

5. Unset export Command

Unset export command on the fly. Execute unset export command with variable one by one whatever commands have been exported by export command.

[narad@tecmint ~]$ unset export HISTCONTROL

6. Save export Command Permanently

Make an entry as follows in .bash_profile to save export command permanently.

[narad@tecmint ~]$ vi .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

export HISTCONTROL=ignoredups

PATH=$PATH:$HOME/bin
export PATH

7. List Specific User’s Executed Commands

How to see command history executed by a specific user. Bash keeps records of history in a ‘~/.bash_history’file. We can view or open file to see the command history.

[narad@tecmint ~]$ vi .bash_history

cd /tmp/
cd logstalgia-1.0.3/
./configure
sudo passwd root
apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc
./configure
make
apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc++
apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc
apt-get install make
mysql -u root -p
apt-get install grsync
apt-get install unison
unison

8. Disable Storing History of Commands

Some organization do not keep history of commands because of security policy of the organization. In this case, we can edit .bash_profile file (It’s hidden file) of user’s and make an entry as below.

[narad@tecmint ~]$ vi .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
HISTSIZE=0
export PATH
.bash_profile (END)

Save file and load changes with below command.

[narad@tecmint ~]$ source .bash_profile

Note: If you don’t want system to remember the commands that you have typed, simply execute below command which will disable or stop recording history on the fly.

[narad@tecmint ~]$ export HISTSIZE=0

Tips: Search ‘HISTSIZE‘ and edit in ‘/etc/profile’ file with superuser. The change in file will effect globally.

9. Delete or Clear History of Commands

With up and down arrow, we can see previously used command which may be helpful or may irate you. Deleting or clearing all the entries from bash history list with ‘-c‘ options.

[narad@tecmint ~]$ history -c

10. Search Commands in History Using Grep Command

Search command through ‘.bash_history‘ by piping your history file into ‘grep‘ as below. For example, the below command will search and find ‘pwd‘ command from the history list.

[narad@tecmint ~]$ history | grep pwd

  113  2013-06-09 10:40:12     pwd
  141  2013-06-09 10:40:12     pwd
  198  2013-06-09 15:46:23     history | grep pwd
  202  2013-06-09 15:47:39     history | grep pwd

11. Search Lastly Executed Command

Search previously executed command with ‘Ctrl+r’ command. Once you’ve found the command you’re looking for, press ‘Enter‘ to execute the same else press ‘esc‘ to cancel it.

(reverse-i-search)`source ': source .bash_profile

12. Recall Last Executed Command

Recall a previously used specific command. Combination of Bang and 8 (!8) command will recall number 8command which you have executed.

[narad@tecmint ~]$ !8

13. Recall Lastly Executed Specific Command

Recall previously used command (netstat -np | grep 22) with ‘!‘ and followed by some letters of that particular command.

[narad@tecmint ~]$ !net
netstat -np | grep 22
(No info could be read for "-p": geteuid()=501 but you should be root.)
tcp        0     68 192.168.50.2:22             192.168.50.1:1857           ESTABLISHED -
tcp        0      0 192.168.50.2:22             192.168.50.1:2516           ESTABLISHED -
unix  2      [ ]         DGRAM                    12284  -                   @/org/freedesktop/hal/udev_event
unix  3      [ ]         STREAM     CONNECTED     14522  -
unix  2      [ ]         DGRAM                    13622  -
unix  3      [ ]         STREAM     CONNECTED     12250  -                   @/var/run/hald/dbus-ujAjOMNa0g
unix  3      [ ]         STREAM     CONNECTED     12249  -
unix  3      [ ]         STREAM     CONNECTED     12228  -                   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     12227  -

We have tried to highlight power of history command. However, this is not end of it. Please share your experience of history command with us through our comment box below.

Source

15 ‘pwd’ (Print Working Directory) Command Examples in Linux

For those working with Linux command Line, command ‘pwd‘ is very helpful, which tells where you are – in which directory, starting from the root (/). Specially for Linux newbies, who may get lost amidst of directories in command Line Interface while navigation, command ‘pwd‘ comes to rescue.

Linux pwd Command Examples

15 pwd Command Examples

What is pwd?

pwd‘ stands for ‘Print Working Directory‘. As the name states, command ‘pwd‘ prints the current working directory or simply the directory user is, at present. It prints the current directory name with the complete path starting from root (/). This command is built in shell command and is available on most of the shell – bash, Bourne shell, ksh,zsh, etc.

Basic syntax of pwd:
# pwd [OPTION]
Options used with pwd
 Options  Description
 -L (logical)  Use PWD from environment, even if it contains symbolic links
 -P (physical)  Avoid all symbolic links
 –help  Display this help and exit
 –version  Output version information and exit

If both ‘-L‘ and ‘-P‘ options are used, option ‘L‘ is taken into priority. If no option is specified at the prompt, pwd will avoid all symlinks, i.e., take option ‘-P‘ into account.

Exit status of command pwd:

0 Success
Non-zero Failure

This article aims at providing you a deep insight of Linux command ‘pwd‘ with practical examples.

1. Print your current working directory.

avi@tecmint:~$ /bin/pwd

/home/avi

pwd linux command

Print Working Directory

2. Create a symbolic link of a folder (say /var/www/html into your home directory as htm). Move to the newly created directory and print working directory with symbolic links and without symbolic links.

Create a symbolic link of folder /var/www/html as htm in your home directory and move to it.

avi@tecmint:~$ ln -s /var/www/html/ htm
avi@tecmint:~$ cd htm

Create Symbolic Link

Create Symbolic Link

3. Print working directory from environment even if it contains symlinks.

avi@tecmint:~$ /bin/pwd -L

/home/avi/htm

Print Current Working Directory

Print Current Working Directory

4. Print actual physical current working directory by resolving all symbolic links.

avi@tecmint:~$ /bin/pwd -P

/var/www/html

Print Physical Working Directory

Print Physical Working Directory

5. Check if the output of command “pwd” and “pwd -P” are same or not i.e., if no options are given at run-time does “pwd” takes option -P into account or not, automatically.

avi@tecmint:~$ /bin/pwd

/var/www/html

Check pwd Output

Check pwd Output

Result: It’s clear from the above output of example 4 and 5 (both result are same) thus, when no options are specified with command “pwd”, it automatically takes option “-P” into account.

6. Print version of your ‘pwd’ command.

avi@tecmint:~$ /bin/pwd --version

pwd (GNU coreutils) 8.23
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jim Meyering.

Check pwd Version

Check pwd Version

Note: A ‘pwd’ command is often used without options and never used with arguments.

Important: You might have noticed that we are executing the above command as “/bin/pwd” and not “pwd”.

So what’s the difference? Well “pwd” alone means shell built-in pwd. Your shell may have different version of pwd. Please refer manual. When we are using /bin/pwd, we are calling the binary version of that command. Both the shell and the binary version of command Prints Current Working Directory, though the binary version have more options.

7. Print all the locations containing executable named pwd.

avi@tecmint:~$ type -a pwd

pwd is a shell builtin
pwd is /bin/pwd

Print Executable Locations

Print Executable Locations

8. Store the value of “pwd” command in variable (say a), and print its value from the variable (important for shell scripting perspective).

avi@tecmint:~$ a=$(pwd)
avi@tecmint:~$ echo "Current working directory is : $a"

Current working directory is : /home/avi

Store Pwd Value in Variable

Store Pwd Value in Variable

Alternatively, we can use printf, in the above example.

9. Change current working directory to anything (say /home) and display it in command line prompt. Execute a command (say ‘ls‘) to verify is everything is OK.

avi@tecmint:~$ cd /home
avi@tecmint:~$ PS1='$pwd> '		[Notice single quotes in the example]
> ls

Change Current Working Directory

Change Current Working Directory

10. Set multi-line command line prompt (say something like below).

/home
123#Hello#!

And then execute a command (say ls) to check is everything is OK.

avi@tecmint:~$ PS1='
> $PWD
$ 123#Hello#!
$ '

/home
123#Hello#!

Set Multi Commandline Prompt

Set Multi Commandline Prompt

11. Check the current working directory and previous working directory in one GO!

avi@tecmint:~$ echo “$PWD $OLDPWD”

/home /home/avi

Check Present Previous Working Directory

Check Present Previous Working Directory

12. What is the absolute path (starting from /) of the pwd binary file.

/bin/pwd 

13. What is the absolute path (starting from /) of the pwd source file.

/usr/include/pwd.h 

14. Print the absolute path (starting from /) of the pwd manual pages file.

/usr/share/man/man1/pwd.1.gz

15. Write a shell script analyses current directory (say tecmint) in your home directory. If you are under directory tecmint it output “Well! You are in tecmint directory” and then print “Good Bye” else create a directory tecmintunder your home directory and ask you to cd to it.

Let’s first create a ‘tecmint’ directory, under it create a following shell script file with name ‘pwd.sh’.

avi@tecmint:~$ mkdir tecmint
avi@tecmint:~$ cd tecmint
avi@tecmint:~$ nano pwd.sh

Next, add the following script to the pwd.sh file.

#!/bin/bash

x="$(pwd)"
if [ "$x" == "/home/$USER/tecmint" ]
then
     {
      echo "Well you are in tecmint directory"
      echo "Good Bye"
     }
else
     {
      mkdir /home/$USER/tecmint
      echo "Created Directory tecmint you may now cd to it"
     }
fi

Give execute permission and run it.

avi@tecmint:~$ chmod 755 pwd.sh
avi@tecmint:~$ ./pwd.sh

Well you are in tecmint directory
Good Bye

Conclusion

pwd is one of the simplest yet most popular and most widely used command. A good command over pwd is basic to use Linux terminal. That’s all for now.

Source

5 Ways to Empty or Delete a Large File Content in Linux

Occasionally, while dealing with files in Linux terminal, you may want to clear the content of a file without necessarily opening it using any Linux command line editors. How can this be achieved? In this article, we will go through several different ways of emptying file content with the help of some useful commands.

Caution: Before we proceed to looking at the various ways, note that because in Linux everything is a file, you must always make sure that the file(s) you are emptying are not important user or system files. Clearing the content of a critical system or configuration file could lead to a fatal application/system error or failure.

With that said, below are means of clearing file content from the command line.

Important: For the purpose of this article, we’ve used file access.log in the following examples.

1. Empty File Content by Redirecting to Null

A easiest way to empty or blank a file content using shell redirect null (non-existent object) to the file as below:

# > access.log

Empty Large File Using Null Redirect in Linux

Empty Large File Using Null Redirect in Linux

2. Empty File Using ‘true’ Command Redirection

Here we will use a symbol : is a shell built-in command that is essence equivalent to the true command and it can be used as a no-op (no operation).

Another method is to redirect the output of : or true built-in command to the file like so:

# : > access.log
OR 
# true > access.log

Empty Large File Using Linux Commands

Empty Large File Using Linux Commands

3. Empty File Using cat/cp/dd utilities with /dev/null

In Linux, the null device is basically utilized for discarding of unwanted output streams of a process, or else as a suitable empty file for input streams. This is normally done by redirection mechanism.

And the /dev/null device file is therefore a special file that writes-off (removes) any input sent to it or its output is same as that of an empty file.

Additionally, you can empty contents of a file by redirecting output of /dev/null to it (file) as input using cat command:

# cat /dev/null > access.log

Empty File Using cat Command

Empty File Using cat Command

Next, we will use cp command to blank a file content as shown.

# cp /dev/null access.log

Empty File Content Using cp Command

Empty File Content Using cp Command

In the following command, if means the input file and of refers to the output file.

# dd if=/dev/null of=access.log

Empty File Content Using dd Command

Empty File Content Using dd Command

4. Empty File Using echo Command

Here, you can use an echo command with an empty string and redirect it to the file as follows:

# echo "" > access.log
OR
# echo > access.log

Empty File Using echo Command

Empty File Using echo Command

Note: You should keep in mind that an empty string is not the same as null. A string is already an object much as it may be empty while null simply means non-existence of an object.

For this reason, when you redirect the out of the echo command above into the file, and view the file contents using the cat command, is prints an empty line (empty string).

To send a null output to the file, use the flag -n which tells echo to not output the trailing newline that leads to the empty line produced in the previous command.

# echo -n "" > access.log

Empty File Using Null Redirect

Empty File Using Null Redirect

5. Empty File Using truncate Command

The truncate command helps to shrink or extend the size of a file to a defined size.

You can employ it with the -s option that specifies the file size. To empty a file content, use a size of 0 (zero) as in the next command:

# truncate -s 0 access.log

Truncate File Content in Linux

Truncate File Content in Linux

That’s it for now, in this article we have covered multiple methods of clearing or emptying file content using simple command line utilities and shell redirection mechanism.

These are not probably the only available practical ways of doing this, so you can also tell us about any other methods not mentioned in this guide via the feedback section below.

Source

Mhddfs – Combine Several Smaller Partition into One Large Virtual Storage

Let’s assume that you have 30GB of movies and you have 3 drives each 20 GB in size. So how will you store?

Obviously you can split your videos in two or three different volumes and store them on the drive manually. This certainly is not a good idea, it is an exhaustive work which requires manual intervention and a lots of your time.

Another solution is to create a RAID array of disk. The RAID has always remained notorious for loss of storage reliability and usable disk space. Another solution is mhddfs.

Combine Multiple Partitions in Linux

Mhddfs – Combine Multiple Partitions in Linux

mhddfs is a driver for Linux that combines several mount points into one virtual disk. It is a fuse based driver, which provides a easy solution for large data storage. It combines all small file systems to create a single big virtual filesystem which contains every particle of its member filesystem including files and free spaces.

Why you need Mhddfs?

All your storage devices creates a single virtual pool and it can be mounted right at the boot. This small utility takes care of, which drive is full and which is empty and to write data to what drive, intelligently. Once you create virtual drives successfully, you can share your virtual filesystem using SAMBA. Your client will always see a huge drive and lots of free space.

Features of Mhddfs

  1. Get attributes of the file system and system information.
  2. Set attributes of the file system.
  3. Create, Read, Remove and write Directories and files.
  4. Support for file locks and Hardlinks on single device.
Pros of mhddfs Cons of mhddfs
 Perfect for home users. mhddfs driver is not built in the Linux Kernel
 Simple to run.  Required lots of processing power during runtime
 No evidence of Data loss  No redundancy solution.
 Do not split the file.  Hardlinks moving not supported
 Add new files to the combined virtual filesystem.
 Manage the location where these files are saved.
  Extended file attributes

Installation of Mhddfs in Linux

On Debian and portable to alike systems, you can install mhddfs package using following command.

# apt-get update && apt-get install mhddfs

Install Mhddfs on Debian based Systems

Install Mhddfs on Debian based Systems

On RHEL/CentOS Linux systems, you need to turn on epel-repository and then execute the below command to install mhddfs package.

# yum install mhddfs

On Fedora 22+ systems, you may get it by dnf package manger as shown below.

# dnf install mhddfs

Install Mhddfs on Fedora

Install Mhddfs on Fedora

If incase, mhddfs package isn’t available from epel repository, then you need to resolve following dependencies to install and compile it from source as shown below.

  1. FUSE header files
  2. GCC
  3. libc6 header files
  4. uthash header files
  5. libattr1 header files (optional)

Next, download the latest source package simply as suggested below and compile it.

# wget http://mhddfs.uvw.ru/downloads/mhddfs_0.1.39.tar.gz
# tar -zxvf mhddfs*.tar.gz
# cd mhddfs-0.1.39/
# make

You should be able to see binary mhddfs in the current directory. Move it to /usr/bin/ and /usr/local/bin/ as root.

# cp mhddfs /usr/bin/ 
# cp mhddfs /usr/local/bin/

All set, mhddfs is ready to be used.

How do I use Mhddfs?

1. Lets see all the HDD mounted to my system currently.

$ df -h

Check Mounted Devices

Sample Output
Filesystem      Size  Used Avail Use% Mounted on

/dev/sda1       511M  132K  511M   1% /boot/efi
/dev/sda2       451G   92G  336G  22% /
/dev/sdb1       1.9T  161G  1.7T   9% /media/avi/BD9B-5FCE
/dev/sdc1       555M  555M     0 100% /media/avi/Debian 8.1.0 M-A 1

Notice the ‘Mount Point‘ name here, which we will be using later.

2. Create a directory /mnt/virtual_hdd where all these all file system will be grouped together as,

# mkdir /mnt/virtual_hdd

3. And then mount all the file-systems. Either as root or as a user who is a member of FUSE group.

# mhddfs /boot/efi, /, /media/avi/BD9B-5FCE/, /media/avi/Debian\ 8.1.0\ M-A\ 1/ /mnt/virtual_hdd  -o allow_other

Mount All File System in Linux

Mount All File System in Linux

Note: We are used mount Point names here of all the HDDs. Obviously the mount point in your case will be different. Also notice “-o allow_other” option makes this Virtual file system visible to all others and not only the person who created it.

4. Now run “df -h” see all the filesystems. It should contain the one you created just now.

$ df -h

Verify Virtual File System Mount

Verify Virtual File System Mount

You can perform all the option to the Virtual File System you created as you would have done to a Mounted Drive.

5. To create this Virtual File system on every system boot, you should add the below line of code (in your case it should be different, depending upon your mount point), at the end of /etc/fstab file as root.

mhddfs# /boot/efi, /, /media/avi/BD9B-5FCE/, /media/avi/Debian\ 8.1.0\ M-A\ 1/ /mnt/virtual_hdd fuse defaults,allow_other 0 0

6. If at any point of time you want to add/remove a new drive to Virtual_hdd, you may mount a new drive, copy the contents of mount point /mnt/virtual_hddun-mount the volume, Eject the Drive you want to remove and/or mount the new drive you want to include, Mount the overall filesystem under Virtual_hdd using mhddfscommand and you should be done.

How do I Un-Mount Virtual_hdd?

Unmounting virtual_hdd is as easy as,

# umount /mnt/virtual_hdd

Unmount Virtual Filesystem

Unmount Virtual Filesystem

Notice it is umount and not unmount. A lot of user type it wrong.

That’s all for now. I am working on another post you people will love to read. Till then stay tuned and connected to Tecmint. Provide us with your valuable feedback in the comments below. Like and share us and help us get spread.

Source

10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk usage of files and directories on a machine. The du command has many parameter options that can be used to get the results in many formats. The du command also displays the files and directory sizes in a recursively manner.

Check Disk Usage In Linux

Check Disk Usage of Files and Folders In Linux

This article explains 10 useful “du” commands with their examples, that might helps you to find out the sizes of files and directories in Linux. The information provided in this article are taken from the man pages of ducommand.

Read Also:

  1. 12 “df” Command to Check Linux System Disk Space
  2. Agedu – A Useful Tool for Tracking Down Wasted Disk Space in Linux

1. To find out the disk usage summary of a /home/tecmint directory tree and each of its sub directories. Enter the command as:

[root@tecmint]# du  /home/tecmint

40      /home/tecmint/downloads
4       /home/tecmint/.mozilla/plugins
4       /home/tecmint/.mozilla/extensions
12      /home/tecmint/.mozilla
12      /home/tecmint/.ssh
689112  /home/tecmint/Ubuntu-12.10
689360  /home/tecmint

The output of the above command displays the number of disk blocks in the /home/tecmint directory along with its sub-directories.

2. Using “-h” option with “du” command provides results in “Human Readable Format“. Means you can see sizes in BytesKilobytes, MegabytesGigabytes etc.

[root@tecmint]# du -h /home/tecmint

40K     /home/tecmint/downloads
4.0K    /home/tecmint/.mozilla/plugins
4.0K    /home/tecmint/.mozilla/extensions
12K     /home/tecmint/.mozilla
12K     /home/tecmint/.ssh
673M    /home/tecmint/Ubuntu-12.10
674M    /home/tecmint

3. To get the summary of a grand total disk usage size of an directory use the option “-s” as follows.

[root@tecmint]# du -sh /home/tecmint

674M    /home/tecmint

4. Using “-a” flag with “du” command displays the disk usage of all the files and directories.

[root@tecmint]# du -a /home/tecmint

4       /home/tecmint/.bash_logout
12      /home/tecmint/downloads/uploadprogress-1.0.3.1.tgz
24      /home/tecmint/downloads/Phpfiles-org.tar.bz2
40      /home/tecmint/downloads
12      /home/tecmint/uploadprogress-1.0.3.1.tgz
4       /home/tecmint/.mozilla/plugins
4       /home/tecmint/.mozilla/extensions
12      /home/tecmint/.mozilla
4       /home/tecmint/.bashrc
689108  /home/tecmint/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
689112  /home/tecmint/Ubuntu-12.10
689360  /home/tecmint

5. Using “-a” flag along with “-h” displays disk usage of all files and folders in human readeable format. The below output is more easy to understand as it shows the files in KilobytesMegabytes etc.

[root@tecmint]# du -ah /home/tecmint

4.0K    /home/tecmint/.bash_logout
12K     /home/tecmint/downloads/uploadprogress-1.0.3.1.tgz
24K     /home/tecmint/downloads/Phpfiles-org.tar.bz2
40K     /home/tecmint/downloads
12K     /home/tecmint/uploadprogress-1.0.3.1.tgz
4.0K    /home/tecmint/.mozilla/plugins
4.0K    /home/tecmint/.mozilla/extensions
12K     /home/tecmint/.mozilla
4.0K    /home/tecmint/.bashrc
673M    /home/tecmint/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M    /home/tecmint/Ubuntu-12.10
674M    /home/tecmint

6. Find out the disk usage of a directory tree with its subtress in Kilobyte blcoks. Use the “-k” (displays size in 1024 bytes units).

[root@tecmint]# du -k /home/tecmint
40      /home/tecmint/downloads
4       /home/tecmint/.mozilla/plugins
4       /home/tecmint/.mozilla/extensions
12      /home/tecmint/.mozilla
12      /home/tecmint/.ssh
689112  /home/tecmint/Ubuntu-12.10
689360  /home/tecmint

7. To get the summary of disk usage of directory tree along with its subtrees in Megabytes (MB) only. Use the option “-mh” as follows. The “-m” flag counts the blocks in MB units and “-h” stands for human readable format.

[root@tecmint]# du -mh /home/tecmint

40K     /home/tecmint/downloads
4.0K    /home/tecmint/.mozilla/plugins
4.0K    /home/tecmint/.mozilla/extensions
12K     /home/tecmint/.mozilla
12K     /home/tecmint/.ssh
673M    /home/tecmint/Ubuntu-12.10
674M    /home/tecmint

8. The “-c” flag provides a grand total usage disk space at the last line. If your directory taken 674MB space, then the last last two line of the output would be.

[root@tecmint]# du -ch /home/tecmint

40K     /home/tecmint/downloads
4.0K    /home/tecmint/.mozilla/plugins
4.0K    /home/tecmint/.mozilla/extensions
12K     /home/tecmint/.mozilla
12K     /home/tecmint/.ssh
673M    /home/tecmint/Ubuntu-12.10
674M    /home/tecmint
674M    total

9. The below command calculates and displays the disk usage of all files and directories, but excludes the files that matches given pattern. The below command excludes the “.txt” files while calculating the total size of diretory. So, this way you can exclude any file formats by using flag “-–exclude“. See the output there is no txtfiles entry.

[root@tecmint]# du -ah --exclude="*.txt" /home/tecmint

4.0K    /home/tecmint/.bash_logout
12K     /home/tecmint/downloads/uploadprogress-1.0.3.1.tgz
24K     /home/tecmint/downloads/Phpfiles-org.tar.bz2
40K     /home/tecmint/downloads
12K     /home/tecmint/uploadprogress-1.0.3.1.tgz
4.0K    /home/tecmint/.bash_history
4.0K    /home/tecmint/.bash_profile
4.0K    /home/tecmint/.mozilla/plugins
4.0K    /home/tecmint/.mozilla/extensions
12K     /home/tecmint/.mozilla
4.0K    /home/tecmint/.bashrc
24K     /home/tecmint/Phpfiles-org.tar.bz2
4.0K    /home/tecmint/geoipupdate.sh
4.0K    /home/tecmint/.zshrc
120K    /home/tecmint/goaccess-0.4.2.tar.gz.1
673M    /home/tecmint/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M    /home/tecmint/Ubuntu-12.10
674M    /home/tecmint

10. Display the disk usage based on modification of time, use the flag “–time” as shown below.

[root@tecmint]# du -ha --time /home/tecmint

4.0K    2012-10-12 22:32        /home/tecmint/.bash_logout
12K     2013-01-19 18:48        /home/tecmint/downloads/uploadprogress-1.0.3.1.tgz
24K     2013-01-19 18:48        /home/tecmint/downloads/Phpfiles-org.tar.bz2
40K     2013-01-19 18:48        /home/tecmint/downloads
12K     2013-01-19 18:32        /home/tecmint/uploadprogress-1.0.3.1.tgz
4.0K    2012-10-13 00:11        /home/tecmint/.bash_history
4.0K    2012-10-12 22:32        /home/tecmint/.bash_profile
0       2013-01-19 18:32        /home/tecmint/xyz.txt
0       2013-01-19 18:32        /home/tecmint/abc.txt
4.0K    2012-10-12 22:32        /home/tecmint/.mozilla/plugins
4.0K    2012-10-12 22:32        /home/tecmint/.mozilla/extensions
12K     2012-10-12 22:32        /home/tecmint/.mozilla
4.0K    2012-10-12 22:32        /home/tecmint/.bashrc
24K     2013-01-19 18:32        /home/tecmint/Phpfiles-org.tar.bz2
4.0K    2013-01-19 18:32        /home/tecmint/geoipupdate.sh
4.0K    2012-10-12 22:32        /home/tecmint/.zshrc
120K    2013-01-19 18:32        /home/tecmint/goaccess-0.4.2.tar.gz.1
673M    2013-01-19 18:51        /home/tecmint/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M    2013-01-19 18:51        /home/tecmint/Ubuntu-12.10
674M    2013-01-19 18:52        /home/tecmint

Read Also :

  1. 10 fdisk Commands to Manage Linux Disk Partitions
  2. 12 Useful “df” Commands to Check Disk Space in Linux

Source

Ways to Use ‘find’ Command to Search Directories More Efficiently

This tutorial will take you through the different ways of finding a directory in Linux. As you may already know, in Linux everything is a file including directories. And one of the common things a Linux user will do within the command line is searching for a file or a directory.

There are several different means and utilities used for searching for files on the command line such as findlocate and which. However, the last utility (which) is only used for locating a command.

For the scope of this tutorial, we will mainly focus on the find utility, which searches files on a live Linux filesystem and is more efficient and reliable as compared to locate.

The downside of locate is that it reads one or more databases created by updatedb, it does not search through a live filesystem. In addition, it does not as well offer flexibility regarding where to search from (starting point).

Below is the syntax for running locate command:

# locate [option] [search-pattern]

To demonstrate the disadvantage of locate, let us assume we are searching for a directory named pkg in the current working directory.

Note: In the command below, the option --basename or -b tells locate to only match the file (directory) basename (which is exactly pkg) but not the path (/path/to/pkg). Where \ is a globbing character, it disables the implicit replacement of pkg by *pkg*.

$ locate --basename '\pkg'

Find Directory Using locate Command

Find Directory Using locate Command

As you can see from the command output above, locate will search beginning from the root (/) directory, that is why other directories with the same name are matched.

Therefore, to deal with this issue, use find by following the simplified syntax below:

$ find starting-point options [expression]

Let us look at a few examples.

To search for the same directory (pkg) above, within the current working directory, run the following command, where the -name flag reads the expression which in this case is the directory basename.

$ find . -name "pkg"

If you encounter “Permission denied” errors, use sudo command like so:

$ sudo find . -name "pkg"

Search a Directory Using find Command

Search a Directory Using find Command

You can prevent find from searching for other file types except directories by using -type flag to specify the type of file (in the command below d means directory) as follows:

$ sudo find . -type d -name "pkg"

Furthermore, if you wish to list the directory in a long listing format, employ the action switch -ls:

$ sudo find . -type d -name "pkg" -ls

Find and List Directory

Find and List Directory

Next, the option -iname will enable a case insensitive search:

$ sudo find . -type d -iname "pkg" 
$ sudo find . -type d -iname "PKG" 

Find Directory with Case Sensitive

Find Directory with Case Sensitive

To find more interesting and advanced usage information, read the man pages of find and locate.

$ man find
$ man locate

As a last remark, the find command is more reliable and efficient for searching files ( or directories) in a Linux system when weighed against the locate command.

Source

4 Ways to Batch Convert Your PNG to JPG and Vice-Versa

In computing, Batch processing is the execution of a series of tasks in a program non-interactively. In this guide will offer you 4 simple ways to batch convert several .PNG images to .JPG and vice-versa using Linux command-line tools.

We will use convert command line tool in all the examples, however, you can as well make use of mogrify to achieve this.

The syntax for using convert is:

$ convert input-option input-file output-option output-file

And for mogrify is:

$ mogrify options input-file

Note: With mogrify, the original image file is replaced with the new image file by default, but it is possible to prevent this, by using certain options that you can find in the man page.

Below are the various ways to batch convert your all .PNG images to .JPG format, if you want to convert .JPG to .PNG, you can modify the commands according to your needs.

1. Convert PNG to JPG Using ‘ls’ and ‘xargs’ Commands

The ls command allows you to list all your png images and xargs make it possible to build and execute a convert command from standard input to convert all .png images to .jpg.

----------- Convert PNG to JPG ----------- 
$ ls -1 *.png | xargs -n 1 bash -c 'convert "$0" "${0%.png}.jpg"'

----------- Convert JPG to PNG ----------- 
$ ls -1 *.jpg | xargs -n 1 bash -c 'convert "$0" "${0%.jpg}.png"'

Explanation about the options used in the above command.

  1. -1 – flag tells ls to list one image per line.
  2. -n – specifies the maximum number of arguments, which is 1 for the case.
  3. -c – instructs bash to run the given command.
  4. ${0%.png}.jpg – sets the name of the new converted image, the % sign helps to remove the old file extension.

Convert PNG to JPG Format in Linux

Convert PNG to JPG Format in Linux

I used ls -ltr command to list all files by modified date and time.

Similarly, you can use above command to convert all your .jpg images to .png by tweaking the above command.

2. Convert PNG to JPG Using GNU ‘Parallel’ Command

GNU Parallel enables a user to build and execute shell commands from standard input in parallel. Make sure you have GNU Parallel installed on your system, otherwise install it using the appropriate commands below:

$ sudo apt-get install parallel     [On Debian/Ubuntu systems]
$ sudo yum install parallel         [On RHEL/CentOS and Fedora]

Once Parallel utility installed, you can run the following command to convert all .png images to .jpg format from the standard input.

----------- Convert PNG to JPG ----------- 
$ parallel convert '{}' '{.}.jpg' ::: *.png

----------- Convert JPG to PNG -----------
$ parallel convert '{}' '{.}.png' ::: *.jpg

Where,

  1. {} – input line which is a replacement string substituted by a complete line read from the input source.
  2. {.} – input line minus extension.
  3. ::: – specifies input source, that is the command line for the example above where *png or *jpg is the argument.

Parallel Command - Converts All PNG Images to JPG Format

Parallel Command – Converts All PNG Images to JPG Format

Alternatively, you can as well use ls and parallel commands together to batch convert all your images as shown:

----------- Convert PNG to JPG ----------- 
$ ls -1 *.png | parallel convert '{}' '{.}.jpg'

----------- Convert JPG to PNG -----------
$ ls -1 *.jpg | parallel convert '{}' '{.}.png'

3. Convert PNG to JPG Using ‘for loop’ Command

To avoid the hustle of writing a shell script, you can execute a for loop from the command line as follows:

----------- Convert PNG to JPG ----------- 
$ bash -c 'for image in *.png; do convert "$image" "${image%.png}.jpg"; echo “image $image converted to ${image%.png}.jpg ”; done'

----------- Convert JPG to PNG -----------
$ bash -c 'for image in *.jpg; do convert "$image" "${image%.jpg}.png"; echo “image $image converted to ${image%.jpg}.png ”; done'

Description of each option used in the above command:

  1. -c allows for execution of the for loop statement in single quotes.
  2. The image variable is a counter for number of images in the directory.
  3. For each conversion operation, the echo command informs the user that a png image has been converted to jpg format and vice-versa in the line $image converted to ${image%.png}.jpg”.
  4. “${image%.png}.jpg” creates the name of the converted image, where % removes the extension of the old image format.

for loop - Convert PNG to JPG Format

for loop – Convert PNG to JPG Format

4. Convert PNG to JPG Using Shell Script

If you do not want to make your command line dirty as in the previous example, write a small script like so:

Note: Appropriately interchange the .png and .jpg extensions as in the example below for conversion from one format to another.

#!/bin/bash
#convert
for image in *.png; do
        convert  "$image"  "${image%.png}.jpg"
        echo “image $image converted to ${image%.png}.jpg ”
done
exit 0 

Save it as convert.sh and make the script executable and then run it from within the directory that has your images.

$ chmod +x convert.sh
$ ./convert.sh

Batch Image Convert Using Shell Script

Batch Image Convert Using Shell Script

In summary, we covered some important ways to batch convert .png images to .jpg format and vice-versa. If you want to optimize images, you can go through our guide that shows how to compress png and jpg images in Linux.

You can as well share with us any other methods including Linux command line tools for converting images from one format to another on the terminal, or ask a question via the comment section below.

Source

How to Recover a Deleted File in Linux

Did this ever happen to you? You realized that you had mistakenly deleted a file – either through the Del key, or using rm in the command line.

In the first case, you can always go to the Trashsearch for the file, and restore it to its original location. But what about the second case? As I am sure you probably know, the Linux command line does not send removed files anywhere – it REMOVES them. Bum. They’re gone.

Suggested Read: How to Recover Deleted Files/Directories Using Scalpel Tool

In this article we will share a tip that may be helpful to prevent this from happening to you, and a tool that you may consider using if at any point you are careless enough to do it anyway.

Create an alias to ‘rm -i’

The -i switch, when used with rm (and also other file-manipulation tools such as cp or mv) causes a prompt to appear before removing a file.

The same applies to copying, moving, or renaming a file in a location where one with the same name exists already.

This prompt gives you a second chance to consider if you actually want to remove the file – if you confirm the prompt, it will be gone. In that case, I’m sorry but this tip will not protect you from your own carelessness.

To replace rm with an alias to 'rm -i', do:

alias rm='rm -i'

The alias command will confirm that rm is now aliased:

Add Alias rm Command

Add Alias rm Command

However, this will only last during the current user session in the current shell. To make the change permanent, you will have to save it to ~/.bashrc (some distributions may use ~/.profile instead) as shown below:

Add Alias Permanently in Linux

Add Alias Permanently in Linux

In order for the changes in ~/.bashrc (or ~/.profile) to take effect immediately, source the file from the current shell:

. ~/.bashrc

Active Alias in Linux

Active Alias in Linux

The forensics tool – Foremost

Hopefully, you will be careful with your files and will only need to use this tool while recovering a lost file from an external disk or USB drive.

However, if you realize you accidentally removed a file in your system and are going to panic – don’t. Let’s take a look at foremost, a forensics tool that was designed for this kind of scenarios.

To install foremost in CentOS/RHEL 7, you will need to enable Repoforge first:

# rpm -Uvh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
# yum install foremost

Whereas in Debian and derivatives, just do

# aptitude install foremost

Once the installation has completed, let’s proceed with a simple test. We will begin by removing an image file named nosdos.jpg from the /boot/images directory:

# cd images
# rm nosdos.jpg

To recover it, use foremost as follows (you’ll need to identify the underlying partition first – /dev/sda1 is where /boot resides in this case):

# foremost -t jpg -i /dev/sda1 -o /home/gacanepa/rescued

where /home/gacanepa/rescued is a directory on a separate disk – keep in mind that recovering files on the same drive where the removed ones were located is not a wise move.

If, during the recovery, you occupy the same disk sectors where the removed files used to be, it may not be possible to recover anything. Additionally, it is essential to stop all your activities before performing the recovery.

After foremost has finished executing, the recovered file (if recovery was possible) will be found inside the /home/gacanepa/rescued/jpg directory.

Summary

In this article we have explained how to avoid removing a file accidentally and how to attempt to recover it if such an undesired event happens. Be warned, however, that foremost can take quite a while to run depending on the size of the partition.

As always, don’t hesitate to let us know if you have questions or comments. Feel free to drop us a note using the form below.

Source

10 Linux Dig (Domain Information Groper) Commands to Query DNS

In our last article we have showed you the most used 8 Nslookup commands with their examples, now here we come with another command line tool called Dig, which is much similar to Linux Nslookup tool. We will see the usage of dig command closely with their examples as shown below.

Linux Dig Command Examples

Linux Dig Command Examples

Dig stands for (Domain Information Groper) is a network administration command-line tool for querying Domain Name System (DNS) name servers. It is useful for verifying and troubleshooting DNS problems and also to perform DNS lookups and displays the answers that are returned from the name server that were queried. dig is part of the BIND domain name server software suite. dig command replaces older tool such as nslookup and the host. dig tool is available in major Linux distributions.

1. Query Domain “A” Record

# dig yahoo.com; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6_3.2 <<>> yahoo.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<

Above command causes dig to look up the “A” record for the domain name yahoo.com. Dig command reads the /etc/resolv.conf file and querying the DNS servers listed there. The response from the DNS server is what dig displays.

Let us understand the output of the commands:
  1. Lines beginning with ; are comments not part of the information.
  2. The first line tell us the version of dig (9.8.2) command.
  3. Next, dig shows the header of the response it received from the DNS server
  4. Next comes the question section, which simply tells us the query, which in this case is a query for the “A”record of yahoo.com. The IN means this is an Internet lookup (in the Internet class).
  5. The answer section tells us that yahoo.com has the IP address 72.30.38.140
  6. Lastly there are some stats about the query. You can turn off these stats using the +nostats option.

2. Query Domain “A” Record with +short

By default dig is quite verbose. One way to cut down the output is to use the +short option. which will drastically cut the output as shown below.

# dig yahoo.com +short

98.139.183.24
72.30.38.140
98.138.253.109

Note: By default dig looks for the “A” record of the domain specified, but you can specify other records also. The MX or Mail eXchange record tells mail servers how to route the email for the domain. Likewise TTLSOA etc.

3. Querying MX Record for Domain

Querying different types of DNS resource records only.

# dig yahoo.com MX

; <> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6_3.2 <> yahoo.com MX
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31450
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 24

;; QUESTION SECTION:
;yahoo.com.                     IN      MX

;; ANSWER SECTION:
yahoo.com.              33      IN      MX      1 mta6.am0.yahoodns.net.
yahoo.com.              33      IN      MX      1 mta7.am0.yahoodns.net.
yahoo.com.              33      IN      MX      1 mta5.am0.yahoodns.net.

4. Querying SOA Record for Domain

# dig yahoo.com SOA

; <> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6_3.2 <> yahoo.com SOA
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2197
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 7, ADDITIONAL: 7

;; QUESTION SECTION:
;yahoo.com.                     IN      SOA

;; ANSWER SECTION:
yahoo.com.              1800    IN      SOA     ns1.yahoo.com. hostmaster.yahoo-inc.com. 2012081409 3600 300 1814400 600

5. Querying TTL Record for Domain

# dig yahoo.com TTL

; <> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6_3.2 <> yahoo.com TTL
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56156
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;yahoo.com.                     IN      A

;; ANSWER SECTION:
yahoo.com.              3589    IN      A       98.138.253.109
yahoo.com.              3589    IN      A       98.139.183.24
yahoo.com.              3589    IN      A       72.30.38.140

6. Querying only answer section

# dig yahoo.com +nocomments +noquestion +noauthority +noadditional +nostats

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6 <<>> yahoo.com +nocomments +noquestion +noauthority +noadditional +nostats
;; global options: +cmd
yahoo.com.              3442    IN      A       72.30.38.140
yahoo.com.              3442    IN      A       98.138.253.109
yahoo.com.              3442    IN      A       98.139.183.24

7. Querying ALL DNS Records Types

# dig yahoo.com ANY +noall +answer

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6 <<>> yahoo.com ANY +noall +answer
;; global options: +cmd
yahoo.com.              3509    IN      A       72.30.38.140
yahoo.com.              3509    IN      A       98.138.253.109
yahoo.com.              3509    IN      A       98.139.183.24
yahoo.com.              1709    IN      MX      1 mta5.am0.yahoodns.net.
yahoo.com.              1709    IN      MX      1 mta6.am0.yahoodns.net.
yahoo.com.              1709    IN      MX      1 mta7.am0.yahoodns.net.
yahoo.com.              43109   IN      NS      ns2.yahoo.com.
yahoo.com.              43109   IN      NS      ns8.yahoo.com.
yahoo.com.              43109   IN      NS      ns3.yahoo.com.
yahoo.com.              43109   IN      NS      ns1.yahoo.com.
yahoo.com.              43109   IN      NS      ns4.yahoo.com.
yahoo.com.              43109   IN      NS      ns5.yahoo.com.
yahoo.com.              43109   IN      NS      ns6.yahoo.com.

8. DNS Reverse Look-up

Querying DNS Reverse Look-up. Only display answer section with using +short.

# dig -x 72.30.38.140 +short

ir1.fp.vip.sp2.yahoo.com.

9. Querying Multiple DNS Records

Query multiple website’s DNS specific query viz. MXNS etc. records.

# dig yahoo.com mx +noall +answer redhat.com ns +noall +answer

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6 <<>> yahoo.com mx +noall +answer redhat.com ns +noall +answer
;; global options: +cmd
yahoo.com.              1740    IN      MX      1 mta6.am0.yahoodns.net.
yahoo.com.              1740    IN      MX      1 mta7.am0.yahoodns.net.
yahoo.com.              1740    IN      MX      1 mta5.am0.yahoodns.net.
redhat.com.             132     IN      NS      ns1.redhat.com.
redhat.com.             132     IN      NS      ns4.redhat.com.
redhat.com.             132     IN      NS      ns3.redhat.com.
redhat.com.             132     IN      NS      ns2.redhat.com.

10. Create .digrc file

Create .digrc file under $HOME/.digrc to store default dig options.

# dig yahoo.com
yahoo.com.              3427    IN      A       72.30.38.140
yahoo.com.              3427    IN      A       98.138.253.109
yahoo.com.              3427    IN      A       98.139.183.24

We have store +noall +answer options permanently in .digrc file under user’s home directory. Now, whenever dig command execute it will show only answer section of dig output. No Need to type every-time options like +noall+answer.

In this article, we tried to find out dig command which may help you to search (DNS) Domain Name Service related information. Share your thoughts through comment box.

Source

WP2Social Auto Publish Powered By : XYZScripts.com