with mv, consider the path/name of the original file when choosing destination [duplicate]

Solution 1:

In the first case, if you are using bash, you can use brace expansion:

mv /etc/dir1/dir2/dir3/{a.out,b.out} 

or even

mv /etc/dir1/dir2/dir3/{a,b}.out

In the second case you could use brace expansion again:

mv some{very,really}bigfilename.png

Or (although it doesn't really save much typing) you could use Bash history expansion as follows:

$ mv someverybigfilename.png !#$:s/very/really
mv someverybigfilename.png somereallybigfilename.png

Solution 2:

either

cd /etc/dir1/dir2/dir3/
mv a.out b.out

or

make a script like

#!/bin/bash
dir_Path="/etc/dir1/dir2/dir3/"

mv $dir_Path\a.out $dir_Path\b.out

exit

and to rename:

#!/bin/bash
dir_Path="/etc/dir1/dir2/dir3/"
name_part="bigfile.png"
cd $dir_Path
mv some$name_part somereally$name_part

exit

Solution 3:

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:

luca@elite:~$ pushd dir1/dir2/dir3/
~/tmp/dir1/dir2/dir3 ~/tmp
luca@elite:~/dir1/dir2/dir3$ mv a.out b.out
luca@elite:~/dir1/dir2/dir3$ popd
~/tmp
luca@elite:~$ 

or

luca@elite:~$ cd dir1/dir2/dir3/
luca@elite:~/dir1/dir2/dir3$ mv b.out a.out
luca@elite:~/dir1/dir2/dir3$ cd -
luca@elite:~$ 

About the filename the only thing that came to my mind is to store it in a variable..

Solution 4:

There's several ways to approach this. One could be via function defined in ~/.bashrc file.

pmv(){
    mv "$1"/"$2" "$1"/"$3"
}

The pmv function then would take 3 arguments. Path to directory, source file, and destination file. For instance:

$ ls testdir
self_test.sh*  test.wxg
$ pmv ./testdir  test.wxg  test2.wxg                                                                                     
$ ls testdir                                                                                                             
self_test.sh*  test2.wx

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/:

pmv(){
    mv "${1%/}"/"$2" "${1%/}"/"$3"
}

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?

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 of bash's features, not mv's. More advanced renaming manipulations, such as in your case inserting text into filename, should be done with prename where you use perl regular expressions to dynamically rename files.

$ touch someverybigfilename.png

$ prename 's/very/really/' someverybigfilename.png                                                                       

$ ls somereallybigfilename.png                                                                                           
somereallybigfilename.png