Move the files which is coming after grepping

Dear Friends,

I am trying to move the files which are listing after greaping command.

see the details below

  1. When i am running the grep command
$  grep -il 'Bufman' *.*
fatal.log
fatal_info.log

it has listed some files now i want to move this files to any another locate so i am trying to write this following command which is not working at all

mv ' grep -il 'Bufman' ~/finacle/RIPU/*' ../MARK/.

Pls help.

Ripudaman Singh

---------- Post updated at 04:23 AM ---------- Previous update was at 04:19 AM ----------

i want to move the files with the grep command only.

mv `grep -il Bufman ~/finacle/RIPU/*` ../MARK/

Or, using $ () instead of old backticks:

mv $(grep -il Bufman ~/finacle/RIPU/*) ../MARK/

If there are a whole bunch (MANY) files to move, and the command length is exceeeded, you would need to use xargs.

It didnt worked.
$ mv `grep -il Bufman /finacle/RIPU/' ../MARK/.
>
$ mv $(grep -il Bufman /finacle/RIPU/
) ../MARK/.
Usage: mv [-f] [-i] [-e warn|force|ignore] f1 f2
mv [-f] [-i] [-e warn|force|ignore] f1 ... fn d1
mv [-f] [-i] [-e warn|force|ignore] d1 d2

---------- Post updated at 05:40 AM ---------- Previous update was at 05:34 AM ----------

$ mv `grep -il 'Bufman' *.*` ../MARK/
mv: fatal.log and ../MARK//fatal.log are identical
mv: fatalinfo.log and ../MARK//fatalinfo.log are identical

try in a loop

grep -il Bufman *.* | while read file
do
echo "Moving $file"
mv $file ../MARK/
done

For the first case:

mv `grep -il Bufman /finacle/RIPU/*' ../MARK/

vs the correct form with backtick instead of quote:

mv `grep -il Bufman /finacle/RIPU/*` ../MARK/

-----------------------
But the backticks or $() are the same, and the second form does not work there, so something else is going on. What does the following produce:

grep -il Bufman /finacle/RIPU/*

../MARK/ depends on the directory you are in when you enter the command, so that is a likely cause. Use an absolute path like /finacle/MARK/ path.

./1.sh[2]: --start: Cannot find or open the file.
Moving 1.sh
mv: 1.sh and ../MARK//1.sh are identical
Moving fatal.log
mv: fatal.log and ../MARK//fatal.log are identical
./1.sh[7]: Syntax error at line 8 : `newline or ;' is not expected.

What does the following produce? If it's a lot of files, show a representative sample.

grep -il Bufman /finacle/RIPU/*

In your script, change ../MARK/ to an absolute path.

Where might --start come from? Figure that out, and you might get somewhere.

Show your current script. And do not type by hand. Copy and paste. And use those code tags. :smiley:

<--start
grep -il Bufman *.* | while read file
do
echo "Moving $file"
mv $file ../MARK/
done
end-->

I am searching file in current directory only

$ echo Bufman > xxx.txt
$ ls
xxx.txt
$ grep -il Bufman *.* | while read file; do echo "Moving $file"; mv -v $file ../MARK/; done
Moving xxx.txt
`xxx.txt' -> `../MARK/xxx.txt'

Do your filenames have blanks?

YES the filies are kind of statement in txt or log format this is just a sample file on which i m trying to do that

Well, if the files have embedded blanks, try using mv -v "$file" syntax instead of mv -v $file syntax.

This will definitely work..

mv `grep -il "Bufman" /finacle/RIPU/*` /finacle/MARK/

Try to use with full path.

you can try the following:

grep -il "Bufman" /finacle/RIPU/* | xargs -0 -I {} mv "{}" /finacle/MARK/

I haven't checked it yet but this should get you the result.

I doubt that will work, since grep isn't delimiting pathnames with a null byte.

Regards,
Alister