How to move files using the `find` command?

#1

Hallo
I have a lot of PDF documents. I diceded to sort all of them. First off all I did was searching for duplicate files with fdupes. I delete all duplicated files, and now I want to move all PDF files on one location. Off course I want to use CLI. And thats why I need a help.
I have tried so:
find ~/Desktop/1-9-5 -name ‘*.pdf’ | mv /home/najkarika/Desktop/345/
but something was wrong.
Please help me.
Thanks

1 Like

#2

That doesn’t work. My not try:

# cd ~/Desktop/1-9-5
# mv *.pdf /home/najkarika/Desktop/345/

I hope that works, it’s not pretty though.

Something more pretty is this:

# find ./ -name '*.pdf' -exec mv {}  /home/najkarika/Desktop/345/
2 Likes

#3

You cannot use pipe with find and mv command. Move command doesn’t work with stdin. You need to use exec here in this fashion:

find ~/Desktop/1-9-5 -name ‘*.pdf’ -exec mv {} /home/najkarika/Desktop/345/ \;

1 Like

#4

Thanks for your answers. But it didnt help. Only with:

find ~/Desktop/1-9-5 -name “*.pdf” -exec mv /home/najkarika/Desktop/345/ {} ;

Became I answer: mv: cannot overwrite non-directory

0 Likes

#5

@Abhishek made a typo. The mv arguments should be in source, destination order:

find ~/Desktop/1-9-5 -name '*.pdf' -exec mv {} /home/najkarika/Desktop/345/ \;

The -exec option tells to the find command to execute another command on each matching file. \; marks the end of that external command. And {} is a placeholder for the name of the matching file currently examined by find.

EDIT: take care of not mistakenly use the backtick ` instead of a single quote ’
EDIT2: I took the liberty of changing the thread title. Don’t hesitate to change it back if you disagree.

4 Likes

#6

That’s it. Perfect. Thank you.
All PDFs are in one Folder.
And the name for thread is also great.

2 Likes

#7

Thanks for spotting that. I was in a hurry :slight_smile:

2 Likes

#8

When i using find and -exec. Hope you got some more idea or so…

find /home -type f -name "*.txt" -exec cp {} . \; find all txt and copy in current dir
find /home -type f -name "*.txt" -exec rm {} \; find all txt and delete
find / -type f -perm 0777 -print -exec chmod 644{} \; find all of 777 perm and chmod to 644

And ofc. I occasional use and tar command when i back-up something.
like find all *.conf files and tar them. u can use -p option in tar to save permission

1 Like