1

I want to make a specific copy. I explain

So here my main folder :

Sub-Directory-name-01\filename-01.jpg
Sub-Directory-name-01\filename-02.jpg
Sub-Directory-name-01\filename-03.jpg
Sub-Directory-name-01\special-filename-01.jpg

Sub-Directory-name-02\filename2-01.jpg
Sub-Directory-name-02\filename2-02.jpg
Sub-Directory-name-02\filename2-03.jpg
Sub-Directory-name-02\special-filename2-01.jpg


Sub-Directory-name-02\filename2-01.jpg
Sub-Directory-name-02\filename2-02.jpg
Sub-Directory-name-02\filename2-03.jpg
Sub-Directory-name-02\special-filename2-01.jpg

I want to copy all file from all dir and :
- keep original file
- copy 2 times the original file
- add a prefix to the new name
- prefix-01 for first copy
- prefix-02 for second copy
- keep the new files in the same dir as original file

I allready succes with a command to copy 1 time with 1 prefix. It works in the sub-directory

for file in *.jpg; do cp "$file" "prefix-$file"; done

I try to do for all sub-dirs but i got an error

find . -type f \( -iname "*.jpg" ! -iname "special-*.jpg" \) | xargs cp -v "$file" "prefix-$file"

( yes i exclude a special name )

But i got error :

cp: target `./Sub-Directory-name-01/filename-01.jpg' is not a directory

i dont know how to solve my problem and how to add the 2nd copy in the cmd.

Thanks

Edit : I havent found any similar question so any answser to solve this problem.

Newbies
  • 39
  • 1
  • 7
  • Possible duplicate of [Rename multiple files in Unix](https://stackoverflow.com/q/1086502/608639), [https://stackoverflow.com/q/6911301/608639](https://stackoverflow.com/q/16541582/608639), [Rename multiple files by replacing a particular pattern in the filenames using a shell script](https://stackoverflow.com/q/6840332/608639), [Find directories with names matching pattern and move them](https://stackoverflow.com/q/22319557/608639), [Rename multiple files shell](https://stackoverflow.com/q/6911301/608639), etc. – jww May 11 '18 at 00:28
  • JWW, It's absolutely not a duplicate of threads you post. I m not even sure you took time to read question and answer.. Thanks to JJO for his help. – Newbies May 17 '18 at 19:25

1 Answers1

0

Note that above $file is set only by the for file in ... ; do ... ;done loop, i.e. in your xargs cmdline you were just using the last leftover value from the loop.

Some things to consider:

  • need to process each file separately => use xargs -l1 (process each 1 line).

  • need to separate DIR/FILENAME as the needed command is something like 'cp $DIR/$FILENAME $DIR/prefix-01-$FILENAME' (and prefix-02 also), use find ... -printf "%h %f\n" for this

  • for each line, need to do couple things (prefix-01,02) => use a scriptlet via sh -c '<scriptlet>'

  • better skip prefix-0?-*.jpg files from find, to be able to re-run it without "accumulating" copies

A possible implementation would be:

find . -type f \( -iname "*.jpg" ! -iname "special-*.jpg"  ! -name "prefix-0?-*.jpg" \) -printf "%h %f\n" | \
xargs -l1 sh -c 'cp -v "$1/$2" "$1/prefix-01-$2"; cp -v "$1/$2" "$1/prefix-02-$2"' --

As xargs runs sh -c '<scriptlet>' -- DIR FILE for each line, the scriptlet will properly evaluate $1 and $2 respectively.

--jjo

PS: directory separator in Unix-like systems is / :)

[Update: fixed to use %f instead of %P, as per comments below]

jjo
  • 2,595
  • 1
  • 8
  • 16
  • Thanks for your answer. I try to understand what you write.. I test your command and i got an error, it's doubling subdirname : cp: cannot stat `./Sub-Directory-name-01/Sub-Directory-name-01/filename-01.jpg': No such file or directory cp: cannot stat `./Sub-Directory-name-01/Sub-Directory-name-01/filename-01.jpg': No such file or directory – Newbies May 10 '18 at 16:42
  • that's working with : -printf "%h %f\n" instead of -printf "%h %P\n" – Newbies May 10 '18 at 16:59