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]

Advertisement

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 ../../../../..”

%d bloggers like this: