Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
I have been trying to do this.. but I don't know why it doesn't work... anyone knows? cd | pwd -LP For some reason pwd -LP is not passed to ...
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Posts
    5

    piping cd and pwd

    I have been trying to do this.. but I don't know why it doesn't work... anyone knows?

    cd | pwd -LP

    For some reason pwd -LP is not passed to the cd command... please help

  2. #2
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    This is probably because this command pipes the output of 'cd' into 'pwd -LP', not the other way around...
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Just Joined!
    Join Date
    Nov 2010
    Posts
    5

    Wink

    Well pwd -LP | cd doesn't work either... so what should I write in that case?!

  4. #4
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    Have you actually read the man page for pwd? I'm sure you realise that it works perfectly but what you're doing has no effect?

    pwd stands for 'print working directory' - i.e. the current directory. If you then cd to it - you're back where you started.
    Linux user #126863 - see http://linuxcounter.net/

  5. #5
    Just Joined!
    Join Date
    Nov 2010
    Posts
    5
    Well, I guess this is not such a newbie question after all, because there are cases when you are wrong.... pwd -LP list the full path to where I am... if I followed a link to go there... the location will be for example /mylink/logs/ but the real full path which pwd -LP will give is /somefolder/another/folder/logs/

    So what I have to do now... is if I do it manually write pwd -LP cut the result... after that write cd <paste the result> .... this takes a lot of time.

    So why is it useful to cd to the full path... because if I write cd.. when I followed the link it takes me back to the place I was before following the link, but if I write cd .. after having cd the full link.. it takes me one step back in the directory structure.

    So no my problem is NOT solved... still need help.

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Google: bash return to previous directory

    Code:
    pushd/popd
    
    Bash will keep a history of the directories you visit, you just have to ask.
    Bash stores the history in a stack and uses the commands pushd
    and popd to manage the stack.
    
    pushd dir - move the current directory onto the stack and change to
    the dir directory.
    
    popd - pops the top directory off of the stack and moves you into it.
    
    We’re opening files all over the file system, internal code, vendor code, templates, configuration files, logs. Because of this we like the ability to take a detour on the file system and still navigate back to our working directory of the day. I think these commands are so useful that I alias’d them in my .bashrc
    
    
    alias cd="pushd"
    alias bd="popd"
    
    Now the cd command manages the stack for me as well as changing directories. Aliasing popd to bd is an easy to remember and easy to type way to move back up the stack, think “change dir” and “back dir”.

  7. #7
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So. I understand what you are attempting to accomplish, so let's assume that is a reasonable request .

    The problem that you are encountering is the way that piping works.

    In your example:
    Code:
    pwd -LP | cd
    You are taking the output of "pwd -LP", which is the resolved-link version of your PWD, and making it the input to cd.

    But "cd" actually does not read any input. "cd" works by reading its commandline arguments. So although you have set up the pipe correctly, cd does not work the way you are expecting.

    The correct way to do what you want is:
    Code:
    cd $(pwd -LP)
    This executes the command "pwd -LP", and replaces the $(...) bit above with the output of that command. So cd will be executed with a commandline argument which is the output of "pwd -LP".

    Does that make sense?
    DISTRO=Arch
    Registered Linux User #388732

  8. #8
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    Quote Originally Posted by linuxaid View Post
    pwd -LP list the full path to where I am... if I followed a link to go there... the location will be for example /mylink/logs/ but the real full path which pwd -LP will give is /somefolder/another/folder/logs/
    Hmmm, not exactly.

    Code:
    # pwd -P
    does what you require. The -L and -P options do the opposite of each other, -P tells pwd to dereference symlinks and show the absolute path, while -L tells pwd to show output using the symlinked versions. Interestingly, the last one is the one that takes priority - on my CentOS system here, -PL behaves like -L, while -LP behaves like -P.

    I didn't know that about 'cd' though; just like you I thought it behaved just like any other BASH command.
    Linux user #126863 - see http://linuxcounter.net/

  9. #9
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    Quote Originally Posted by HROAdmin26 View Post
    Google: bash return to previous directory

    Code:
    pushd/popd
    
    Bash will keep a history of the directories you visit, you just have to ask.
    Bash stores the history in a stack and uses the commands pushd
    and popd to manage the stack.
    
    pushd dir - move the current directory onto the stack and change to
    the dir directory.
    
    popd - pops the top directory off of the stack and moves you into it.
    
    We’re opening files all over the file system, internal code, vendor code, templates, configuration files, logs. Because of this we like the ability to take a detour on the file system and still navigate back to our working directory of the day. I think these commands are so useful that I alias’d them in my .bashrc
    
    
    alias cd="pushd"
    alias bd="popd"
    
    Now the cd command manages the stack for me as well as changing directories. Aliasing popd to bd is an easy to remember and easy to type way to move back up the stack, think “change dir” and “back dir”.
    You don't need to apply these aliases. If you do
    Code:
    # cd -
    it takes you back to the directory you last used cd to get out of. It's not a complete stack, though, it's only one hop deep.
    Linux user #126863 - see http://linuxcounter.net/

  10. #10
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Quote Originally Posted by Roxoff View Post
    ...
    Congrats, you missed the point that OP can search Google for a very simple phrase related to his question and get a workable answer.

    In other breaking news, there are at least XXX ways to accomplish nearly anything in Linux.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...