How to remove all the parts of a split RAR archive file using command line?

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.

fswings

Posted 2015-04-05T23:53:45.420

Reputation: 666

Answers

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??

Devan

Posted 2015-04-05T23:53:45.420

Reputation: 186

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.)

user105707

Posted 2015-04-05T23:53:45.420

Reputation:

1Two 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 – 2015-04-06T20:15:38.680

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 {} \;

nod

Posted 2015-04-05T23:53:45.420

Reputation: 478

2But 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*. – None – 2015-04-06T03:10:16.437