0

I wanted to copy all my pdfs to a pdf folder, for that I used this find command with the -exec option. Before I started, I created a folder named pdf in this current folder

sudo find . -type f -iname "*.pdf" -exec mv {} /pdf \;

The pdfs are gone, but not where i expected. Also there is no /pdf folder I thought there they could be. There is staff folder at /pdf

-rw-r--r--@   1 myusername  staff   9,9M 19 Aug 19:53 pdf

Any idea?

2 Answers 2

5

The target directory for the mv you gave as /pdf, you should have used ./pdf. Since there was no directory (folder) named pdf in the top-level directory /, but since you were root, mv moved each of the files it moved to the file /pdf. The only recoverable file is now called /pdf, and was the last one processed by mv.

As an aside, when I sudo find, I ALWAYS use the --target-directory switch to mv, and run the find with echo first, to ensure it does what I want, like this:

# NOTE: This demonstrates a FAIL
sudo find . -type f -iname '*.pdf' -exec echo mv --target-directory=/pdf {}  \;

But, I'd probably do it like this:

mkdir ./pdf
find . -type f -iname '*.pdf' -print0 | xargs -0 mv --target-directory=./pdf

mv will show an error message if the argument to --target-directory does not exist.
I use find -print0 paired with xargs -0 to deal with file names containing spaces and other silly characters.

2
5

That's not a folder. Folders show up with a d in front of the other permissions on the far left. You'd see something like drwx-r-xr-x if it was a folder.

Instead, you've renamed each file /pdf. Renaming a file to a name that already exists deletes the original file. That means you've deleted your files, except for the last one you renamed.


Another related question deals with recovering the lost PDFs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .