Find the answer to your Linux question:
Results 1 to 9 of 9
I find using the command line I often have these scenarios: make a directory and then switch into that same directory copy a file into a directory and then cd ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    4

    Basic Bash Command Line Change DIR

    I find using the command line I often have these scenarios:

    • make a directory and then switch into that same directory
    • copy a file into a directory and then cd into that same dir


    It is tiresome always doing 2 commands:
    Code:
    mkdir temp
    cd temp
    You can also do
    Code:
    mkdir temp && cd temp
    But I want something better like this:

    Code:
    mkdir temp && cd <SPECIAL SYMBOL REFERRING TO THE DIR JUST REFERENCED>

    Hypothetical:

    Code:
    mkdir temp && cd _ (makes directory and then switches there)
    copy temp.text temp2 && cd _ (copies file into temp2 and then switches there)
    How can I simulate this hypothetical with a working command?? There should be a way!!!!

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Write two shell scripts mkdircd and cpcd.
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    4
    Yeah that would work a little bit I guess. I want it general though for all commands like mv and stuff too. It should be builtin to the bash program don't you think? Is it something they will still change or bash something that hasn't been updated since '80's?

  4. #4
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Quote Originally Posted by dlandis View Post
    Yeah that would work a little bit I guess. I want it general though for all commands like mv and stuff too.
    Well, write a script that takes as first parameter the command and as second parameter the name of the directory then.

    Quote Originally Posted by dlandis View Post
    It should be builtin to the bash program don't you think? Is it something they will still change or bash something that hasn't been updated since '80's?
    GNU bash has still active maintainers. The last patch is dated 10/24/2009.
    The GNU Bourne-Again Shell

    I am sure there is a way with aliases or something to do this already. I just can't bother to read the manual to find out.
    Debian GNU/Linux -- You know you want it.

  5. #5
    Linux Newbie tetsujin's Avatar
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by GNU-Fan View Post
    Write two shell scripts mkdircd and cpcd.
    OK, pop quiz! Why won't that work?

    Actually, forget the pop quiz. It won't work because a shell script runs as a separate process with its own working directory and environment. So any changes you make to the working directory and environment in the script don't apply to the shell in which you ran the script - unless you go out of your way to run the script within the current shell, by typing "source scriptfile" or ". scriptfile"

    Basically to implement things like this in the shell you either need to write a function or an alias. Either one will be local to the particular shell process in which it's defined, so to have it appear for every shell you'd need to put it in your profile or bashrc.

  6. #6
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    Code:
    function mkdirs () { mkdir $1 && cd $1; }
    In a world without walls and fences, who needs Windows and Gates?

  7. #7
    Just Joined!
    Join Date
    Oct 2009
    Posts
    4
    Thanks for the replies. I have read some man pages and stuff like that which is not what I'd rather do. I have found something deep in the section on "Manipulating History". It is called 'yank-last-arg' command. It is the closest to what I'm asking.

    You do

    Code:
    mkdir test
    cd <M-.>
    It will then expand on the line to the "last arg". The reason it is not perfect is that if you do:

    Code:
    ls /
    mkdir test && cd <M-.>
    Then it will expand to /, and not test !! It is a bug for me I don't know about anyone else.

  8. #8
    Just Joined! mehorter's Avatar
    Join Date
    May 2006
    Posts
    20
    But I want something better like this:

    Code:
    mkdir temp && cd <SPECIAL SYMBOL REFERRING TO THE DIR JUST REFERENCED>
    I've found this to work well... let me know if this solves your problem.

    Code:
    mkdir temp; cd $_

  9. #9
    Just Joined!
    Join Date
    Oct 2009
    Posts
    4

    Very good

    YES. That is definitely working for me I just tried it. Thanks for responding.

    That symbol $_ is not referenced ANYWHERE in the bash man page which is weird. Also it is hard to google special symbols like that making it even harder. So thanks, it would be perfect but it's a little clunky typing the dollar sign, but very NICE nevertheless. THANKS.

Posting Permissions

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