Skip to main content
Protect against patterns and filenames that start with dashes, and also show how to do it in one grep pre file
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

I move (mv) files to an outside directory (SAFE_DIR_FOR_DEL) -PLEASE CREATE THIS directory BEFORE EXECUTING the loops- in order to check them later for safety, then you can delete the whole directory. Also, this solution can find multiple patterns per file.

str_rp = You put here whatever you would like to find. Note that some characters may need to be escaped eg. !, "", (), etc (see grep/regex help). You may change the files that you like to search the grep string eg. *.html, *.txt or whatever.

Note that if any pattern of 2 are found, the file is moved into the SAFE_DIR_FOR_DEL. If you like to move files in two different directories, you can easily change the name of the folder in 2nd pattern, but first you must have create it.

CAUTION: before executing it, you try it in some files in an temporary folder with temporary files, with temporary safe dir structure.

i=1; for fl in *.html; do 

  str_rp="1st pattern"; 
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK1-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 1st"
  fi

  # if file moved from 1st grep, then 2nd grep may result normally an error "No such file or directory".
  str_rp="2nd pattern";
  if n=$(grep "$str_rp"  "$fl"); then
    echo "OK2-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 2nd"
  fi

  echo $i;  let "i+=1";
done;

Or, more concisely:

for pattern in "1st pattern" "2nd pattern"; do
  for file in *.html; do
     if grep -q -e "$pattern" -- "$file"; then
        mv -- "$file" ../SAFE_DIR_FOR_DEL/
     fi
  done
done

or

for file in *.html; do
   if grep -q -e "1st pattern" -e "2nd pattern" -- "$file"; then
      mv -- "$file" ../SAFE_DIR_FOR_DEL/
   fi
done

I move (mv) files to an outside directory (SAFE_DIR_FOR_DEL) -PLEASE CREATE THIS directory BEFORE EXECUTING the loops- in order to check them later for safety, then you can delete the whole directory. Also, this solution can find multiple patterns per file.

str_rp = You put here whatever you would like to find. Note that some characters may need to be escaped eg. !, "", (), etc (see grep/regex help). You may change the files that you like to search the grep string eg. *.html, *.txt or whatever.

Note that if any pattern of 2 are found, the file is moved into the SAFE_DIR_FOR_DEL. If you like to move files in two different directories, you can easily change the name of the folder in 2nd pattern, but first you must have create it.

CAUTION: before executing it, you try it in some files in an temporary folder with temporary files, with temporary safe dir structure.

i=1; for fl in *.html; do 

  str_rp="1st pattern"; 
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK1-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 1st"
  fi

  # if file moved from 1st grep, then 2nd grep may result normally an error "No such file or directory".
  str_rp="2nd pattern";
  if n=$(grep "$str_rp"  "$fl"); then
    echo "OK2-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 2nd"
  fi

  echo $i;  let "i+=1";
done;

Or, more concisely:

for pattern in "1st pattern" "2nd pattern"; do
  for file in *.html; do
     if grep -q "$pattern" "$file"; then
        mv -- "$file" ../SAFE_DIR_FOR_DEL/
     fi
  done
done

I move (mv) files to an outside directory (SAFE_DIR_FOR_DEL) -PLEASE CREATE THIS directory BEFORE EXECUTING the loops- in order to check them later for safety, then you can delete the whole directory. Also, this solution can find multiple patterns per file.

str_rp = You put here whatever you would like to find. Note that some characters may need to be escaped eg. !, "", (), etc (see grep/regex help). You may change the files that you like to search the grep string eg. *.html, *.txt or whatever.

Note that if any pattern of 2 are found, the file is moved into the SAFE_DIR_FOR_DEL. If you like to move files in two different directories, you can easily change the name of the folder in 2nd pattern, but first you must have create it.

CAUTION: before executing it, you try it in some files in an temporary folder with temporary files, with temporary safe dir structure.

i=1; for fl in *.html; do 

  str_rp="1st pattern"; 
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK1-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 1st"
  fi

  # if file moved from 1st grep, then 2nd grep may result normally an error "No such file or directory".
  str_rp="2nd pattern";
  if n=$(grep "$str_rp"  "$fl"); then
    echo "OK2-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 2nd"
  fi

  echo $i;  let "i+=1";
done;

Or, more concisely:

for pattern in "1st pattern" "2nd pattern"; do
  for file in *.html; do
     if grep -q -e "$pattern" -- "$file"; then
        mv -- "$file" ../SAFE_DIR_FOR_DEL/
     fi
  done
done

or

for file in *.html; do
   if grep -q -e "1st pattern" -e "2nd pattern" -- "$file"; then
      mv -- "$file" ../SAFE_DIR_FOR_DEL/
   fi
done
Removed incorrect statement (it is the glob (*) that fails, not grep or find); provided more concise and easier to read version using the same basic style
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

if the files of a directory are many eg. 300thousands then find / grep without loop will fail.

I move (mv) files to an outside directory (SAFE_DIR_FOR_DEL) -PLEASE CREATE THIS directory BEFORE EXECUTING the loops- in order to check them later for safety, then you can delete the whole directory. Also, this solution can find multiple patterns per file.

str_rp = You put here whatever you would like to find. Note that some characters may need to be escaped eg. !, "", (), etc (see grep/regex help). You may change the files that you like to search the grep string eg. *.html, *.txt or whatever.

Note that if any pattern of 2 are found, the file is moved into the SAFE_DIR_FOR_DEL. If you like to move files in two different directories, you can easily change the name of the folder in 2nd pattern, but first you must have create it.

CAUTION: before executing it, you try it in some files in an temporary folder with temporary files, with temporary safe dir structure.

i=1; for fl in *.html; do 

  str_rp="1st pattern"; 
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK1-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 1st"
  fi

  # if file moved from 1st grep, then 2nd grep may result normally an error "No such file or directory".
  str_rp="2nd pattern";
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK2-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 2nd"
  fi

  echo $i;  let "i+=1";
done;

Or, more concisely:

for pattern in "1st pattern" "2nd pattern"; do
  for file in *.html; do
     if grep -q "$pattern" "$file"; then
        mv -- "$file" ../SAFE_DIR_FOR_DEL/
     fi
  done
done

if the files of a directory are many eg. 300thousands then find / grep without loop will fail.

I move (mv) files to an outside directory (SAFE_DIR_FOR_DEL) -PLEASE CREATE THIS directory BEFORE EXECUTING the loops- in order to check them later for safety, then you can delete the whole directory. Also, this solution can find multiple patterns per file.

str_rp = You put here whatever you would like to find. Note that some characters may need to be escaped eg. !, "", (), etc (see grep/regex help). You may change the files that you like to search the grep string eg. *.html, *.txt or whatever.

Note that if any pattern of 2 are found, the file is moved into the SAFE_DIR_FOR_DEL. If you like to move files in two different directories, you can easily change the name of the folder in 2nd pattern, but first you must have create it.

CAUTION: before executing it, you try it in some files in an temporary folder with temporary files, with temporary safe dir structure.

i=1; for fl in *.html; do 

  str_rp="1st pattern"; 
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK1-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 1st"
  fi

  # if file moved from 1st grep, then 2nd grep may result normally an error "No such file or directory".
  str_rp="2nd pattern";
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK2-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 2nd"
  fi

  echo $i;  let "i+=1";
done;

I move (mv) files to an outside directory (SAFE_DIR_FOR_DEL) -PLEASE CREATE THIS directory BEFORE EXECUTING the loops- in order to check them later for safety, then you can delete the whole directory. Also, this solution can find multiple patterns per file.

str_rp = You put here whatever you would like to find. Note that some characters may need to be escaped eg. !, "", (), etc (see grep/regex help). You may change the files that you like to search the grep string eg. *.html, *.txt or whatever.

Note that if any pattern of 2 are found, the file is moved into the SAFE_DIR_FOR_DEL. If you like to move files in two different directories, you can easily change the name of the folder in 2nd pattern, but first you must have create it.

CAUTION: before executing it, you try it in some files in an temporary folder with temporary files, with temporary safe dir structure.

i=1; for fl in *.html; do 

  str_rp="1st pattern"; 
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK1-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 1st"
  fi

  # if file moved from 1st grep, then 2nd grep may result normally an error "No such file or directory".
  str_rp="2nd pattern";
  if n=$(grep "$str_rp"  "$fl"); then
    echo "OK2-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 2nd"
  fi

  echo $i;  let "i+=1";
done;

Or, more concisely:

for pattern in "1st pattern" "2nd pattern"; do
  for file in *.html; do
     if grep -q "$pattern" "$file"; then
        mv -- "$file" ../SAFE_DIR_FOR_DEL/
     fi
  done
done
fixed formatting
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

if the files of a directory are many eg. 300thousands then find / grep without loop will fail.

I move (mv) files to an outside directory (SAFE_DIR_FOR_DEL) -PLEASE CREATE THIS directory BEFORE EXECUTING the loops- in order to check them later for safety, then you can delete the whole directory. Also, this solution can find multiple patterns per file.

str_rp = You put here whatever you would like to find. Note that some characters may need to be escaped eg. !, "", (), etc (see grep/regex help). You may change the files that you like to search the grep string eg. *.html, *.txt or whatever.

Note that if any pattern of 2 are found, the file is moved into the SAFE_DIR_FOR_DEL. If you like to move files in two different directories, you can easily change the name of the folder in 2nd pattern, but first you must have create it.

CAUTION: before executing it, you try it in some files in an temporary folder with temporary files, with temporary safe dir structure.

i=1; for fl in *.html; do 

str_rp="1st pattern"; 
if n=$(grep  "$str_rp"  "$fl"); then
echo "OK1-" "$fl"
mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
else
echo "NONE 1st"
fi

# if file moved from 1st grep, then 2nd grep may result normally an error "No such file or directory".
str_rp="2nd pattern";
if n=$(grep  "$str_rp"  "$fl"); then
echo "OK2-" "$fl"
mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
else
echo "NONE 2nd"
fi

echo $i;  let "i+=1";
done;
i=1; for fl in *.html; do 

  str_rp="1st pattern"; 
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK1-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 1st"
  fi

  # if file moved from 1st grep, then 2nd grep may result normally an error "No such file or directory".
  str_rp="2nd pattern";
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK2-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 2nd"
  fi

  echo $i;  let "i+=1";
done;

if the files of a directory are many eg. 300thousands then find / grep without loop will fail.

I move (mv) files to an outside directory (SAFE_DIR_FOR_DEL) -PLEASE CREATE THIS directory BEFORE EXECUTING the loops- in order to check them later for safety, then you can delete the whole directory. Also, this solution can find multiple patterns per file.

str_rp = You put here whatever you would like to find. Note that some characters may need to be escaped eg. !, "", (), etc (see grep/regex help). You may change the files that you like to search the grep string eg. *.html, *.txt or whatever.

Note that if any pattern of 2 are found, the file is moved into the SAFE_DIR_FOR_DEL. If you like to move files in two different directories, you can easily change the name of the folder in 2nd pattern, but first you must have create it.

CAUTION: before executing it, you try it in some files in an temporary folder with temporary files, with temporary safe dir structure.

i=1; for fl in *.html; do 

str_rp="1st pattern"; 
if n=$(grep  "$str_rp"  "$fl"); then
echo "OK1-" "$fl"
mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
else
echo "NONE 1st"
fi

# if file moved from 1st grep, then 2nd grep may result normally an error "No such file or directory".
str_rp="2nd pattern";
if n=$(grep  "$str_rp"  "$fl"); then
echo "OK2-" "$fl"
mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
else
echo "NONE 2nd"
fi

echo $i;  let "i+=1";
done;

if the files of a directory are many eg. 300thousands then find / grep without loop will fail.

I move (mv) files to an outside directory (SAFE_DIR_FOR_DEL) -PLEASE CREATE THIS directory BEFORE EXECUTING the loops- in order to check them later for safety, then you can delete the whole directory. Also, this solution can find multiple patterns per file.

str_rp = You put here whatever you would like to find. Note that some characters may need to be escaped eg. !, "", (), etc (see grep/regex help). You may change the files that you like to search the grep string eg. *.html, *.txt or whatever.

Note that if any pattern of 2 are found, the file is moved into the SAFE_DIR_FOR_DEL. If you like to move files in two different directories, you can easily change the name of the folder in 2nd pattern, but first you must have create it.

CAUTION: before executing it, you try it in some files in an temporary folder with temporary files, with temporary safe dir structure.

i=1; for fl in *.html; do 

  str_rp="1st pattern"; 
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK1-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 1st"
  fi

  # if file moved from 1st grep, then 2nd grep may result normally an error "No such file or directory".
  str_rp="2nd pattern";
  if n=$(grep  "$str_rp"  "$fl"); then
    echo "OK2-" "$fl"
    mv -- "$fl" ../SAFE_DIR_FOR_DEL/;
  else
    echo "NONE 2nd"
  fi

  echo $i;  let "i+=1";
done;
added 108 characters in body
Source Link
Estatistics
  • 350
  • 1
  • 4
  • 16
Loading
Rollback to Revision 3
Source Link
Estatistics
  • 350
  • 1
  • 4
  • 16
Loading
deleted 743 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
added 9 characters in body
Source Link
Estatistics
  • 350
  • 1
  • 4
  • 16
Loading
added 121 characters in body
Source Link
Estatistics
  • 350
  • 1
  • 4
  • 16
Loading
Post Made Community Wiki
Source Link
Estatistics
  • 350
  • 1
  • 4
  • 16
Loading