user1701840
user1701840

Reputation: 1112

how do I move specific files from one directory to other using xargs?

suppose I type this command:

find /etc/info/ -name ".c" | xargs -I {} grep -l 'importantFile' {}

Now I have all the files that I am interested, which has the suffix of .c and keywords "importantFile". How do I move it to one of my current directory(name: folder)?

I tried:

find /etc/info/ -name ".c" | xargs -I {} grep -l 'importantFile' {} mv{} ./folder

and it doesn't work. Please help :p

Upvotes: 2

Views: 2665

Answers (2)

user2548919
user2548919

Reputation: 131

This works for me moving copying some PDF

find . -name "*.pdf" | xargs -I % cp % ../testTarget/

So for your example it should be

 find /etc/info/ -name "*.c" | xargs -I % mv % ./folder

Upvotes: 1

Amit
Amit

Reputation: 20456

If you like to stick with find, something like this should work:

xargs -r0 --arg-file <(find . -name "*.c" -type f -exec grep -lZ importantFile {} +
  ) mv -i --target-directory ./folder

Try this

grep -lir 'importantFile' /etc/info/*.c | xargs mv -t ./folder

Upvotes: 2

Related Questions