scientificlinuxforum.org QR code
Scientific Linux Forum.org



  Reply to this topicStart new topicStart Poll

> HOWTO make a script to compress directories, Not yet solved
pieniValkoinenHattu
 Posted: Apr 25 2012, 03:13 PM
Quote Post


SLF Junior
**

Group: Members
Posts: 25
Member No.: 1452
Joined: 10-April 12









Hello,

I would like to know how to make a script to compress all the directories contained in a parent directory.
To be clear I would like a way to do something like that:

for directory from firstDirectory to lastDirectory do
zip directory //the name of the zip should be the same as the directory
end

Thank you.
PM
^
log69
 Posted: Apr 25 2012, 03:47 PM
Quote Post


SLF Member
***

Group: Members
Posts: 98
Member No.: 1325
Joined: 24-February 12









Not exactly entirely clear what you want.

To compress a dir recursively:

CODE
zip -r output.zip mydir


To compress all dirs separately in a parent dir:

CODE
ls PARENTDIR | while read MYDIR; do zip -r MYDIR.zip MYDIR; done


or

CODE
find PARENTDIR -type d | while read MYDIR; do zip -r MYDIR.zip MYDIR; done


My script looks like this (lbzip2 uses all CPU cores):

CODE
#!/bin/bash
# COMPRESS FILE OR DIRECTORY

FILE="$1"
FNAME=`basename "$FILE"`

if [ -z "$FILE" ]; then echo "usage: command [dir|file]"; exit 1; fi


if [ -d "$FILE" ];
then

       SIZE=`du -sb "$FILE" | cut -f1`
       tar -cf - "$FILE" 2>/dev/null | pv -s "$SIZE" | lbzip2 > "$FNAME".tar.bz2

else

       pv "$FILE" | lbzip2 > "$FNAME".bz2

fi
PM
^
AndrewSerk
 Posted: Apr 25 2012, 03:56 PM
Quote Post


SLF Moderator
******

Group: Moderators
Posts: 528
Member No.: 54
Joined: 14-April 11









Moved from "How-to's / manuals / instructional manuals" to "Software and applications" as "How-to's / manuals / instructional manuals" if not for questions.

This post has been edited by AndrewSerk: Apr 25 2012, 03:57 PM
PM
^
pieniValkoinenHattu
 Posted: Apr 25 2012, 04:05 PM
Quote Post


SLF Junior
**

Group: Members
Posts: 25
Member No.: 1452
Joined: 10-April 12









Thank you very much for answering however I don't know how to use what you wrote.

To be more precise I would like some kind of loop that compress each directory contained in a directory for exemple:

CODE
[isen@ISENMIC-EU0I8NA Family]$ ls
Brother  Father  Me  Mother  Sister


I am in the folder Family and I want to have one zip file for each directory it contains ( Father.zip, Mother.zip etc...)
PM
^
log69
 Posted: Apr 26 2012, 07:52 AM
Quote Post


SLF Member
***

Group: Members
Posts: 98
Member No.: 1325
Joined: 24-February 12









QUOTE (pieniValkoinenHattu @ Apr 25 2012, 05:05 PM)
Thank you very much for answering however I don't know how to use what you wrote.

To be more precise I would like some kind of loop that compress each directory contained in a directory for exemple:

CODE
[isen@ISENMIC-EU0I8NA Family]$ ls
Brother  Father  Me  Mother  Sister


I am in the folder Family and I want to have one zip file for each directory it contains ( Father.zip, Mother.zip etc...)


So you go with the command starting with the find command. Stand in your Family directory, and run it.
PM
^
pieniValkoinenHattu
 Posted: Apr 26 2012, 09:42 AM
Quote Post


SLF Junior
**

Group: Members
Posts: 25
Member No.: 1452
Joined: 10-April 12









Sorry for being such a noob but it does not work:

CODE
[isen@ISENMIC-EU0I8NA Family]$ find ../Family/* -type d | while read MYDIR; do zip -r MYDIR.zip MYDIR; done
zip warning: name not matched: MYDIR

zip error: Nothing to do! (try: zip -r MYDIR.zip . -i MYDIR)
zip warning: name not matched: MYDIR

zip error: Nothing to do! (try: zip -r MYDIR.zip . -i MYDIR)
zip warning: name not matched: MYDIR

zip error: Nothing to do! (try: zip -r MYDIR.zip . -i MYDIR)
zip warning: name not matched: MYDIR

zip error: Nothing to do! (try: zip -r MYDIR.zip . -i MYDIR)
zip warning: name not matched: MYDIR

zip error: Nothing to do! (try: zip -r MYDIR.zip . -i MYDIR)


I don't know what should I put instead of MYDIR, (I replaced the PARENTDIR by ../Family/* because other way it didn't work at all: not found directory )
PM
^
log69
 Posted: Apr 26 2012, 11:56 AM
Quote Post


SLF Member
***

Group: Members
Posts: 98
Member No.: 1325
Joined: 24-February 12









QUOTE (pieniValkoinenHattu @ Apr 26 2012, 10:42 AM)
Sorry for being such a noob but it does not work:

CODE
[isen@ISENMIC-EU0I8NA Family]$ find ../Family/* -type d |
zip warning: name not matched: MYDIR

zip error: Nothing to do! (try: zip -r MYDIR.zip . -i MYDIR)
zip warning: name not matched: MYDIR

zip error: Nothing to do! (try: zip -r MYDIR.zip . -i MYDIR)
zip warning: name not matched: MYDIR

zip error: Nothing to do! (try: zip -r MYDIR.zip . -i MYDIR)
zip warning: name not matched: MYDIR

zip error: Nothing to do! (try: zip -r MYDIR.zip . -i MYDIR)
zip warning: name not matched: MYDIR

zip error: Nothing to do! (try: zip -r MYDIR.zip . -i MYDIR)


I don't know what should I put instead of MYDIR, (I replaced the PARENTDIR by ../Family/* because other way it didn't work at all: not found directory )


No problem, I said it incorrectly. Stay inside your Family dir, and run it with "find . -type d ...." with an extra options like this:

CODE
find . -maxdepth 1 -type d | sort | tail -n+2 | while read MYDIR; do zip -r "$MYDIR".zip "$MYDIR"; done


I tested it and it seems to be working just fine.
PM
^
pieniValkoinenHattu
 Posted: Apr 26 2012, 02:33 PM
Quote Post


SLF Junior
**

Group: Members
Posts: 25
Member No.: 1452
Joined: 10-April 12









Thank you so much, this is awesome !

Could you explain me the line ?
I would like to be able to make script like this one on my own.

Thanks again !

PM
^
log69
 Posted: Apr 26 2012, 03:11 PM
Quote Post


SLF Member
***

Group: Members
Posts: 98
Member No.: 1325
Joined: 24-February 12









QUOTE (pieniValkoinenHattu @ Apr 26 2012, 03:33 PM)
Thank you so much, this is awesome !

Could you explain me the line ?
I would like to be able to make script like this one on my own.

Thanks again !


man find
man sort
man tail

besides, try the script in parts, like:

CODE
find . -maxdepth 1 -type d


CODE
find . -maxdepth 1 -type d | sort
seq 1 10
seq 1 10 | shuf
seq 1 10 | shuf | sort


CODE
seq 1 30 | tail
seq 1 10 | tail -n2
seq 5 | head -n 2


etc.
PM
^
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:

Topic Options Reply to this topicStart new topicStart Poll