Hack #21 -> Sed Basics – Find and Replace Using RegEx

This hack explains how to use sed substitute command “s”.

The `s’ command is probably the most important in `sed’ and has a lot

of different options.

The `s’ command attempts to match the pattern space against the

supplied REGEXP; if the match is successful, then that portion of the

pattern space which was matched is replaced with REPLACEMENT.

Syntax:

 

#sed ‘ADDRESSs/REGEXP/REPLACEMENT/FLAGS’ filename

#sed ‘PATTERNs/REGEXP/REPLACEMENT/FLAGS’ filename

 

  • s is substitute command
  • / is a delimiter
  • REGEXP is regular expression to match
  • REPLACEMENT is a value to replace

FLAGS can be any of the following :

  • g Replace all the instance of REGEXP with REPLACEMENT
  • n Could be any number,replace nth instance of the REGEXP with

REPLACEMENT.

  • p If substitution was made, then prints the new pattern space.
  • i match REGEXP in a case-insensitive manner.
  • w file If substitution was made, write out the result to the given

file.

  • We can use different delimiters ( one of @ % ; : ) instead of /

Let us first create thegeekstuff.txt file that will be used in all the

examples mentioned below.

 

$ cat thegeekstuff.txt

 

# Instruction Guides

  1. Linux Sysadmin, Linux Scripting etc.
  2. Databases – Oracle, mySQL etc.
  3. Security (Firewall, Network, Online Security etc)
  4. Storage in Linux
  5. Productivity (Too many technologies to explore, not

much time available)

# Additional FAQS

  1. Windows- Sysadmin, reboot etc.

 

Substitute Word “Linux” to “Linux-Unix” Using sed s//

In the example below, in the output line “1. Linux-Unix Sysadmin, Linux

Scripting etc” only first Linux is replaced by Linux-Unix. If no flags are

specified the first match of line is replaced.

 

$ sed ‘s/Linux/Linux-Unix/’ thegeekstuff.txt

 

# Instruction Guides

  1. Linux-Unix Sysadmin, Linux Scripting etc.
  2. Databases – Oracle, mySQL etc.
  3. Security (Firewall, Network, Online Security etc)
  4. Storage in Linux-Unix
  5. Productivity (Too many technologies to explore, not

much time available)

# Additional FAQS

  1. Windows- Sysadmin, reboot etc.

 

Substitute all Appearances of a Word Using sed s//g

The below sed command replaces all occurrences of Linux to Linux-Unix

using global substitution flag “g”.

 

$ sed ‘s/Linux/Linux-Unix/g’ thegeekstuff.txt

 

# Instruction Guides

  1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.
  2. Databases – Oracle, mySQL etc.
  3. Security (Firewall, Network, Online Security etc)
  4. Storage in Linux-Unix
  5. Productivity (Too many technologies to explore, not

much time available)

# Additional FAQS

  1. Windows- Sysadmin, reboot etc.

 

Substitute Only 2nd Occurrence of a Word Using sed s//2

In the example below, in the output line “1. Linux Sysadmin, Linux-Unix

Scripting etc.” only 2nd occurrence of Linux is replaced by Linux-Unix.

 

$ sed ‘s/Linux/Linux-Unix/2’ thegeekstuff.txt

 

# Instruction Guides

  1. Linux Sysadmin, Linux-Unix Scripting etc.
  2. Databases – Oracle, mySQL etc.
  3. Security (Firewall, Network, Online Security etc)
  4. Storage in Linux
  5. Productivity (Too many technologies to explore, not

much time available)

# Additional FAQS

  1. Windows- Sysadmin, reboot etc.
  2. Write Changes to a File and Print the Changes Using sed

s//gpw

The example below has substitution with three flags. It substitutes all

the occurrence of Linux to Linux-Unix and prints the substituted output

as well as written the same to the given the file.

 

$ sed -n ‘s/Linux/Linux-Unix/gpw output’ thegeekstuff.txt

 

  1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.
  2. Storage in Linux-Unix

 

$ cat output

 

  1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.
  2. Storage in Linux-Unix
  3. Substitute Only When the Line Matches with the Pattern

Using sed

In this example, if the line matches with the pattern “-”, then it replaces

all the characters from “-” with the empty.

 

$ sed ‘/\-/s/\-.*//g’ thegeekstuff.txt

 

# Instruction Guides

  1. Linux Sysadmin, Linux Scripting etc.
  2. Databases
  3. Security (Firewall, Network, Online Security etc)
  4. Storage in Linux
  5. Productivity (Too many technologies to explore, not

much time available)

# Additional FAQS

  1. Windows

 

Delete Last X Number of Characters From Each Line Using

sed

 

This sed example deletes last 3 characters from each line.

$ sed ‘s/…$//’ thegeekstuff.txt

 

# Instruction Gui

  1. Linux Sysadmin, Linux Scripting e
  2. Databases – Oracle, mySQL e
  3. Security (Firewall, Network, Online Security e
  4. Storage in Li
  5. Productivity (Too many technologies to explore, not

much time availab

# Additional F

  1. Windows- Sysadmin, reboot e

 

Eliminate Comments Using sed

Delete all the comment lines from a file as shown below using sed

command.

$ sed -e ‘s/#.*//’ thegeekstuff.txt

 

  1. Linux Sysadmin, Linux Scripting etc.
  2. Databases – Oracle, mySQL etc.
  3. Security (Firewall, Network, Online Security etc)
  4. Storage in Linux
  5. Productivity (Too many technologies to explore, not

much time available)

  1. Windows- Sysadmin, reboot etc.
  2. Eliminate Comments and Empty Lines Using sed

 

In the following example, there are two commands separated by ‘;’

First command replaces the lines starting with the # to the blank lines

Second command deletes the empty lines.

 

$ sed -e ‘s/#.*//;/^$/d’ thegeekstuff.txt

 

  1. Linux Sysadmin, Linux Scripting etc.
  2. Databases – Oracle, mySQL etc.
  3. Security (Firewall, Network, Online Security etc)
  4. Storage in Linux
  5. Productivity (Too many technologies to explore, not

much time available)

  1. Windows- Sysadmin, reboot etc.
  2. Convert DOS newlines (CR/LF) to Unix format Using sed

 

Eliminate HTML Tags from file Using sed

 

In this example, the regular expression given in the sed command

matches the html tags and replaces with the empty.

$ sed -e ‘s/<[^>]*>//g’

 

This <b> is </b> an <i>example</i>.

This is an example

Advertisements

Hack #20 -> Execute Commands in the Background

You can use one of the 5 methods explained in this hack to execute a

Linux command, or shell script in the background.

 

Method 1. Use &

You can execute a command (or shell script) as a background job by

appending an ampersand to the command as shown below.

 

$ ./my-shell-script.sh &

 

Method 2. Nohup

After you execute a command (or shell script) in the background using

&, if you logout from the session, the command will get killed. To avoid

that, you should use nohup as shown below.

 

$ nohup ./my-shell-script.sh &

 

Method 3. Screen Command

After you execute a command in the background using nohup and &, the

command will get executed even after you logout. But, you cannot

connect to the same session again to see exactly what is happening on

the screen. To do that, you should use screen command.

Linux screen command offers the ability to detach a session that is

running some process, and then attach it at a later time. When you

reattach the session later, your terminals will be there exactly in the way

you left them earlier.

 

Method 4. At Command

Using at command you can schedule a job to run at a particular date

and time. For example, to execute the backup script at 10 a.m

tomorrow, do the following.

 

$ at -f backup.sh 10 am tomorrow

 

Method 5. Watch Command

To execute a command continuously at a certain interval, use watch

command as shown below.

 

$ watch df -h

Hack #19 -> Display Total Connect Time of Users

Ac command will display the statistics about the user’s connect time.

 

Connect time for the current logged in user

With the option –d, it will break down the output for the individual days.

In this example, I’ve been logged in to the system for more than 6 hours

today. On Dec 1st, I was logged in for about 1 hour.

$ ac –d

Dec 1 total 1.08

Dec 2 total 0.99

Dec 3 total 3.39

Dec 4 total 4.50

Today total 6.10

 

Connect time for all the users

To display connect time for all the users use –p as shown below. Please

note that this indicates the cumulative connect time for the individual

users.

$ ac -p

john 3.64

madison 0.06

sanjay 88.17

nisha 105.92

ramesh 111.42

total 309.21

 

Connect time for a specific user

To get a connect time report for a specific user, execute the following:

 

$ ac -d sanjay

Jul 2 total 12.85

Aug 25 total 5.05

Sep 3 total 1.03

Sep 4 total 5.37

Dec 24 total 8.15

Dec 29 total 1.42

Today total 2.95

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 comparison.

In the following diff output:

  • The lines above —, indicates the changes happened in first file in

the diff command (i.e name_list.txt).

  • The lines below —, indicates the changes happened to the

second file in the diff command (i.e name_list_new.txt). The lines

that belong to the first file starts with < and the lines of second

file starts with >.

 

# diff -w name_list.txt name_list_new.txt

2c2,3

< John Doe

> John M Doe

> Jason Bourne

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: 2007-02-02 14:17:27.000000000 -0800

$ stat /home/ramesh

File: `/home/ramesh’

Size: 4096 Blocks: 8 IO Block: 4096

directory

Device: 803h/2051d Inode: 5521409 Links: 7

Access: (0755/drwxr-xr-x) Uid: (401/ramesh) Gid:

(401/ramesh)

Access: 2009-01-01 12:17:42.000000000 -0800

Modify: 2009-01-01 12:07:33.000000000 -0800

Change: 2009-01-09 12:07:33.000000000 -0800

 

Display the status of the filesystem using option –f

 

$ stat -f /

File: “/”

ID: 0 Namelen: 255 Type: ext2/ext3

Blocks: Total: 2579457 Free: 2008027 Available:

1876998 Size: 4096

Inodes: Total: 1310720 Free: 1215892

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 delimited file

$ cut -d: -f 1,3 names.txt

Emma Thomas:Marketing

Alex Jason:Sales

Madison Randy:Product Development

Sanjay Gupta:Support

Nisha Singh:Sales

Display only the first 8 characters of every line in a file

$ cut -c 1-8 names.txt

Emma Tho

Alex Jas

Madison

Sanjay G

Nisha Si

Misc Cut command examples

  • cut -d: -f1 /etc/passwd Displays the unix login names for all the

users in the system.

  • free | tr -s ‘ ‘ | sed ‘/^Mem/!d’ | cut -d” ” -f2 Displays the

total memory available on the system.

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.

  1. When you have an employee file with duplicate entries, you can do

the following to remove duplicates.

$ sort namesd.txt | uniq

$ sort –u namesd.txt

  1. If you want to know how many lines are duplicates, do the following.

The first field in the following examples indicates how many duplicates

where found for that particular line. So, in this example the lines

beginning with Alex and Emma were found twice in the namesd.txt file.

$ sort namesd.txt | uniq –c

2 Alex Jason:200:Sales

2 Emma Thomas:100:Marketing

1 Madison Randy:300:Product Development

1 Nisha Singh:500:Sales

1 Sanjay Gupta:400:Support

  1. The following displays only the entries that are duplicates.

$ sort namesd.txt | uniq –cd

2 Alex Jason:200:Sales

2 Emma Thomas:100:Marketing

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

Alex Jason:200:Sales

Emma Thomas:100:Marketing

Madison Randy:300:Product Development

Nisha Singh:500:Sales

Sanjay Gupta:400:Support

Sort a text file in descending order

$ sort -r names.txt

Sanjay Gupta:400:Support

Nisha Singh:500:Sales

Madison Randy:300:Product Development

Emma Thomas:100:Marketing

Alex Jason:200:Sales

Sort a colon delimited text file on 2nd field (employee_id)

$ sort -t: -k 2 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 tab delimited text file on 3rd field (department_name) and

suppress duplicates

$ sort -t: -u -k 3 names.txt

Emma Thomas:100:Marketing

Madison Randy:300:Product Development

Alex Jason:200:Sales

Sanjay Gupta:400:Support

Sort the passwd file by the 3rd field (numeric userid)

$ sort -t: -k 3n /etc/passwd | more

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

Sort /etc/hosts file by ip-address

$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts

127.0.0.1 localhost.localdomain localhost

192.168.100.101 dev-db.thegeekstuff.com dev-db

192.168.100.102 prod-db.thegeekstuff.com prod-db

192.168.101.20 dev-web.thegeekstuff.com dev-web

192.168.101.21 prod-web.thegeekstuff.com prod-web

Combine sort with other commands

  • ps –ef | sort : Sort the output of process list
  • ls -al | sort +4n : List the files in the ascending order of the filesize.

i.e sorted by 5th filed and displaying smallest files first.

  • ls -al | sort +4nr : List the files in the descending order of the

file-size. i.e sorted by 5th filed and displaying largest files first.

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.

  1. 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 problem.

find ~ -name ‘*.log’ -print0 | xargs -0 rm -f

  1. Get a list of all the *.conf file under /etc/. There are different ways to

get the same result. Following example is only to demonstrate the use of

xargs. The output of the find command in this example is passed to the

ls –l one by one using xargs.

# find /etc -name “*.conf” | xargs ls –l

  1. If you have a file with list of URLs that you would like to download, you

can use xargs as shown below.

# cat url-list.txt | xargs wget –c

  1. Find out all the jpg images and archive it.

 

# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

  1. Copy all the images to an external hard-drive.

# ls *.jpg | xargs -n1 -i cp {} /external-harddrive/directory

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 a-z < department.txt

100 finance

200 marketing

300 product development

400 sales

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 Gupta

400 Ashok Sharma

$ cat bonus.txt

100 $5,000

200 $500

300 $3,000

400 $1,250

$ join employee.txt bonus.txt

100 Jason Smith $5,000

200 John Doe $500

300 Sanjay Gupta $3,000

400 Ashok Sharma $1,250

Hack #10-> Suppress Standard Output and Error Message

Sometime while debugging a shell script, you may not want to see either

the standard output or standard error message. Use /dev/null as shown

below for suppressing the output.

Suppress standard output using > /dev/null

This will be very helpful when you are debugging shell scripts, where

you don’t want to display the echo statement and interested in only

looking at the error messages.

# cat file.txt > /dev/null

# ./shell-script.sh > /dev/null

Suppress standard error using 2> /dev/null

This is also helpful when you are interested in viewing only the standard

output and don’t want to view the error messages.

# cat invalid-file-name.txt 2> /dev/null

# ./shell-script.sh 2> /dev/null

Note: One of the most effective ways to use this is in the crontab,

where you can suppress the output and error message of a cron task as

shown below.

30 1 * * * command > /dev/null 2>&1

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 “*mail*”

How to find all the files greater than certain size?

The following command will list all the files in the system greater than

100MB.

# find / -type f -size +100M

How to find files that are not modified in the last x

number of days?

The following command will list all the files that were modified more

than 60 days ago under the current directory.

# find . -mtime +60

How to find files that are modified in the last x number of

days?

The following command will list all the files that were modified in the last

two days under the current directory.

# find . –mtime -2

How to delete all the archive files with extension *.tar.gz

and greater than 100MB?

Please be careful while executing the following command as you don’t

want to delete the files by mistake. The best practice is to execute the

same command with ls –l to make sure you know which files will get

deleted when you execute the command with rm.

# find / -type f -name *.tar.gz -size +100M -exec ls -l {}

\;

# find / -type f -name *.tar.gz -size +100M -exec rm -f {}

\;

How to archive all the files that are not modified in the

last x number of days?

The following command finds all the files not modified in the last 60

days under /home/jsmith directory and creates an archive files under

/tmp in the format of ddmmyyyy_archive.tar.

# find /home/jsmith -type f -mtime +60 | xargs tar -cvf

/tmp/date '+%d%m%Y'_archive.tar

Hack #8-> Regular Expression in Grep

Regular expressions are used to search and manipulate the text, based

on the patterns. Most of the Linux commands and programming

languages use regular expression.

This hack explains how to use most frequently used reg-ex operators in

Grep command.

Beginning of line ( ^ )

In grep command, caret Symbol ^ matches the expression at the start of

a line. In the following example, it displays all the line which starts with

the Nov 10. i.e All the messages logged on November 10.

$ grep “^Nov 10” messages.1

Nov 10 01:12:55 gs123 ntpd[2241]: time reset +0.177479 s

Nov 10 01:17:17 gs123 ntpd[2241]: synchronized to

LOCAL(0), stratum 10

Nov 10 01:18:49 gs123 ntpd[2241]: synchronized to

15.1.13.13, stratum 3

Nov 10 13:21:26 gs123 ntpd[2241]: time reset +0.146664 s

Nov 10 13:25:46 gs123 ntpd[2241]: synchronized to

LOCAL(0), stratum 10

Nov 10 13:26:27 gs123 ntpd[2241]: synchronized to

15.1.13.13, stratum 3

The ^ matches the expression in the beginning of a line, only if it is the

first character in a regular expression. ^N matches line beginning with

N.

End of the line ( $)

Character $ matches the expression at the end of a line. The following

command will help you to get all the lines which ends with the word

“terminating”.

$ grep “terminating.$” messages

Jul 12 17:01:09 cloneme kernel: Kernel log daemon

terminating.

Oct 28 06:29:54 cloneme kernel: Kernel log daemon

terminating.

From the above output you can come to know when all the kernel log

has got terminated. Just like ^ matches the beginning of the line only if

it is the first character, $ matches the end of the line only if it is the last

character in a regular expression.

Count of empty lines ( ^$ )

Using ^ and $ character you can find out the empty lines available in a

file. “^$” specifies empty line.

$ grep -c “^$” messages anaconda.log

messages:0

anaconda.log:3

The above commands displays the count of the empty lines available in

the messages and anaconda.log files.

Single Character (.)

The special meta-character “.” (dot) matches any character except the

end of the line character. Let us take the input file which has the content

as follows.

$ cat input

  1. first line
  2. hi hello
  3. hi zello how are you
  4. cello
  5. aello
  6. eello
  7. last line

Now let us search for a word which has any single character followed by

ello. i.e hello, cello etc.,

$ grep “.ello” input

  1. hi hello
  2. hi zello how are you
  3. cello
  4. aello
  5. eello

In case if you want to search for a word which has only 4 character you

can give grep -w “….” where single dot represents any single character.

Zero or more occurrence (*)

The special character “*” matches zero or more occurrence of the

previous character. For example, the pattern ’1*’ matches zero or more

’1′.

The following example searches for a pattern “kernel: *” i.e kernel: and

zero or more occurrence of space character.

$ grep “kernel: *.” *

messages.4:Jul 12 17:01:02 cloneme kernel: ACPI: PCI

interrupt for device 0000:00:11.0 disabled

messages.4:Oct 28 06:29:49 cloneme kernel: ACPI: PM-Timer

IO Port: 0x1008

messages.4:Oct 28 06:31:06 btovm871 kernel: sda: sda1

sda2 sda3

messages.4:Oct 28 06:31:06 btovm871 kernel: sd 0:0:0:0:

Attached scsi disk sda

.

.

In the above example it matches for kernel and colon symbol followed

by any number of spaces/no space and “.” matches any single

character.

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

jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash

jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash

Option -v, will display all the lines except the match. In the example

below, it displays all the records from /etc/password that doesn’t match

John.

Note: There are several lines in the /etc/password that doesn’t contain

the word John. Only the first line of the output is shown below.

# grep -v John /etc/passwd

jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash

How many lines matched the text pattern in a particular

file?

In the example below, it displays the total number of lines that contains

the text John in /etc/passwd file.

# grep -c John /etc/passwd

2

You can also get the total number of lines that did not match the specific

pattern by passing option -cv.

# grep -cv John /etc/passwd

How to search a text by ignoring the case?

Pass the option -i (ignore case), which will ignore the case while

searching.

# grep -i john /etc/passwd

jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash

jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash

How do I search all subdirectories for a text matching a

specific pattern?

Use option -r (recursive) for this purpose. In the example below, it will

search for the text “John” by ignoring the case inside all the

subdirectories under /home/users.

This will display the output in the format of “filename: line that matching

the pattern”. You can also pass the option -l, which will display only the

name of the file that matches the pattern.

# grep -ri john /home/users

/home/users/subdir1/letter.txt:John, Thanks for your

contribution.

/home/users/name_list.txt:John Smith

/home/users/name_list.txt:John Doe

# grep -ril john /root

/home/users/subdir1/letter.txt

/home/users/name_list.txt

Hack #6-> Automatically Correct Mistyped Directory Names

Use shopt -s cdspell to correct the typos in the cd command
automatically as shown below. If you are not good at typing and make
lot of mistakes, this will be very helpful.

# cd /etc/mall
-bash: cd: /etc/mall: No such file or directory
# shopt -s cdspell
# cd /etc/mall
# pwd
/etc/mail
[Note: By mistake, when I typed mall instead of mail, cd
corrected it automatically]

Hack #5-> Manipulate Directory Stack

You can use directory stack to push directories into it and later pop
directory from the stack. Following three commands are used in this
example.
• dirs: Display the directory stack
• pushd: Push directory into the stack
• popd: Pop directory from the stack and cd to it
Dirs will always print the current directory followed by the content of the
stack. Even when the directory stack is empty, dirs command will still
print only the current directory as shown below.

# popd
-bash: popd: directory stack empty
# dirs
~
# pwd
/home/ramesh

How to use pushd and popd? Let us first create some temporary
directories and push them to the directory stack as shown below.

# mkdir /tmp/dir1
# mkdir /tmp/dir2
# mkdir /tmp/dir3
# mkdir /tmp/dir4

# cd /tmp/dir1
# pushd .
# cd /tmp/dir2
# pushd .
# cd /tmp/dir3
# pushd .
# cd /tmp/dir4
# pushd .
# dirs
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
[Note: The first directory (/tmp/dir4) of the dir command
output is always the current directory and not the content
from the stack.]

At this stage, the directory stack contains the following directories:

/tmp/dir4
/tmp/dir3
/tmp/dir2
/tmp/dir1

The last directory that was pushed to the stack will be at the top. When
you perform popd, it will cd to the top directory entry in the stack and
remove it from the stack. As shown above, the last directory that was
pushed into the stack is /tmp/dir4. So, when we do a popd, it will cd to
the /tmp/dir4 and remove it from the directory stack as shown below.

# popd

# pwd
/tmp/dir4
[Note: After the above popd, directory Stack Contains:
/tmp/dir3
/tmp/dir2
/tmp/dir1]
# popd
# pwd
/tmp/dir3
[Note: After the above popd, directory Stack Contains:
/tmp/dir2
/tmp/dir1]
# popd
# pwd
/tmp/dir2
[Note: After the above popd, directory Stack Contains:
/tmp/dir1]
# popd
# pwd
/tmp/dir1
[Note: After the above popd, directory Stack is empty!]
# popd
-bash: popd: directory stack empty

Hack #4->Toggle Between Directories

You can toggle between the last two current directories using cd – as
shown below.

# cd /tmp/very/long/directory/structure/that/is/too/deep
# cd /tmp/subdir1/subdir2/subdir3
# cd –
# pwd
/tmp/very/long/directory/structure/that/is/too/deep
# cd –
# pwd
/tmp/subdir1/subdir2/subdir3

# cd –
# pwd
/tmp/very/long/directory/structure/that/is/too/deep

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 () { mkdir -p “$@” && eval cd “\”\$$#\””;
}

Now, perform both mkdir and cd at the same time using a single
command as shown below:

# mkdircd /tmp/subdir1/subdir2/subdir3
[Note: This creates the directory and cd to it
automatically]
# pwd
/tmp/subdir1/subdir2/subdir3

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 of the
following four alias methods:

Method 1: Navigate up the directory using “..n”

In the example below, ..4 is used to go up 4 directory level, ..3 to go up
3 directory level, ..2 to go up 2 directory level. Add the following alias to
your ~/.bash_profile and re-login.

alias ..=”cd ..”
alias ..2=”cd ../..”
alias ..3=”cd ../../..”
alias ..4=”cd ../../../..”
alias ..5=”cd ../../../../..”
# cd /tmp/very/long/directory/structure/that/is/too/deep
# ..4
[Note: use ..4 to go up 4 directory level]
# pwd
/tmp/very/long/directory/structure/

Method 2: Navigate up the directory using only dots

In the example below, ….. (five dots) is used to go up 4 directory level.
Typing 5 dots to go up 4 directory structure is really easy to remember,
as when you type the first two dots, you are thinking “going up one
directory”, after that every additional dot, is to go one level up.

So, use …. (four dots) to go up 3 directory level and .. (two dots) to go
up 1 directory level. Add the following alias to your ~/.bash_profile and
re-login for the ….. (five dots) to work properly.

alias ..=”cd ..”
alias …=”cd ../..”
alias ….=”cd ../../..”
alias …..=”cd ../../../..”
alias ……=”cd ../../../../..”
# cd /tmp/very/long/directory/structure/that/is/too/deep
# …..
[Note: use ….. (five dots) to go up 4 directory level]
# pwd
/tmp/very/long/directory/structure/

Method 3: Navigate up the directory using cd followed by
consecutive dots

In the example below, cd….. (cd followed by five dots) is used to go up
4 directory level. Making it 5 dots to go up 4 directory structure is really
easy to remember, as when you type the first two dots, you are thinking
“going up one directory”, after that every additional dot, is to go one
level up. So, use cd…. (cd followed by four dots) to go up 3 directory
level and cd… (cd followed by three dots) to go up 2 directory level. Add
the following alias to your ~/.bash_profile and re-login for the above
cd….. (five dots) to work properly.

alias cd..=”cd ..”
alias cd…=”cd ../..”
alias cd….=”cd ../../..”
alias cd…..=”cd ../../../..”
alias cd……=”cd ../../../../..”

# cd /tmp/very/long/directory/structure/that/is/too/deep
# cd…..
[Note: use cd….. to go up 4 directory level]
# pwd
/tmp/very/long/directory/structure

Method 4: Navigate up the directory using cd followed by
number

In the example below, cd4 (cd followed by number 4) is used to go up 4
directory level.

alias cd1=”cd ..”
alias cd2=”cd ../..”
alias cd3=”cd ../../..”
alias cd4=”cd ../../../..”
alias cd5=”cd ../../../../..”

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 under
current directory]

# export CDPATH=/etc
# cd mail
/etc/mail

[Note: The above cd is looking for mail under /etc and not
under current directory]

# pwd
/etc/mail

To make this change permanent, add

export CDPATH=/etc  to your ~/.bash_profile

Similar to the PATH variable, you can add more than one directory entry
in the CDPATH variable, separating them with : , as shown below.

export CDPATH=.:~:/etc:/var

This hack can be very helpful under the following situations:
• Oracle DBAs frequently working under $ORACLE_HOME, can set
the CDPATH variable to the oracle home
• Unix sysadmins frequently working under /etc, can set the
CDPATH variable to /etc
• Developers frequently working under project directory
/home/projects, can set the CDPATH variable to /home/projects
• End-users frequently accessing the subdirectories under their
home directory, can set the CDPATH variable to ~ (home
directory)