cancel
Showing results for 
Search instead for 
Did you mean: 

Script writting

 
SOLVED
Go to solution
Highlighted
Regular Advisor

Script writting

There are some files under the path /tmp , if I want to remove all the files which contains the word "testing" , what can I do ? thx
13 REPLIES 13
Highlighted
Trusted Contributor
Solution

Re: Script writting

Try:

#find . -type f |xargs grep test|awk -F ":" '{print $1}' |uniq

to see if files are matched correctly , then

#find . -type f |xargs grep test|awk -F ":" '{print $1}' |uniq |xargs rm
Highlighted
Trusted Contributor

Re: Script writting

s/find ./find /tmp/g
Highlighted
Regular Advisor

Re: Script writting

thx reply ,

it is strange that I try it in two machines , one is perfectly work , but another machine can only show the word "testing" in the result ( what I need is the file name not the word I search ) , can advise what is wrong in this machine ? thx

$find . -type f -maxdepth 1 |xargs grep testing|awk -F ":" '{print $1}' |uniq
testing
Highlighted
Honored Contributor

Re: Script writting

Well, if that find on one machine, only finds 1 file, then grep will by default NOT output the filename ebfore each match, just show the match.
On the other machine there must have been more than one matches for the find, and xargs gives more than 1 input file to grep, and grep then knows it needs to tell you which file gave the match.

You can force this using: grep -H for --with-filename

(on my version of grep. Check with grep --help)

hth,
Hein van den Heuvel
Highlighted
Regular Advisor

Re: Script writting

thx Hein van den ,

It works fine now after add -H , I still have a question , if I want to move it to another directory , I try


find . -type f -maxdepth 1 |xargs grep testing|awk -F ":" '{print $1}' |uniq |xargs mv /tmp/ora , it is not work , can advise what is wrong ? thx

mv: cannot overwrite non-directory `./abc' with directory `/tmp/ora/'
Highlighted
Honored Contributor

Re: Script writting

You may have to drop the final xargs.
It combines multiple arguments for one operation. That's fine for 'rm' but tricky for 'mv'.

Just replace the 'mv' with 'echo' and see in clear print what you were about to ask the system to do!

Hein.

Highlighted
Regular Advisor

Re: Script writting

thx Hein van den ,

I am not too understand , can advise what I need to do ? thx
Highlighted
Regular Advisor

Re: Script writting

Thx reply ,

I have solved it , thx much for help.
Highlighted
Honored Contributor

Re: Script writting

Why not use 'find' for almost-everything?

find /tmp -type f -exec grep "testing" {} \; -exec mv {} /other/path/ \;

Same format can be used for the 'rm'.
One long-haired git at your service...