5

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

tachomi
  • 7,592

5 Answers5

4

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

3

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.

muru
  • 72,889
2

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
jw013
  • 51,212
1

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.

muru
  • 72,889
Sreeraj
  • 5,062
  • @muru Thanks for correcting. Any reason you said 'never parse the output of ls ? – Sreeraj Dec 04 '14 at 08:37
  • 2
    Consider what will happen, if you have a file named a b.csv. And for a full discussion: http://unix.stackexchange.com/questions/128985/why-not-parse-ls (Accordingly, other things in your answer need changing, such as quoting of $i, etc.) – muru Dec 04 '14 at 08:40
0

You can make simple script in home folder(or any folder other than the folder which contains the files you are intending to move). The script will be like this:

#!/bin/bash
for var in ./*
do
if [ -f "$var" ]
then
mv "$var" /home/user/"$var".backup1
fi
done
  • run this script inside the folder from which you are intending to move files i.e. by cd <the directory your files are in> and then run this script
  • this script will move all files to /home/user and will change the name accordingly
  • if the filenames contain spaces no problem
  • it will also make sure that its moving only files not directory so that you dont get any error message or unexpected result
Alex Jones
  • 6,353