CD is the most used commands while operating with any Command line interface. Hence it is important to learn some hacks with cab increase our productivity.
The 5 cd command hacks mentioned in this blog will boost your productivity instantly and make it easier to navigate the directory structure from command line.
Hack 1. 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
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 this method:-
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/
Hack 3. Combining mkdir and cd
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 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
Hack 5. 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]
GREP COMMANDS
GREP command is used to search for a particular word in a line or in a file or if you searching for a file in. Particular directory
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 ketan /etc/passwd
kgrover:x:1082:1082:ketan Grover:/home/kgrover:/bin/bash
karora:x:1083:1083:ketan arora:/home/karora:/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 Ketan.
Note: There are several lines in the /etc/password that doesn’t contain the word Ketan. Only the first line of the output is shown below.
# grep -v Ketan /etc/passwd
dgrover:x:1084:1084:Dinesh Grover:/home/dgrover:/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 Ketan in /etc/passwd file.
# grep -c Ketan /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 Ketan /etc/passwd
39
How to search a text by ignoring the case?
Pass the option -i (ignore case), which will ignore the case while searching.
# grep -i ketan /etc/passwd
kgrover:x:1082:1082:Ketan Grover:/home/kgrover:/bin/bash
karora:x:1083:1083:Ketan Arora:/home/karora:/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 ketan /home/users /home/users/subdir1/letter.txt:Ketan, Thanks for your contribution. /home/users/name_list.txt:Ketan Grover /home/users/name_list.txt:Ketan Grover
# grep -ril john /root
That's a wrap....