Say I need to move the file /etc/dir1/dir2/dir3/a.out
to /etc/dir1/dir2/dir3/b.out
Typing the whole path twice is tedious, and going to the directory might not be ideal. Is there a way to do such a move without retyping the directory?
On a related question (that I suspect has the same solution). Say I want to move a file someverybigfilename.png
to somereallybigfilename.png
. Is there a way to accomplish that without retyping the name?
(I know I can use a script. But does mv
support some such thing natively?)
In short: how can I use move to partially alter/alter just a part of a filename? How to do a small change like extension?
In the first case, if you are using
bash
, you can use brace expansion:or even
In the second case you could use brace expansion again:
Or (although it doesn't really save much typing) you could use Bash history expansion as follows:
either
or
make a script like
and to rename:
If the path is the same I don't see the reason why not entering in the folder.
You could save the path with pushd or came back with cd -
Example:
or
About the filename the only thing that came to my mind is to store it in a variable..
There's several ways to approach this. One could be via function defined in
~/.bashrc
file.The
pmv
function then would take 3 arguments. Path to directory, source file, and destination file. For instance:Notice that in this function we join directory path and file path with slash, thus when path to directory is given as command-line argument, there shouldn't be a trailing slash at the end. We can, however, improve the function definition using bash's parameter expansion, which will then allow us to use either
/path/to/dir
or/path/to/dir/
:Effectively,no, there is no way to do that with
mv
alone. Even steeldriver's and mine solutions for first part of your question make use ofbash
's features, notmv
's. More advanced renaming manipulations, such as in your case inserting text into filename, should be done withprename
where you use perl regular expressions to dynamically rename files.