Search code examples
linuxfindcpmv

cp: invalid option -- 'D'


My goal is to find all .pdf files from multiple subfolder structures and then move them to another folder.

For this I have assembled the following.

find /mnt/user/Data/01_Persönliche_Dokumente/01_Firmen -iname \*.pdf -type f | xargs cp -t /mnt/user/Data/01_Persönliche_Dokumente/Paperless_input/

But as an error you get the following:

root@Tower:/mnt/user/Data/01_Persönliche_Dokumente/01_Firmen# find "/mnt/user/Data/01_Persönliche_Dokumente/01_Firmen" -iname \*.pdf -type f | xargs cp -t "/mnt/user/Data/01_Persönliche_Dokumente/Paperless_input"
cp: invalid option -- 'D'
Try 'cp --help' for more information.

I try diffrent options and get some help in the Unraid Discord.


Solution

  • I got a hint from a Friend of mine. The correct comand looks like this:

    find "/mnt/user/Data/01_Persönliche_Dokumente/01_Firmen" -iname \*.pdf -type f -print0  | xargs -0 cp -t "/mnt/user/Data/01_Persönliche_Dokumente/Paperless_input"
    

    for the find command, I added -print0 which means:

    print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output.

    for the xargs command I added 0 which means:

    -0 : input items are terminated by null character instead of white spaces.

    basically removing any characters that would screw with the result of find