1

I have many txt files, named like file1.txt file2.txt file3.txt ... file1000.txt How can I delete all these files? I am new to unix and concerned about accidentally deleting other files in my directory.

I've looked at other questions but I'm unsure, could I do something like

rm -v !("differentfile"|"anotherfile"|"otherfile"|"finalfile") 

and therefore delete all my files with the same name and just different numbers and keep the few I want? And this line should only delete files in my working directory/folder correct?

Is there a way to use rm but only specific the files I want to delete all at once?

1
  • I know it's tough to read but have a look at man 7 regex
    – bey0nd
    Mar 12, 2020 at 19:29

3 Answers 3

5

rm file*.txt will remove all files in the current directory that has names that start with the string file and ends with the string .txt.

If there are many of these, you will get an "Argument list too long" error. In this case, you may use

find . -maxdepth 1 -name 'file*.txt' -delete

instead.

This will use the find command to find any file whose name matches file*.txt in the current directory, and delete these. The -maxdepth 1 option restricts find from entering any subdirectories other than . (the current directory).

The command does not make a difference between files and directories (just like rm file*.txt wouldn't do either). If you want to make sure to only delete regular files, use

find . -maxdepth 1 -type f -name 'file*.txt' -delete

For find implementations that does not have the non-standard -delete and/or -maxdepth options, use

find . ! -path . -prune -type f -name 'file*.txt' -exec rm -f {} +

Or, you could just resort to a simple but slow loop:

for name in file*.txt; do
    rm -f "$name"
done
1
  • The find command worked, the files were too many, and thank you for the explanation too, both saved me from a panicked night and taught me something
    – DN1
    Mar 12, 2020 at 20:03
2

If there are no other files of the same structure, use so called globbing with prefix 'file'. An asterisk means any other string (incl. empty string). Check first with 'ls' (list) instead of 'rm':

ls file*.txt

You could also narrow this down to at least a single number after 'file' and anything else after:

ls file[[:digit:]]*.txt
3
  • Thank you for this, so rm file*.txt will remove all files of those similar names?
    – DN1
    Mar 12, 2020 at 19:36
  • I tried this and it came back with -bash: /bin/rm: Argument list too long is there another way to do this in sections like with your number example do I just put file[[:1:]]*.txt and I can get all the 1s?
    – DN1
    Mar 12, 2020 at 19:40
  • Simply omit the asterisk and control the number of [[:digit:]]-placeholders, e.g. file[[:digit:]][[:digit:]].txt (no asterisk) deleting file00.txt until file99.txt. You have a bunch of files there, I guess more than 1000. Depends on your flavor of *nix how many parameters are allowed: globbing turns the pattern into the list of matching parameters. @Kusalananda: thx for fixing the brackets.
    – motzmann
    Mar 12, 2020 at 19:50
1

If you are not sure if your pattern will include other files, you could for example:

  • move them away to another (temporary) folder with mv PATTERN check the list of files and if everything is ok, then you remove the temporary folder.

  • use rm -i PATTERN. You will have to confirm each removal manually.

  • before doing rm you could do an ls PATTERN or echo PATTERN and check if the list is ok.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .