Delete Files with String Found in File - Linux Cli

Delete files with string found in file - Linux cli

For safety I normally pipe the output from find to something like awk and create a batch file with each line being "rm filename"

That way you can check it before actually running it and manually fix any odd edge cases that are difficult to do with a regex

find . | xargs grep -l email@example.com | awk '{print "rm "$1}' > doit.sh
vi doit.sh // check for murphy and his law
source doit.sh

How to delete from a text file, all lines that contain a specific string?

To remove the line and print the output to standard out:

sed '/pattern to match/d' ./infile

To directly modify the file – does not work with BSD sed:

sed -i '/pattern to match/d' ./infile

Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

sed -i '' '/pattern to match/d' ./infile

To directly modify the file (and create a backup) – works with BSD and GNU sed:

sed -i.bak '/pattern to match/d' ./infile

Find specific string in files then delete other files with same filename

Thank you all especially @Barmar. I got it to work with

for file in $(grep -l "string" *.alias)
do
prefix=${file%%~*}
rm $prefix*.*
done

How to mass remove files that contain special characters in file name

This will delete every file whose name ends in (1), recursively:

find . -name '*(1)' -exec rm {} +
  • -name '*(1) (1)' to only delete files ending with a double 1.
  • -name '*([0-9])' will match any single digit.
  • find . -mindepth 1 -maxdepth 1 -name '*(1)' -exec rm {} + for no recursion.
  • I would do find . -name '*(1)' -exec echo rm {} \; to print a neat list of the rm commands to be executed. Review it, then remove echo to run for real.
  • Changing \; back to + is optional, + is more efficient.

Find and delete files that contain same string in filename in linux terminal

I only now understand your question, I think. You want to remove all files that contain a numeric value that is not unique (in a particular folder). If a filename contains a value that is also found in another filename, you want to remove both files, right?

This is how I would do that (it may not be the fastest way):

# put all files in your folder in a list
# for array=(*) to work make sure you have enabled nullglob: shopt -s nullglob
array=(*)
delete=()

for elem in "${array[@]}"; do
# for each elem in your list extract the number
num_regex='([0-9]+)\.'
[[ "$elem" =~ $num_regex ]]
num="${BASH_REMATCH[1]}"
# use the extracted number to check if it is unique
dup_regex="[^0-9]($num)\..+?(\1)"
# if it is not unique, put the file in the files-to-delete list
if [[ "${array[@]}" =~ $dup_regex ]]; then
delete+=("$elem")
fi
done

# delete all found duplicates
for elem in "${delete[@]}"; do
rm "$elem"
done

In your example, array would be:

array=(werrt-110009.jpg asfff-110009.JPG asffa-123489.jpg asffa-111122.JPG)

And the result in delete would be:

delete=(werrt-110009.jpg asfff-110009.JPG)

Is this what you meant?

Bash - how to find and remove all files that contain a given string?

BrowSlow's answer (in the comments) is correct, except for the escape of the /for/endetta.
Try this command:

find . -type f -exec grep -q '\\/for\\/endetta' {} \; -delete 

If the exit status of grep is true, the following action is executed. Replace -delete with -print to see which files would be deleted.

How do I delete all the files that contain a certain string in a directory in UNIX?



rm *\).MOV

Linux command to delete file content

The '>' character is the shell (often Bash) redirection character. You are at a command prompt of the shell, probably Bash but you don't specify. The command as you have written basically says "redirect <nothing> to the file /AAA/BBB/CCC/DDD/main_copy.json". The net result is to truncate the file to zero length, effectively deleting its contents.

Since there are no spaces in the argument to '>', bash treats it as a single argument, thus there is no possibility that your command will delete contents of any files in any of the directories in your path. In any case, the '>' redirect operator does not work with multiple arguments. So if you were to say:

[user@yyy ~]$ > /AAA /BBB/CCC/DDD/main_copy.json

The net effect would issue an error because you can't redirect to a directory, only to a file. If you were to say:

[user@yyy ~]$ > /AAA/myfile.txt BBB/CCC/DDD/main_copy.json

the shell would truncate the file myfile.txt to zero length, (or create a zero-length file if it did not exist) and then treat the second argument as an executable command, which of course would fail with something like "Permission Denied" because it's not an executable.

Hope that helps. Bash (and other shells) is a complicated beast and takes years to really learn well.



Related Topics



Leave a reply



Submit