0

I am trying to find files with specific extensions and copy or rsync it to another hard drive / directory

sudo find . \( -type f \( -iname '*.jpg' -o -iname "*png" -o -iname "*mov" -o -iname "*mp3" -o -iname "*mp4" -o -iname "*swf" -o -iname "*avi" \) -exec rsync {} ~/test/dest/ \; \)

and shell output is this:

cp: './dest/DSC_0085.JPG' and '/home/sanz/test/dest/DSC_0085.JPG' are the same file
cp: './dest/DSC_0086.avi' and '/home/sanz/test/dest/DSC_0086.avi' are the same file

and so on...

Why does it keep telling me that what I'm copying and where I'm trying to copy are the same file, even though they are in different directors. this makes no sense.

I'm also trying different command variant, but it also doesn't work as I wanted it to work:

sudo find . -type f -iname '*.jpg' -o -name "*png" -o -name "*mov" -o -name "*mp3" -o -name "*mp4" -o -name "*swf" -o -name "*avi" -exec rsync -ah --progress {} ~/test/dest \;

It copies ONLY avi files and completely ignores others. It is just strange to me how the same command without -exec normally outputs all the specifically labelled files, but when I want it to work with copying command it only reads/copies avi files.

2 Answers2

1

You are running the command in /home/sanz/test and then finding the files with the names that are specified anywhere inside which includes the subdirectory dest which find identifies as ./dest. That is the same directory as ~/test/dest/ and /home/sanz/test/dest/ so you are effectively trying to copy the directories and files into themselves.

If you want to find and copy the files inside of /home/sanz/test into ~/test/dest, then you need to use the -prune option to exclude /home/sanz/test/dest:

sudo find . -path ./dest -prune -o \( -type f \( -iname '*.jpg' -o -iname "*png" -o -iname "*mov" -o -iname "*mp3" -o -iname "*mp4" -o -iname "*swf" -o -iname "*avi" \) -exec rsync {} ~/test/dest/ \; \)
Nasir Riley
  • 11,422
0

So as it turns out if you copy files from directory to that directory's sub-directory it counts as if you copy same files at same dir as Freddy says. So I created directory somewhere else and after more smashing my head to a keyboard later I created this code:

sudo find ~/test/ \( -type f \( -iname '*.jpg' -o -iname "*png" -o -iname "*mov" -o -iname "*mp3" -o -iname "*mp4" -o -iname "*swf" -o -iname "*avi" \) -exec rsync {} ~/dest/ \; \)

This worked like charm. I do not have terminology for these techy things, but I hope yall understand me.

  • It is not that it counts as if it is the same file. It is the same file. You copied the file to a sub-directory, then found it there, and tried to copy it again. If it must be a sub-directory, then you need to use an exclude as part of the search. – ctrl-alt-delor Apr 18 '20 at 21:27
  • Oh, that makes sense. I feel stupid now... Thanks to you too :* – xXxAirSnifferxXx Apr 18 '20 at 21:32
  • That is the feeling of learning. When you feel like that, it is in comparison with your new state. Keep learning. – ctrl-alt-delor Apr 18 '20 at 21:37