Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
TetraWiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Linux 101 hacks
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
='''Chapter 1: Powerful CD Command Hacks'''= cd is one of the most frequently used commands during a UNIX session. The 6 cd command hacks mentioned in this chapter will boost your productivity instantly and make it easier to navigate the directory structure from command line. =='''Hack 1. Use CDPATH to define the base directory for cd command'''== 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. <nowiki>[ramesh@dev-db ~]# </nowiki>'''pwd''' /home/ramesh <nowiki>[ramesh@dev-db </nowiki>~]# '''cd mail''' -bash: cd: mail: No such file or directory <nowiki>[</nowiki>'''Note:''' This is looking for mail directory under current directory] <nowiki>[ramesh@dev-db ~]# </nowiki>'''export CDPATH=/etc'''<nowiki> [ramesh@dev-db ~]# </nowiki>'''cd mail''' /etc/mail <nowiki>[</nowiki>'''Note:''' This is looking for mail under /etc and not under current directory] <nowiki>[ramesh@dev-db /etc/mail]# </nowiki>'''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) =='''Hack 2. Use cd alias to navigate up the directory effectively'''== 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 ../../../../ <nowiki># pwd /tmp/very/long/directory/structure</nowiki> Instead of executing cd ../../../.. to navigate four levels up, use one of the following three 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 ../../../../.." <nowiki># cd /tmp/very/long/directory/structure/that/is/too/deep</nowiki> <nowiki># </nowiki>..4 <nowiki>[</nowiki>'''Note:''' use ..4 to go up 4 directory level] <nowiki># pwd /tmp/very/long/directory/structure/</nowiki> '''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 # ..... <nowiki>[</nowiki>'''Note:''' use ..... (five dots) to go up 4 directory level] <nowiki># pwd /tmp/very/long/directory/structure/</nowiki> '''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..... <nowiki>[</nowiki>'''Note:''' use cd..... to go up 4 directory level] <nowiki># pwd /tmp/very/long/directory/structure</nowiki> '''Method 5: 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 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: <nowiki># </nowiki>mkdircd /tmp/subdir1/subdir2/subdir3 <nowiki>[</nowiki>'''Note:''' This creates the directory and cd to it automatically] <nowiki># pwd /tmp/subdir1/subdir2/subdir3</nowiki> =='''Hack 4. Use “cd -” to toggle between the last two 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 <nowiki># </nowiki>'''cd -''' <nowiki># </nowiki>pwd /tmp/very/long/directory/structure/that/is/too/deep =='''Hack 5. Use dirs, pushd and popd to 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. o dirs: Display the directory stack o pushd: Push directory into the stack o 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. <nowiki># </nowiki>popd -bash: popd: directory stack empty <nowiki># </nowiki>dirs ~ <nowiki># pwd /home/ramesh</nowiki> 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 . <nowiki># </nowiki>dirs /tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1 <nowiki>[</nowiki>'''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. <nowiki># </nowiki>popd <nowiki># pwd /tmp/dir4</nowiki> <nowiki>[</nowiki>'''Note''': After the above popd, directory Stack Contains: /tmp/dir3 /tmp/dir2 /tmp/dir1] <nowiki># </nowiki>popd <nowiki># pwd /tmp/dir3</nowiki> <nowiki>[</nowiki>'''Note''': After the above popd, directory Stack Contains: /tmp/dir2 /tmp/dir1] <nowiki># </nowiki>popd <nowiki># pwd /tmp/dir2</nowiki> <nowiki>[</nowiki>'''Note''': After the above popd, directory Stack Contains: /tmp/dir1] <nowiki># </nowiki>popd <nowiki># pwd /tmp/dir1</nowiki> <nowiki>[</nowiki>'''Note''': After the above popd, directory Stack is empty!] <nowiki># </nowiki>popd -bash: popd: directory stack empty =='''Hack 6. Use “shopt -s cdspell” to automatically correct mistyped directory names on cd'''== 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. <nowiki># </nowiki>cd /etc/mall -bash: cd: /etc/mall: No such file or directory # '''shopt -s cdspell ''' # cd /etc/mall # pwd /etc/mail <nowiki>[</nowiki>'''Note''': By mistake, when I typed mall instead of mail, cd corrected it automatically]
Summary:
Please note that all contributions to TetraWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
TetraWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)