Operating System - Tru64 Unix
1748146 Members
3705 Online
108758 Solutions
New Discussion юеВ

renaming files containing a pattern

 
SOLVED
Go to solution
Eilish
New Member

renaming files containing a pattern

i'm not very up on scripting so apologies if this is in the wrong place and/ or is a stupid question.

i'm trying to find a way to list all the files in a given directory that contacin a certain pattern and then move all the matching files into another directory.
i've tried every combination of grep and find i can think of but i'm getting nowhere fast
most recently i tried the snippet below but no joy. any advice would be much appreciated

for file in 'grep -l "pattern" *'
do
mv $file /dir
done
9 REPLIES 9
Ralf Puchner
Honored Contributor

Re: renaming files containing a pattern

why not using the find command with parameter "exec"?
Help() { FirstReadManual(urgently); Go_to_it;; }
Eilish
New Member

Re: renaming files containing a pattern

i'm even worse with find than i am with grep!

how do i list the files containing a pattern with find,
i've only used find when i know the filenames before.

Hein van den Heuvel
Honored Contributor
Solution

Re: renaming files containing a pattern

Ralf, I think he looks for 'file containing pattern' not 'filename match pattern'.

Eilish, You are on the right track. Just need a back-tick instead of a single quotes!
This works for me:

$ for file in `grep -l aap x*`
> do
> echo "File " $file " should be moved"
> done
File x should be moved
File x1 should be moved
File x3 should be moved


I might be tempted to use xargs though!

grep -l aap x* | xargs -i echo "move file {} please!"
move file x please!
move file x1 please!
move file x3 please!

Or awk:

grep -l aap x* | awk '{system("echo move " $1 " now.")}'

Or a perl script to glob to the directory, open, read, on patternmatch close and move.

Hein.



Ninad_1
Honored Contributor

Re: renaming files containing a pattern

Try This


for i in `ls -l | tr -s " " " " | cut -f 9 -d " "`
do
if test `grep -c pattern $i` != "0"
then
mv $i /dir
fi
done

Regards
Ninad
Michael Schulte zur Sur
Honored Contributor

Re: renaming files containing a pattern

Hi,

what is grep for? Example move all files with ending text:
for FILE in *.txt
do
mv ${FILE} /dir/
done
Eilish
New Member

Re: renaming files containing a pattern

you're a star, that works perfectly!

thanks for your help I've been pulling my hair out trying to get it right.
Michael Schulte zur Sur
Honored Contributor

Re: renaming files containing a pattern

Well, I may have understood you wrong.

try this:
grep -l "pattern" searchfiles|xargs -n1 -I{} mv "{}" /dir/

greetings,

Michael
Joris Denayer
Respected Contributor

Re: renaming files containing a pattern

Eilish,

What works fine now ?

There are scripts that will move the file when the content of the file contains a pattern.
And there is a script that will move the file when the filename contains a certain pattern.
To err is human, but to really faul things up requires a computer
Ralf Puchner
Honored Contributor

Re: renaming files containing a pattern

or

ls -Tl | awk '($9==1999){print $10}' | xargs mv /home/darryl/test

will move all files with date 1999 to /home/darryl/test....

Help() { FirstReadManual(urgently); Go_to_it;; }