Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-26-2009, 06:09 PM
|
#1
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Rep:
|
How to tar all subdirectories individually?
Is there a better way to do it than this? (I have not tested this script yet, so it is more like pseudo code...)
Code:
#!/bin/sh
#tar & compress all subdirectories individually
for file in $(ls -1); do
if [ -d file ]; then
tar --delete-files -czf ${file}.zip $file
fi
done
exit 0
|
|
|
04-26-2009, 06:24 PM
|
#2
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep: 
|
What are you suggesting is wrong with your proposed method?
I would suggest a more "traditional" file extension though - probably filename.tar.gz
|
|
|
04-26-2009, 06:30 PM
|
#3
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Original Poster
Rep:
|
Quote:
Originally Posted by billymayday
What are you suggesting is wrong with your proposed method?
I would suggest a more "traditional" file extension though - probably filename.tar.gz
|
I didn't know if anything was wrong with this method. But usually when i think a loop is the right way to do it, someone on here gives me a 1-liner that is much better.
I just tested my code and this works:
Code:
for file in $(ls -1); do if [ -d $file ]; then tar --remove-files -czvvf ${file}.zip ${file}/; fi; done
But if there is a better way, please let me know.
|
|
|
04-26-2009, 06:32 PM
|
#4
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Original Poster
Rep:
|
i'm not sure if the windows users in my family will know what to do when they see a .tar.gz extension...
so I thought I would try to make things easy for them with .zip
|
|
|
04-26-2009, 06:44 PM
|
#5
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep: 
|
If you want them to be able to unzip them, you won't want to use tar then.
|
|
|
04-26-2009, 06:48 PM
|
#6
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Original Poster
Rep:
|
Quote:
Originally Posted by billymayday
If you want them to be able to unzip them, you won't want to use tar then.
|
thanks for the heads up. what should i use so a Windows user can unzip an archive like this?
|
|
|
04-26-2009, 06:58 PM
|
#7
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep: 
|
Add the files to the zip archive without using tar
|
|
|
04-26-2009, 07:13 PM
|
#8
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Original Poster
Rep:
|
Quote:
Originally Posted by billymayday
Add the files to the zip archive without using tar
|
I know no more than this:
Quote:
zip is a compression and file packaging utility ... it is analogous to a combination of the UNIX commands tar and compress and is compatible with PKZIP (Phil Katz’s ZIP for MSDOS systems).
|
That makes me think the prior tar -cjf command would do the same thing.
BTW, this works:
Code:
for file in $(ls -1); do if [ -d $file ]; then zip -rm ${file}.zip ${file}/; fi; done
|
|
|
04-26-2009, 07:33 PM
|
#9
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep: 
|
That is telling you that pkzip is compatible with windows zip, not that tar is.
Glad that version works.
BM
|
|
|
04-26-2009, 11:41 PM
|
#10
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Original Poster
Rep:
|
Code:
#!/bin/sh
# zip all subdirectories individually
if [ $# -ne 1 ]; then
echo 1>&2 Usage: $0 path/exclusion.lst
exit 1
fi
echo "exclusion list contains" `cat $1`
for file in $(ls -1); do
echo checking "$file"
if [ -d "$file" ]; then
echo "$file is a directory"
exclCheck=`grep "$file" $1`
if [ -z $exclCheck ]; then
echo "zipping $file"
zip -rm "${file}.zip" "${file}/"
else
echo "Exclusion list contains ${exclCheck}."
echo "skipping $file"
fi;
fi;
done
exit 0
Code:
cat exclude.lst
folderIDoNotWantZipped
anotherSpecialFolder
doNotZipMe
AnyFolder
|
|
|
All times are GMT -5. The time now is 07:56 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|