0

How can I delete with a script each file with an extension that is present in a list of file extensions? The list must be included in the script itself as I need to remove easily a bunch of files that are generated when I run another program.

I want to delete all the files with certain extensions from the current directory only (no subdirs) without returning errors if they don't exist.

I want to run the script from terminal. The extensions will always be the same. I need to delete e.g. some .log and .txt that are generated from another program but I don't need, so I don't need any command line arguments, just $ myscript.

I don't know pretty anything about bash scripting. I wrote some tests but I deleted them because they didn't work. In pseudocode it should be something like this:

myexts = .log, .txt, .somethingelse
for ext in myexts:
    if exists *.ext
        delete *.ext

I tried the code suggested by ilkkachu but I get

$ ./myscript
find: ‘’: No such file or directory
find: ‘’: No such file or directory
find: ‘’: No such file or directory

I tried to change the extensions in the list and to create files with the extensions in the original list but I always get this error.

4
  • 1
    Do you want to delete all files with certain extensions in the current directory (or other directories?), or do you want to check whether a given file has an extension matching a list of extensions before deleting it? Jul 8 '20 at 9:02
  • Please add more details how you want to specify the list of file extensions and how you want to run the script. (Should the script have command line arguments? ...) Is a command like rm *.foo *.bar *.baz sufficient?
    – Bodo
    Jul 8 '20 at 9:14
  • What extensions? Will they always be the same or do you need to be able to choose them when you launch the script? Will the files always be in the same directory or will you need to recurse into subdirectories? Please edit your question and give us more details. Maybe an example of the file you want to delete and the files you want to keep. Also, please show us what you have done so far so we don't repeat your work.
    – terdon
    Jul 8 '20 at 9:21
  • @Marin, sorry, I wasn't too clear. Replace "$dir" in the script with the actual target directory, or just . if you want to start at the current directory. I edited the answer a bit.
    – ilkkachu
    Jul 8 '20 at 11:54
2

Assuming you have Bash or ksh:

#!/bin/bash
extensions=(c~ h~ bak)
for e in "${extensions[@]}"; do
    find . -name "*.$e" -delete
done

Or substitute -exec rm -- {} + for -delete if your find doesn't have the latter. Of course you may also want to run a test run without -delete first to see the files that would be deleted.

The above would delete the files in all subdirectories. If you only want to delete the files in the current directory (not subdirectories), then we can use rm instead of find:

#!/bin/bash
extensions=(c~ h~ bak)
shopt -s nullglob
for e in "${extensions[@]}"; do
    rm ./*."$e"
done

Or just simply

#!/bin/bash
shopt -s nullglob
rm -- *.c~ *.h~ *.bak

A more general version would take the directory and the extensions as arguments to the script:

#!/bin/sh
dir=$1
shift
for e in "$@"; do
    find "$dir" -name "*.$e" -delete
done

You'd run that with rmscript.sh /some/target/dir c~ h~ bak.

1
  • 1
    @StephenKitt, Yngh. Code-golf points to you, but perhaps rm -- ${extensions[*]/#/*.} or rm ${extensions[*]/#/./*.}.
    – ilkkachu
    Jul 8 '20 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.