Unix pushd And popd Commands

advertisement
Unix pushd And popd Commands
By Will Mayall
22-JAN-2013
Contents
Using pushd and popd....................................................................................................................... 1
Conclusion............................................................................................................................................... 4
Using pushd and popd instead of cd
The pushd unix command works like the cd command, but
saves your current directory at the top of the stack,
so you can POP back to it using the popd command.
Let me show you how it works:
Let’s start by printing the working directory using
pwd:
m0188ussac1:~$ pwd
/cust/home/wmayal
The above shows I am in the /cust/home/wmayal directory.
Now let’s list what is in the directory using ls –la:
m0188ussac1:~$ ls -la
total 461
drwx--x---
20 wmayal customer
drwxr-xr-x 340 root
root
1024 Jan 22 18:12 .
5632 Jan 14 23:46 ..
-rw-------
1 wmayal customer
18600 Jan 18 23:29 .bash_history
-rw-------
1 wmayal customer
24568 Nov 20 22:26 .lsof_m0188ussac1
-rwx------
1 wmayal customer
121 Jan 22 18:07 .my.cnf
1
-rw-------
1 wmayal customer 300134 Jan 22 18:12 .mysql_history
-rw-r--r--
1 wmayal customer
22 Oct 23 19:41 .plan
-rw-r--r--
1 wmayal customer
2085 Nov 12 18:22 .profile
-rwxr-xr-x
1 wmayal customer
2060 Oct 15 21:54 .profile.bak
-rwx------
1 wmayal customer
41 Oct 12 16:42 .source_mysql.ksh
drwxr-xr-x
5 wmayal customer
1536 Jan 22 17:17 anthology_preprod
drwxr-xr-x
5 wmayal customer
1024 Jan 22 17:24 anthology_prod
drwxr-xr-x
3 wmayal customer
512 Nov 20 21:21 bin
drwxr-xr-x
4 wmayal customer
512 Jan 22 16:53 campusroardev
drwxr-xr-x
2 wmayal customer
2048 Oct 23
2008 docs
-rw-r--r-1 wmayal customer
fix_collation_anthology.sql
592 Nov
drwxr-xr-x
4 wmayal customer
512 Jan 22 17:26 fuelboard_preprod
drwxr-xr-x
4 wmayal customer
1024 Jan 22 17:31 fuelboard_prod
drwxr-xr-x
5 wmayal customer
1024 Jan 22 17:32 hyperevo_preprod
drwxr-xr-x
4 wmayal customer
hyperevo_preprodenc18
9 16:57
512 Jan 22 17:33
drwxr-xr-x
5 wmayal customer
1024 Jan 22 17:40 hyperevo_prod
-rw-r--r--
1 wmayal customer
drwxr-xr-x
2 wmayal customer
drwxr-xr-x
6 wmayal customer
512 Nov
-rw-r--r--
1 wmayal customer
704 Jan 10 17:43 query_cache.txt
drwxr-xr-x
3 wmayal customer
-rw-r--r--
1 wmayal customer
drwxr-xr-x
2 wmayal customer
-rw-r--r--
1 wmayal customer
drwxr-xr-x
2 wmayal customer
0 Oct 24 22:11 mysqlaccess.log
7168 Nov 20 17:18 oracle_utilities
8 23:28 percona-toolkit
1024 Jan 16 17:57 scripts
51200 Jan 10 16:58 scripts.tar
512 Jan 16 17:59 sql
20480 Jan 10 16:58 sql.tar
512 Oct 25 20:40 testdb
2
drwxr-xr-x
4 wmayal customer
1024 Jan 22 17:42 wellnesspilot
drwx-----8 wmayal customer
import_preprod
1024 Jan 22 17:44 werun-
drwxr-xr-x
1024 Jan 22 17:51 werun-import_prod
6 wmayal customer
Now let’s use pushd instead of cd to change directories
to hyperevo_preprodenc18
m0188ussac1:~$ pushd hyperevo_preprodenc18
~/hyperevo_preprodenc18 ~
Now we are in the /cust/home/wmayal/hyperevo_preprodenc18
directory, you can verify that by typing pwd:
m0188ussac1:~/hyperevo_preprodenc18$ pwd
/cust/home/wmayal/hyperevo_preprodenc18
You can see we are now in the
/cust/home/wmayal/ hyperevo_preprodenc18
directory.
Now let’s use pushd to go to the ../fuelboard_preprod
directory instead of using the cd command.
m0188ussac1:~/hyperevo_preprodenc18$ pushd ../fuelboard_preprod
~/fuelboard_preprod ~/hyperevo_preprodenc18 ~
m0188ussac1:~/fuelboard_preprod$ pwd
/cust/home/wmayal/fuelboard_preprod
3
Now you can see we are in the
/cust/home/wmayal/fuelboard_preprod directory.
Now, let’s use popd to get back to the
/cust/home/wmayal/hyperevo_preprodenc18
directory.
m0188ussac1:~/fuelboard_preprod$ popd
~/hyperevo_preprodenc18 ~
m0188ussac1:~/hyperevo_preprodenc18$ pwd
/cust/home/wmayal/hyperevo_preprodenc18
Walla, we are back in the
directory! And if you
issue the popd command again, it will take you home!
/cust/home/wmayal/hyperevo_preprodenc18
m0188ussac1:~/hyperevo_preprodenc18$ popd
~
m0188ussac1:~$ pwd
/cust/home/wmayal
m0188ussac1:~$
Conclusion
Using the pushd and popd commands instead of cd, can
save you time when switching between directories!
4
Download