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!
=='''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
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)