Rick James
Rick James

Reputation: 141

Move multiple files with unique name to new folder and append to file name

I have about 2000 files in a folder.

All the files contain the string test in the name.

What I need to do is move all those files ~1250 to a folder called trash within the same directory and append _scrap to the end of each file.

 mv *test* trash/

What I want is something like this:

[root@server] ls
test1.txt  test2.txt  test3.txt trash  video1.txt  video2.txt  video3.txt
[root@server] mv *test* trash/*_scrap
[root@server] ls
trash  vidoe1.txt  video2.txt  video3.txt
[root@server] ls trash/
test1.txt_scrap  test2.txt_scrap  test3.txt_scrap

I can move all files, however I cannot figure out how to append the _scrap to the end.

As I have to do this on a number of machines, a one liner would be preferable over a small script.

Upvotes: 0

Views: 1607

Answers (2)

ErlVolton
ErlVolton

Reputation: 6784

You can use rename to avoid shell for loops. It's a perl script but it comes installed with many common distros (including Ubuntu 14):

$ mv *test* trash/
$ rename 's/$/_scrap/g' trash/*
$ ls trash/
test1.txt_scrap  test3.txt_scrap  test2.txt_scrap

Upvotes: 2

amdn
amdn

Reputation: 11582

$ touch test1.txt  test2.txt  test3.txt vidoe1.txt  vidoe2.txt  vidoe3.txt
$ mkdir trash
$ for file in *test*; do mv "$file" "trash/${file}_scrap"; done
$ ls
trash       vidoe1.txt  vidoe2.txt  vidoe3.txt
$ ls trash
test1.txt_scrap test2.txt_scrap test3.txt_scrap
$ 

You could also use xargs

$ ls *test* | xargs -t -I{} mv {} trash/{}_scrap
mv test1.txt trash/test1.txt_scrap
mv test2.txt trash/test2.txt_scrap
mv test3.txt trash/test3.txt_scrap
$ 

You could use find

$ find . -name '*test*' -maxdepth 1 -exec mv {} trash/{}_scrap \;

Upvotes: 3

Related Questions