LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   compress folder in rar (https://www.linuxquestions.org/questions/linux-newbie-8/compress-folder-in-rar-652612/)

infonlinebr 06-30-2008 12:27 PM

compress folder in rar
 
I wanted to compress each folder in a directory rar or zip, and the files would have to have the name of your folders

colucix 06-30-2008 01:05 PM

To create a zip archive the command is
Code:

zip -R directory.zip directory/*
To crete a rar archive - if you have rar installed
Code:

rar a -r directory.rar directory/*
The zip archive preserves the directory structure inside the archive. I don't know how to preserve it using rar archives.

infonlinebr 06-30-2008 01:11 PM

I wanted to do this is automated, because I have about 7000 folders

would be something like?
Code:

for folder; do rar a -r "$folder.rar" "$folder"; done

colucix 06-30-2008 01:25 PM

Yes, it should be. However, the syntax you used (without in) does a loop over the arguments of the script. To specify a list of directories you have to do something like this
Code:

for folder in */
do
  echo rar a -r "${folder%/}.rar" "$folder"
done

this loops over all the folders in the current directory. The parameter substitution ${folder%/} serves to strip out the "/" at the end of the folder name.

I put a leading echo to the rar command for testing purpose. Visualize the commands which will be executed and when you're satisfied with the results, strip out the echo.

Just a note regarding what I previously stated. Actually the rar utility preserves the directory structure inside the archive. I had only to extract by unrar x instead of unrar e.

infonlinebr 06-30-2008 01:34 PM

Perfect, worked certinho, thank you very much.
Whenever I have some doubts in these exchanges of office files to folders.

francoros 03-17-2016 02:08 PM

Attention to echo command, maybe don't work if it is present in CentosOS

Code:

for folder in */; do rar a -m0 -r "${folder%/}.rar" "$folder"; done
you can also change

Code:

${folder%/}.rar
into
Code:

${folder%/}.zip
-m0 is minimum compression so zipping is fastest

source: http://stackoverflow.com/questions/2...-centos-ubuntu


All times are GMT -5. The time now is 09:31 PM.