2

I have a directory with lots of sub directories inside of it and I want to find a way to turn all of those sub directories into their own .rar files using the terminal, since there are too many to do individually. I need each sub directory to be it's own .rar file as opposed to just raring the main directory into one big .rar file, because the sub directories have to go to different places once their compressed. Thanks.

2

If you really need .rar extension then you need to install rar.

sudo apt install rar

The following commands will compress all the directories in current working directory (Use command pwd to know your current directory):

for dir in */
do
  base=$(basename "$dir")
  rar a "${base}" "$dir"
done

For normal tar balls you can use:

cd directory
for dir in */
do
  base=$(basename "$dir")
  tar -czf "${base}.tar.gz" "$dir"
done
  • That works perfectly except for the fact that some of my folders have spaces in their names, and it is skipping over those, I assume I need to add quotations somewhere in there but where? – LionParty Apr 16 '17 at 4:00
  • Oh right! I should've put the quotations in the same place as the second one so it goes like this: rar a "${base}" "$dir" – Khalid Abu Shawarib Apr 16 '17 at 12:23
  • I'm using the first one you recommended and it's recognizing the folders with spaces in their names, but it says "WARNING: No files" when attempting to rar any folder with a space in the name, the error however does not appear when raring folders without spaces in their names. – LionParty Apr 16 '17 at 19:59
  • "WARNING: No files" means that the folder is empty and It won't be compressed. It shouldn't have anything to do with spaces. – Khalid Abu Shawarib Apr 16 '17 at 21:22
  • The problem is that the folder is definitely not empty, I know there are other folders and files inside the folders that I'm getting the "WARNING: No files" error in. It seems that the directories that I'm getting this error on do not have files inside of them, but rather only folders in them which contain files. – LionParty Apr 17 '17 at 0:12

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.