Use Grep to Find Content in Files and Move Them If They Match

Use grep to find content in files and move them if they match

If you want to find and move files that do not match your pattern (move files that don't contain 'Subject \[SPAM\]' in this example) use:

grep -L -Z -r 'Subject: \[SPAM\]' . | xargs -0 -I{} mv {} DIR

The -Z means output with zeros (\0) after the filenames (so spaces are not used as delimeters).

xargs -0

means interpret \0 to be delimiters.

The -L means find files that do not match the pattern. Replace -L with -l if you want to move files that match your pattern.

Then

-I{} mv {} DIR

means replace {} with the filenames, so you get mv filenames DIR.

Moving files with a certain extension using grep

for that I would use find:

 find /original/file/path/ -maxdepth 1 -name '*.txt'  -exec mv {} /the/new/path/ \;

explaining the parameters:

  • look for files in /original/file/path/
  • only on the current folder (find will go deep in the tree if you want)
  • with the name *.txt
  • and execute the command mv replacing {} by each file found.

Find text in files and than move those files into new location

To copy all matching files to a directory you can write:

grep -rl "<text>" /path  | xargs cp -t /path/to/destination

grep filenames matching a pattern and move to desired folder

I think glob expansion is the way to go here:

while read pattern; do
mv "${pattern}"* ../folder_b/"$pattern"
done < list.txt

Start with an echo in front of the mv command, and remove it when you're happy with the output.

Move and delete all files matching grep in folder

Looks like a job for xargs:

find buffer/ | grep -i "$varname" | xargs -n1 -I{} -- zip "output/{}.zip" {}

For varname="" and a folder buffer created like this:

mkdir buffer
touch buffer/1 buffer/2

The command will execute:

zip output/buffer/1.zip buffer/1
zip output/buffer/1.zip buffer/2

Probably you would want to remove the buffer part from the filename, we can use sed 's#^buffer/##' for that. We can instruct find to list only files with -type f. So the following:

find buffer/ -type f | grep -i "$varname" | sed 's#buffer/##' | xargs -n1 -I{} -- zip "output/{}.zip" buffer/{}

will execute:

zip output/1.zip buffer/1
zip output/2.zip buffer/2

To delete the original files, we can do smth like this:

find buffer/ -type f | grep -i "$varname" | sed 's#buffer/##' | xargs -n1 -I{} -- bash -c 'zip "output/{}.zip" buffer/{}; rm "buffer/{}"'

If you want to iterate over the files and do smth more advanced you can use a while read line:

find buffer/ -type f | grep -i "$varname" | sed 's#buffer/##' \
| while read -r line; do
zip output/${line}.zip buffer/${line}
rm buffer/${line}
done

How to find all files containing specific text (string) on Linux?

Do the following:

grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
  • -e is the pattern used during the search

Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:

  • This will only search through those files which have .c or .h extensions:

    grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
  • This will exclude searching all the files ending with .o extension:

    grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
  • For directories it's possible to exclude one or more directories using the --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:

    grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"

This works very well for me, to achieve almost the same purpose like yours.

For more options, see man grep.

Find content of one file from another file in UNIX

One way with awk:

awk -v FS="[ =]" 'NR==FNR{rows[$1]++;next}(substr($NF,1,length($NF)-1) in rows)' File1 File2

This should be pretty quick. On my machine, it took under 2 seconds to create a lookup of 1 million entries and compare it against 3 million lines.

Machine Specs:

Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz (8 cores)
98 GB RAM

How to find and move all files matching given Beginning Of File (BOF) string?

Use a Perl one-liner in combination with find and xargs, like so:

echo foo > 1.txt
echo "foo\nbar" > 2.txt
echo "bar\nfoo" > 3.txt
mkdir foodir
find . -maxdepth 1 -name '[123].txt' -exec perl -lne 'print $ARGV if /^foo/; last;' {} \; | xargs -I{} mv {} foodir
find foodir -type f
# foodir/2.txt
# foodir/1.txt

How do I use grep to search the current directory for all files having a given string and then move these files to a new folder?

You should use grep from within find:

find /path/to/dir -type f -exec grep -q "zone 19" {} \; -exec mv -i {} zone19 \;


Related Topics



Leave a reply



Submit