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 ...
- 10-28-2009 #1Just 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:
You can also doCode:mkdir temp cd temp
But I want something better like this:Code:mkdir temp && cd temp
Code:mkdir temp && cd <SPECIAL SYMBOL REFERRING TO THE DIR JUST REFERENCED>
Hypothetical:
How can I simulate this hypothetical with a working command?? There should be a way!!!!Code:mkdir temp && cd _ (makes directory and then switches there) copy temp.text temp2 && cd _ (copies file into temp2 and then switches there)
- 10-28-2009 #2
Write two shell scripts mkdircd and cpcd.
Debian GNU/Linux -- You know you want it.
- 10-29-2009 #3Just 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?
- 10-29-2009 #4
Well, write a script that takes as first parameter the command and as second parameter the name of the directory then.
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.
- 10-29-2009 #5
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.
- 10-29-2009 #6Linux 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?
- 10-29-2009 #7Just 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
It will then expand on the line to the "last arg". The reason it is not perfect is that if you do:Code: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.Code:ls / mkdir test && cd <M-.>
- 11-05-2009 #8
But I want something better like this:
I've found this to work well... let me know if this solves your problem.Code:mkdir temp && cd <SPECIAL SYMBOL REFERRING TO THE DIR JUST REFERENCED>
Code:mkdir temp; cd $_
- 11-05-2009 #9Just 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.


Reply With Quote
