Unrar to a certain folder and then delete source folder

The following command works for unrar to extract an archive to a certain folder.

unrar e -r -o- /home/username/source/directory/*.rar /home/username/copy/extracted/to

Inside /home/username/source/directory there are some subdirectories with .rar archives and some other files (.r01, .r02, …). I want to extract the .rar files to /home/username/copy/extracted/to and delete the /home/username/source/directory folder or all the files in that one folder.

In the above command, where should I put the delete command when the extracting is done?

Asked By: Disgrace

||

Update

After OP’s clarification that the .rar files are inside subdirectories, I think that this is a better approach.

Use find to find all .rar files in a directory and its subdirectories. Use find‘s -exec flag to execute the unrar command to unrar the files and add && rm -r /home/username/source/directory at the end to delete the /home/username/source/directory folder and its contents:

find /home/username/source/directory/ -type f -name "*.rar" -exec unrar e -o- {} /home/username/copy/extracted/to ; && rm -r /home/username/source/directory

Old answer

You can do the following:

for file in /home/username/source/directory/*.rar; do unrar e -r -o- "$file" /home/username/copy/extracted/to && rm -- "$file"; done
Answered By: BeastOfCaerbannog
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.