Move file and change directory in one command
Hi,
I am a UNIX/Linux users since ten years back and I've been using the terminal (BASH) almost daily since then. One thing that has always annoyed me is the lack of a command to move a file (mv) to a directory and then immediately cd (ie. change directory) to that same destination. This would be a very nifty command saving both my time and wrists! Example: Code:
arnold@computer:~/path$ mkdir newpath Using a script would not, as I see it, work because changing directory withing the script would only change current directory within the script and not outside in my terminal instance. Writing an alias I am not sure would be possible unless some parameter magic can be done within the alias functionality. Is there anyone who has got the slightest idea to take on this silly little quest? Thanks Jens |
You can:
1. Write a script 2. Use ";" |
1. A script does not work because the $PWD is local to the script. A child process cannot export variables back to the parent processes that spawned them.
2. Elaborate. I cannot see how using a ';' would yield anything useful in this case as long as it was possible to extract the parameters when using aliasing. |
Another possibility is to define a function in your .bashrc. It can be something like this:
Code:
function mvc(){ Code:
$ pwd |
Thanks! A few simple modifications and this works like a charm:
Code:
function mvcd { Thanks again! Jens |
Very good! Indeed no need to loop over files. Well done! :)
|
Not so sure. Up to 8 files to move, I think this would work, but beyond that, the 9-parameters limit is reached and the script fails. This should work:
Code:
function mvcd { Code:
$ function test() { |
Quote:
Code:
mv *.c some_dir Code:
mv *.c Code:
mv: target `XXX' is not a directory Code:
mv a.c b.c So as precaution: Code:
function mvcd { |
Yves, you are right! Thanks for notice. However in bash you can avoid the problem using brackets to envelop positional parameters greater than 9. So, following the example I posted above, this version should work
Code:
function mvc () |
All times are GMT -5. The time now is 01:03 AM. |