[SOLVED] Can anybody help me understand pushd and popd?

Status
Not open for further replies.

GodCorleone

Honorable
Apr 12, 2014
33
0
10,530
Hi,
Lets take an example:
$ cd temp
$ mkdir -p i/like/icecream
$ pushd i/like/icecream
~/temp/i/like/icecream ~/temp
$ popd
~/temp
$ pwd
~/temp
$ pushd i/like
~/temp/i/like ~/temp
$ pwd
~/temp/i/like
$ pushd icecream
~/temp/i/like/icecream ~/temp/i/like ~/temp
$ pwd
~/temp/i/like/icecream
$ popd
~/temp/i/like ~/temp
$ pwd
~/temp/i/like
$ popd
~/temp
$ pushd i/like/icecream
~/temp/i/like/icecream ~/temp
$ pushd
~/temp ~/temp/i/like/icecream
$ pwd
~/temp
$ pushd
~/temp/i/like/icecream ~/temp
$ pwd
~/temp/i/like/icecream
$

I can write the above code in my terminal and can get results but I cannot understand what I am doing. Please anybody help me out? I am learning CLI on my own.

Q2) What's the difference between ls -1R and ls -R
Q3) What's the cp-r command and how does it differ from the regular cp command?

Thank you so much.
 
Solution
Pushd adds to the directory tree you're working in.

in your example, you start out the code with
-cd temp; this command means we're now working in the "temp" folder; cd is a command that means "change directory"
-you follow this up by making the file tree i/like/icecream with the mkdir -p command, this happens in the temp folder because that's where we're working
-next we have pushd make it's appearance, all its doing is giving you a shorthand for saying move to work in temp\i\like\"icecream", it's like navigating down the file tree in windows.

popd on the other hand just backs up to the last folder you were in prior to the pushd command.

It might make more sense to write it out like you'd see it in a DOS command prompt...
Pushd adds to the directory tree you're working in.

in your example, you start out the code with
-cd temp; this command means we're now working in the "temp" folder; cd is a command that means "change directory"
-you follow this up by making the file tree i/like/icecream with the mkdir -p command, this happens in the temp folder because that's where we're working
-next we have pushd make it's appearance, all its doing is giving you a shorthand for saying move to work in temp\i\like\"icecream", it's like navigating down the file tree in windows.

popd on the other hand just backs up to the last folder you were in prior to the pushd command.

It might make more sense to write it out like you'd see it in a DOS command prompt

C:\> cd users
C:\Users> pushd Admin\AppData\Roaming
C:\Users\Admin\AppData\Roaming>popd
C:\Users\>

2) ls is list directory in linux/unix (its not a dos command), ls -r would be listing the directory in reverse. i'm not sure what ls -1r is.

3) cp is just a "copy" command, the -r switch copies the whole directory tree as well.
example, if I type
-cp image.jpg /pictures, i'll copy the file image.jpg to the folder "pictures",

however what if I want to copy all files in a folder? this is what cp -r is for

cp -r pictures /temp
now i've copied the whole folder and all it's contents into a folder called temp (assuming it exists)
 
Solution


THANK YOU. That made it crystal clear after I read it over and over again :)
 

They explain in great detail. Exactly. And that's why I find it quite overwhelming to look up the man pages... They aren't exactly novice-friendly I think.
 

Hmm you are right but I wrote ls -R and I looked it up and I found that it means to list subdirectories recursively... not sure what's that supposed to mean though... 😳 recursive is a vaguely familiar word to me 'cause am not a native english speaker :)

 
Status
Not open for further replies.

TRENDING THREADS