Ian Grey
Ian Grey

Reputation: 13

Find, copy, rename within the same directory

I an new to bash and could do with some help. I am looking to create a command that will instantly duplicate the last created file with a new new. So far I come up with the following command;

find /home/ian/Desktop/TEST/ -type f -mmin -1 -exec echo cp {} /home/ian/Desktop/TEST/ \;

But for the life or me I cannot find a way to rename the file and I have looked around and tried various methods but none seemed to work. I would like to have control over the renaming so that I can add '_backup' to each file that is copied.

What would be the best way to go about this? Sorry if this is such a basic question

All the best, Ian

Upvotes: 1

Views: 1068

Answers (3)

Sylvain Leroux
Sylvain Leroux

Reputation: 51990

You probably need to use -execdir instead of -exec:

-execdir command ;

-execdir command {} +
       Like -exec, but the specified command is run from the  subdirec‐
       tory  containing  the  matched  file,  which is not normally the
       directory in which you started find.

The following command seems to work as expected:

find /home/ian/Desktop/TEST/ -type f -mmin -1 -execdir echo cp \{} \{}_backup \;

Upvotes: 1

Etan Reisner
Etan Reisner

Reputation: 80921

find /home/ian/Desktop/TEST/ -type f -mmin -1 -exec echo cp {} /home/ian/Desktop/TEST/{}_backup

Upvotes: 0

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16017

you can make the output of a command a string, like a=$(my_command). (My_command being the find.) Then you can say mv "$a" "$a".backup.

Upvotes: 0

Related Questions