17 April 2013

Navigate faster with these CD Command Tricks

2 comments

If you are a Terminal freak or use terminal to do most of your task, then it is obvious that you are frequently using cd Command to navigate from one directory to another. Here are some cd tricks which helps you to navigate much more faster and will boost your productivity.

Trick 1:

Move to your home directory from anywhere: cd (without any argument) and cd ~ (Tilda) commands are used to move to user's home directory from any directory in the file system.

$ pwd
/etc/apache2
$ cd
$ pwd
/home/sandeep

$ pwd
/etc/apache
$ cd ~
$ pwd
/home/sandeep

Trick 2:

Toggle between last two directories: cd - (minus) command is used to toggle between last two visited directories.

$ pwd
/home/sandeep
$ cd /etc/apache2
$ pwd
/etc/apache2

$ cd -
$ pwd
/home/sandeep
$ cd -
$ pwd
/etc/apache2

Trick 3:

Move to parent directory: cd .. (Dot Dot) is used to move to the parent directory of the current directory.

$ pwd
/home/sandeep
$ cd ..
$ pwd
/home

$ cd /etc/apache2
$ pwd
/etc/apache2
$ cd ../..
$ pwdcd *
/

Trick 4:

cd * : This command can be useful if first file is a directory in your current working directory.

$ ls -l
drwxr-xr-x 5 rajkumar rajkumar 4096 Jan 20 14:54 bootstrap
drwxrwxr-x 7 rajkumar rajkumar 4096 Mar 29 13:15 Calibre Library

$ cd *
$ pwd
/home/sandeep/bootstrap

Note: If first file is not a directory then this command doesn't work.

Trick 5:

cd !$ : Sometimes when you create a new directory, you may cd to the new directory immediately to perform some work. You have to use directory name with cd command to move to that directory, this can be sometime tedious if you have to move deep inside the directory. Above command helps you to move faster to the newly created directory.

$ mkdir sandeep
$ cd !$
cd sandeep
$ pwd
/home/sandeep/sandeep

$ mkdir very/long/directory/structure
$ cd !$
very/long/directory/structure
$ pwd
/home/sandeep/very/long/directory/structure

Trick 6:

Consider a scenario If you are frequently moving to subdirectories of a specific parent directory like/var/www, you can set the CDPATH to the parent directory and perform cd to the subdirectories without giving the parent directory path.

$ pwd
/home/sandeep
$ ls -l /var/www
drwxrwxr-x 3 rajkumar rajkumar 4096 Dec 25 18:52 antipattern
drwxrwxr-x 3 rajkumar rajkumar 4096 Mar 1 23:40 Backup
drwxr-xr-x 8 rajkumar rajkumar 4096 Jan 28 22:30 CI
drwxr-xr-x 6 rajkumar rajkumar 4096 Sep 28 2012 CodeIgniter_2.1.2

$ CDPATH=/var/www
$ cd Backup
/var/www/Backup

$ pwd
/var/www/Backup

Note: To make this change permanent add CDPATH=/var/www to your ~/.bash_profile.

That's it.
Hope you Enjoyed the Post.


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

2 comments:

  1. Thank you great tips! Especially the switching with cd - . I didn't know that. Dirstack pushd popd dirs plus this is extremely useful!

    ReplyDelete