LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 04-26-2009, 06:09 PM   #1
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Rep: Reputation: 41
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
 
Old 04-26-2009, 06:24 PM   #2
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
What are you suggesting is wrong with your proposed method?

I would suggest a more "traditional" file extension though - probably filename.tar.gz
 
Old 04-26-2009, 06:30 PM   #3
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by billymayday View Post
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.
 
Old 04-26-2009, 06:32 PM   #4
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Original Poster
Rep: Reputation: 41
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
 
Old 04-26-2009, 06:44 PM   #5
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
If you want them to be able to unzip them, you won't want to use tar then.
 
Old 04-26-2009, 06:48 PM   #6
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by billymayday View Post
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?
 
Old 04-26-2009, 06:58 PM   #7
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Add the files to the zip archive without using tar
 
Old 04-26-2009, 07:13 PM   #8
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by billymayday View Post
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
 
Old 04-26-2009, 07:33 PM   #9
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
That is telling you that pkzip is compatible with windows zip, not that tar is.

Glad that version works.

BM
 
Old 04-26-2009, 11:41 PM   #10
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Original Poster
Rep: Reputation: 41
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
BackUp & Restore with TAR (.tar / .tar.gz / .tar.bz2 / tar.Z) asgarcymed Linux - General 5 12-31-2006 02:53 AM
building gcc components individually chrisortiz Linux - Software 1 12-23-2006 10:17 PM
installing software individually on FC3 ninadb Fedora 4 02-07-2005 04:45 PM
tar --exclude for subdirectories mrtwice Linux - Software 2 10-23-2003 12:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:42 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