LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2008, 12:27 PM   #1
infonlinebr
LQ Newbie
 
Registered: May 2008
Posts: 20

Rep: Reputation: 0
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
 
Old 06-30-2008, 01:05 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 06-30-2008, 01:11 PM   #3
infonlinebr
LQ Newbie
 
Registered: May 2008
Posts: 20

Original Poster
Rep: Reputation: 0
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
 
Old 06-30-2008, 01:25 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 06-30-2008, 01:34 PM   #5
infonlinebr
LQ Newbie
 
Registered: May 2008
Posts: 20

Original Poster
Rep: Reputation: 0
Perfect, worked certinho, thank you very much.
Whenever I have some doubts in these exchanges of office files to folders.
 
Old 03-17-2016, 02:08 PM   #6
francoros
LQ Newbie
 
Registered: Mar 2016
Posts: 2

Rep: Reputation: Disabled
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

Last edited by francoros; 03-17-2016 at 02:12 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
question on how to rar multiple files into there own rar file steve51184 Linux - Software 13 10-24-2007 04:23 PM
how do i add a folder into a rar archive steve51184 Linux - Software 6 12-06-2006 04:27 PM
Compress folder to Zip file makan007 Red Hat 1 09-05-2006 04:27 AM
to compress a file or folder sanjay2004 Linux - Newbie 1 04-11-2006 08:28 AM
How to compress a folder deWin Linux - Newbie 3 11-11-2004 10:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration