Category Archives: Daily Hacks

Hack #18 -> Diff Command

Diff command compares two different files and reports the difference. The output of the diff command is very cryptic and not straight forward to read. Syntax: diff [options] file1 file2   What was modified in my new file when compare to my old file?   The option -w in the diff command will ignore the white space while performing the

Read more

Hack #17 -> Stat Command

Stat command can be used either to check the status/properties of a single file or the filesystem. Display statistics of a file or directory.   $ stat /etc/my.cnf File: `/etc/my.cnf’ Size: 346 Blocks: 16 IO Block: 4096 regular file Device: 801h/2049d Inode: 279856 Links: 1 Access: (0644/-rw-r–r–) Uid: (0/root) Gid: (0/root) Access: 2009-01-01 02:58:30.000000000 -0800 Modify: 2006-06-01 20:42:27.000000000 -0700 Change:

Read more

Hack #16 -> Cut Command

Cut command can be used to display only specific columns from a text file or other command outputs. The following are some of the examples. Display the 1st field (employee name) from a colon delimited file $ cut -d: -f 1 names.txt Emma Thomas Alex Jason Madison Randy Sanjay Gupta Nisha Singh Display 1st and 3rd field from a colon

Read more

Hack #15 -> Uniq Command

Uniq command is mostly used in combination with sort command, as uniq removes duplicates only from a sorted file. i.e In order for uniq to work, all the duplicate entries should be in the adjacent lines. The following are some common examples. When you have an employee file with duplicate entries, you can do the following to remove duplicates. $

Read more

Hack #14 -> Sort Command

Sort command sorts the lines of a text file. Following are several practical examples on how to use the sort command based on the following sample text file that has employee information in the format: employee_name:employee_id:department_name. $ cat names.txt Emma Thomas:100:Marketing Alex Jason:200:Sales Madison Randy:300:Product Development Sanjay Gupta:400:Support Nisha Singh:500:Sales Sort a text file in ascending order $ sort names.txt

Read more

Hack #13 -> Xargs Command

Xargs is a very powerful command that takes output of a command and pass it as argument of another command. The following are some practical examples on how to use xargs effectively. When you are trying to delete too many files using rm, you may get error message: /bin/rm Argument list too long – Linux. Use xargs to avoid this

Read more

Hack #12 -> Change the Case

Convert a file to all upper-case $ cat employee.txt 100 Jason Smith 200 John Doe 300 Sanjay Gupta 400 Ashok Sharma $ tr a-z A-Z < employee.txt 100 JASON SMITH 200 JOHN DOE 300 SANJAY GUPTA 400 ASHOK SHARMA Convert a file to all lower-case $ cat department.txt 100 FINANCE 200 MARKETING 300 PRODUCT DEVELOPMENT 400 SALES $ tr A-Z

Read more

Hack #11-> Join Command

Join command combines lines from two files based on a common field. In the example below, we have two files – employee.txt and salary.txt. Both have employee-id as common field. So, we can use join command to combine the data from these two files using employee-id as shown below. $ cat employee.txt 100 Jason Smith 200 John Doe 300 Sanjay

Read more

Hack #9-> Find Command

Find is frequently used command to find files in the UNIX filesystem based on numerous conditions. Let us review some practice examples of find command. Syntax: find [pathnames] [conditions] How to find files containing a specific word in its name? The following command looks for all the files under /etc directory with mail in the filename. # find /etc -name

Read more

Hack #7-> Grep Command

Grep command is used to search files for a specific text. This is incredibly powerful command with lots of options. Syntax: grep [options] pattern [files] How can I find all lines matching a specific keyword on a file? In this example, grep looks for the text John inside /etc/passwd file and displays all the matching lines. # grep John /etc/passwd

Read more

Hack #3-> Perform mkdir and cd Using a Single Command

Sometimes when you create a new directory, you may cd to the new directory immediately to perform some work as shown below. # mkdir -p /tmp/subdir1/subdir2/subdir3 # cd /tmp/subdir1/subdir2/subdir3 # pwd /tmp/subdir1/subdir2/subdir3 Wouldn’t it be nice to combine both mkdir and cd in a single command? Add the following to the .bash_profile and re-login. # vi .bash_profile function mkdircd ()

Read more

Hack #2 -> Use CD Alias to Navigate Up the Directory

When you are navigating up a very long directory structure, you may be using cd ..\..\ with multiple ..\’s depending on how many directories you want to go up as shown below. # mkdir -p /tmp/very/long/directory/structure/that/is/too/deep # cd /tmp/very/long/directory/structure/that/is/too/deep # pwd /tmp/very/long/directory/structure/that/is/too/deep # cd ../../../../ # pwd /tmp/very/long/directory/structure Instead of executing cd ../../../.. to navigate four levels up, use one

Read more

Hack #1 -> Define CD Base Directory Using CDPATH

If you are frequently performing cd to subdirectories of a specific parent directory, you can set the CDPATH to the parent directory and perform cd to the subdirectories without giving the parent directory path as explained below. # pwd /home/ramesh # cd mail -bash: cd: mail: No such file or directory [Note: The above cd is looking for mail directory

Read more