You are not logged in.

#1 2021-06-27 08:34:11

amaro
Member
From: xfce
Registered: 2014-05-09
Posts: 367

[SOLVED] mv command for multiple files using fd and grep

Hello everybody!

I am trying to move multiple 'pdf' files containing specific word in the filename from different folders to a new folder on another partition.
I am using the 'mv' command

mv $(fd linux | grep pdf) /new/partition/new/folder/

It works as long as there are no spaces in the filenames.

If there are spaces in the filename I get

mv: cannot stat 'Downloads/Linux': No such file or directory
mv: cannot stat 'commands': No such file or directory
mv: cannot stat 'cheat': No such file or directory
mv: cannot stat 'sheet': No such file or directory
mv: cannot stat 'by': No such file or directory
mv: cannot stat 'PhoenixNAP.pdf': No such file or directory

I did some gooooooogling and found an alternative way to do it

fd linux | grep pdf > pdf_list
while IFS= read -r file; do mv -- "$file" "/new/partition/new/folder/"; done < pdf_list

Any suggestions how to avoid errors with the 'mv' command when the 'fd' command finds file(s) with spaces in the filename are appreciated!

Thank you in advance!

Last edited by amaro (2021-06-27 10:02:52)

Offline

#2 2021-06-27 09:15:41

flyingscorpio
Member
Registered: 2020-06-04
Posts: 34

Re: [SOLVED] mv command for multiple files using fd and grep

How about the -exec flag of find?

find -iname '*linux*.pdf' -exec mv {} /new/location/ \;

Last edited by flyingscorpio (2021-06-27 09:27:20)

Offline

#3 2021-06-27 09:30:17

loqs
Member
Registered: 2014-03-06
Posts: 19,009

Re: [SOLVED] mv command for multiple files using fd and grep

find -name '*linux*.pdf' -exec mv '{}' /new/partition/new/folder/ \;

I think the equivalent using fd is:

fd -g '*linux*.pdf' -x mv '{}' /new/partition/new/folder/ \;

Offline

#4 2021-06-27 09:35:53

amaro
Member
From: xfce
Registered: 2014-05-09
Posts: 367

Re: [SOLVED] mv command for multiple files using fd and grep

I tried

find . -iname "quotes" -exec mv {} /new/partition/new/folder \;

but no file was moved.

Offline

#5 2021-06-27 09:40:29

loqs
Member
Registered: 2014-03-06
Posts: 19,009

Re: [SOLVED] mv command for multiple files using fd and grep

Why did you use "quotes" instead of '*linux*.pdf' ?  Was that the actual command?
Edit:

$ mkdir -p src/subdir dest/anotherdir
$ touch 'src/subdir/some name with linux in it .pdf'
$ find src dest
src
src/subdir
src/subdir/some name with linux in it .pdf
dest
dest/anotherdir
$ find -name '*linux*.pdf' -exec mv '{}' dest/anotherdir \;
find src dest
src
src/subdir
dest
dest/anotherdir
dest/anotherdir/some name with linux in it .pdf

Last edited by loqs (2021-06-27 09:48:29)

Offline

#6 2021-06-27 09:56:35

amaro
Member
From: xfce
Registered: 2014-05-09
Posts: 367

Re: [SOLVED] mv command for multiple files using fd and grep

@loqs

I am moving lots of files. The 'quotes' was used when I tried to move files containing 'quotes' in the filename. Used it to show the command I tried.

Anyway, I have just tried your version of the 'find' command with the word 'linux'.

$ fd linux | grep pdf
Downloads/Linux commands cheat sheet by PhoenixNAP.pdf
$ find -name '*linux*.pdf' -exec mv '{}' /new/partition/TB/quotes/ \;
$ ls /new/partition/TB/quotes/

It did not work.

So I tried the 'fd' command you suggested

$ fd -g '*linux*.pdf' -x mv '{}' /new/partition/TB/quotes/ \;
$ ls /new/partition/TB/quotes/
'Linux commands cheat sheet by PhoenixNAP.pdf'

It worked.

Thank you, loqs!

Last edited by amaro (2021-06-27 09:57:26)

Offline

#7 2021-06-27 10:02:49

loqs
Member
Registered: 2014-03-06
Posts: 19,009

Re: [SOLVED] mv command for multiple files using fd and grep

$ fd linux | grep pdf
Downloads/Linux commands cheat sheet by PhoenixNAP.pdf
$ find -name '*linux*.pdf' -exec mv '{}' /new/partition/TB/quotes/ \;
$ ls /new/partition/TB/quotes/

Case mismatch linux will not match Linux using -name you would need -iname.

Offline

#8 2021-06-27 10:05:16

amaro
Member
From: xfce
Registered: 2014-05-09
Posts: 367

Re: [SOLVED] mv command for multiple files using fd and grep

Absolutely!
Thank you once again, loqs!

Offline

Board footer

Powered by FluxBB