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

  • 1
    You should specify what shell you are using. – jw013 Dec 3 '14 at 19:39
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

  • But this is specific for the example given, I want something general cause it could be a different destination directory and different extra text – tachomi Dec 3 '14 at 19:27
  • POST edited accordingly – Gilles Quenot Dec 3 '14 at 19:34
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.

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
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 Thanks for correcting. Any reason you said 'never parse the output of ls ? – Sree Dec 4 '14 at 8:37
  • 2
    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.) – muru Dec 4 '14 at 8: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.