How to move a file and change its name without retyping the name and just add the new characters

22,615

Solution 1

You could use a or function added in your shell rc file :

mymv(){ echo mv "$1" "$2/${1##*/}_$3"; }
mymv file.csv /home/user backup1

remove the echo when tests are done

Solution 2

The simplest route, IMHO, is to use a variable:

a=file.csv; mv "$a" ~user/"$a"_backup

You can avail of tab completion with variables, both while setting them and while using them.

Solution 3

In bash, you could try the following:

  • Type mv file1.
  • Press Ctrl-w enough times to delete file1.
  • Press Ctrl-y to paste file1 back.
  • Type /home/user/.
  • Press Ctrl-y to paste file1.
  • Type the rest: _backup

Solution 4

Another solution.

A little crude, but this is going to work if all the original files are going to have the extension .csv and if you want to move all the .csv files from the current directory.

for i in *.csv; do 
    mv $i /home/user; 
    rename .csv .csv_backup1 /home/user/*.csv; 
done

Just change the 'user' for each users when needed.

Share:
22,615

Related videos on Youtube

tachomi
Author by

tachomi

Updated on September 18, 2022

Comments

  • tachomi
    tachomi almost 2 years

    I change too frequently the location of some files generated daily. The thing is that I want to change their names by only adding the new required characters.

    What I want is something like this:

    $ mv file.csv /home/user/{something}_backup1
    

    So I could see:

    $ ls /home/user
    file.csv_backup1
    

    What I'm doing now is the simple:

    $ mv file.csv /home/user/file.csv_backup1
    

    You could say "don't be lazy and do it that way", the thing is that the real file names have around 25 characters and retyping them is really annoying.

    The past given is only an example, it could be a different directory or different new text.

    By the way I'm using bash shell

    • jw013
      jw013 over 9 years
      You should specify what shell you are using.
  • tachomi
    tachomi over 9 years
    But this is specific for the example given, I want something general cause it could be a different destination directory and different extra text
  • Gilles Quenot
    Gilles Quenot over 9 years
    POST edited accordingly
  • Sreeraj
    Sreeraj over 9 years
    @muru Thanks for correcting. Any reason you said 'never parse the output of ls ?
  • muru
    muru over 9 years
    Consider what will happen, if you have a file named a b.csv. And for a full discussion: unix.stackexchange.com/questions/128985/why-not-parse-ls (Accordingly, other things in your answer need changing, such as quoting of $i, etc.)