6

I’m generally use a Debian based Linux distribution of some sort; either Debian 7.8 or Linux Mint 17. I have a split RAR file like this:

filename.rar
filename.r01
filename.r02
filename.r03
etc ...

Having extracted the archive using dtrx or unrar, I’d like to delete all the part files of the split archive using, preferably a single line.

At the moment I do rm filename.r*, which in general works well, however if the files are name like rabbid.ranger.robot.r01 it can delete the extracted directory rabbid.ranger.robot as well.

So I’m looking for something a bit more robust.

Will accept a short script as well.

6

Depending on the number of .r0* files you have you could replace the * with a ? this will remove files that have .r0 and 1 other character. So it will remove rabbid.ranger.robot.r01 but not rabbid.ranger.robot.r010 Generally I would use it as such:

rm filename.r??
10

Suppose you have a split RAR with parts numbered up to 12 (i.e., filename.r12 as your last file) and you want to be extra careful not to remove any other files. Assuming bash is your shell and you're using version 4 or higher (bash --version to check), you can create an explicit list of files easily with brace expansion:

rm filename.rar filename.r{01..12}

This will remove only those files: even files named filename.rtf (or filename.r13) will be spared. Depending on what other files you have (or might have) in the folder, this may or may not be preferable to the usually quite good .r?? way suggested by Devan.

Brace expansion, including numeric ranges, is a very old feature of several shells. But before version 4, bash did not support zero-padding (i.e., you want names like filename.r05, not filename.r5).

You can check first to see what filenames a brace expression will expand to:

$ echo filename.rar filename.r{01..12}
filename.rar filename.r01 filename.r02 filename.r03 filename.r04 filename.r05 filename.r06 filename.r07 filename.r08 filename.r09 filename.r10 filename.r11 filename.r12

(You can check expressions with * or ? this way, too--just remember that what it expands to depends on what folder you're in when you run the command.)

  • 1
    Two very good answers, hard to choose between them. I'll certainly be using this feature of bash as well. I felt Devan's answer had the edge, although not to belittle this answer. – fswings Apr 6 '15 at 20:15
4

You can use the find command; from the working directory:

find . -type f -name "filename.*" -exec rm  {} \;

This searches only for files that match, directories are excluded. It may be a good idea to first test the command with ls rather than rm to make sure it’s returning the results you desire.

find . -type f -name "filename.*" -exec ls {} \;
  • 2
    But rm (without -r) also excludes directories. This find way has no advantage over rm filename.r*, which fswings is trying to improve upon. rm filename.r* deletes as much as the OP wants to delete (and possibly more); this method with find deletes at least as much as rm filename.r*, and sometimes more since find is recursive (i.e., it descends into subdirectories and deletes files there, and in their subdirectories, etc.). You could confine it to operating on the contents of . by adding -maxdepth 1, but it still wouldn't have any advantages over rm filename.r*. – user105707 Apr 6 '15 at 3:10

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.