Quickly archive multiple directories into separate archive files

Posted by on Aug 24, 2009 in Ubuntu23 comments

I recently needed to quickly archive several hundred directories, and their contents into separate, compressed files named after the directories. The horribly slow way to do this would be to either right-click each directory and select “create archive” via the File Manager, or, via the command line, to manually run tar several hundred times – once for each directory. Neither option is pleasant, or viable. However, with a simple shell for loop, combined with ls and the relevant archiver command, the task becomes quick and painless:

for dir in `ls`;do cd $dir; tar -cvzf $dir.tar.gz *;mv $dir.tar.gz ..; cd ..;done

This will run an ls command, and temporarily save the output for use in the For loop. The code loops through each part of the ls output, moves into the current directory (assuming it is a directory), creates the archive and moves the archive file to the parent directory, before moving itself to the parent directory to begin again with the next part of the ls output.

So, for example, if you have the following directory structure:

/home
--username
----stuff_to_archive
------dir-to-archive1
------dir-to-archive2
------dir-to-archive3
------dir-to-archive4
------dir-to-archive5

and you wish to archive dir-to-archive1 through dir-to-archive5, and all the contents therein, you would run:

cd /home/username/stuff_to_archive
for dir in `ls`;do cd $dir; tar -cvzf $dir.tar.gz *;mv $dir.tar.gz ..; cd ..;done

The code will loop through each directory and archive its contents into a file named after the directory (e.g. dir-to-archive1.tar.gz), leaving you with:

/home
--username
----stuff_to_archive
------dir-to-archive1
------dir-to-archive1.tar.gz
------dir-to-archive2
------dir-to-archive2.tar.gz
------dir-to-archive3
------dir-to-archive3.tar.gz
------dir-to-archive4
------dir-to-archive4.tar.gz
------dir-to-archive5
------dir-to-archive5.tar.gz

If you want to use ZIP instead, then run:

for dir in `ls`;do cd $dir; zip $dir *;mv $dir.zip ..; cd ..;done

or replace the archiver command with whatever you want to use to compress the directory contents.

23 comments

» Comments RSS Feed
  1. Hi buddy.

    By chance I’ve had some similar needs lately. I wanted to create an archive of the deepest directory inside a structure with different depths (my music folder actually)
    But first, you shouldn’t parse ls output.
    find $PWD -type d
    would be much better. Also, there’s no need to cd in a command or a script.
    However, here’s what I did
    This nice piece of bit finds the deepest subdirs in a given stack, returning the full path for each one:

    find $PWD -type d -exec bash -c ‘shopt -s nullglob; d=(“$1″/*/); ((!${#d[@]}))’ _ {} \; -print > /tmp/dirlist

    and then run a nice smooth and safe

    for i in $(sed ‘1,$!d’ /tmp/dirlist); do tar cvfj “${i##*/}”.tar.bz2 “$i” ; done

    :)

    I just rolled a blog too, join me if you feel like :)

    blog.loomsen.org

  2. man, i googled for a script, ran across your code.

    complete time saver man, props to you!

    thanks so much.

  3. [...] Quickly archive multiple directories into separate archive files | 64 Bit Jungle – [...]

  4. ?????????)

  5. Hurrah, that’s what I was searching for, what a information! existing here at this webpage,
    thanks admin of this site.

  6. Great post. I used to be checking continuously this blog and I am impressed!
    Extremely helpful info particularly the closing section :) I care for such info much.
    I was looking for this particular info for a long
    time. Thanks and good luck.

  7. What i don’t understood is if truth be told how you’re no longer actually a lot
    more neatly-liked than you might be right now.
    You’re very intelligent. You already know therefore significantly relating to this
    subject, produced me in my opinion believe it from
    a lot of various angles. Its like men and women don’t seem to be fascinated unless it’s one thing to do with Girl
    gaga! Your individual stuffs great. All the time maintain it up!

  8. Hi there, after reading this awesome paragraph i am as well delighted to share my knowledge here with mates.

  9. When someone writes an paragraph he/she retains the plan of a user in his/her mind
    that how a user can understand it. Therefore that’s why this post is great.

    Thanks!

  10. Thanks a bunch for sharing this with all people you actually understand what you are talking about!
    Bookmarked. Kindly also seek advice from my website =).
    We could have a hyperlink trade arrangement among us

  11. I know this website offers quality based articles and extra data, is there any other website which offers these kinds
    of data in quality?

  12. hello there and thank you for your information – I have certainly picked up anything new
    from right here. I did however expertise several technical points using this website,
    since I experienced to reload the web site a lot of times previous
    to I could get it to load correctly. I had been wondering if your
    web host is OK? Not that I’m complaining, but slow loading
    instances times will often affect your placement in google
    and could damage your high-quality score if advertising and marketing with Adwords.
    Well I am adding this RSS to my e-mail and can look out
    for much more of your respective exciting content.
    Ensure that you update this again soon.

  13. great issues altogether, you just gained a brand new reader.
    What could you recommend about your publish that you just made some
    days in the past? Any certain?

  14. Hmm it looks like your website ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly
    enjoying your blog. I too am an aspiring blog blogger
    but I’m still new to the whole thing. Do you have any tips
    for inexperienced blog writers? I’d really appreciate it.

  15. Wonderful goods from you, man. I’ve understand your stuff previous to and you are just extremely fantastic.
    I really like what you’ve acquired here, certainly like
    what you are saying and the way in which you say it. You make it enjoyable and you still care for to keep
    it wise. I can’t wait to read far more from you.
    This is actually a great website.

  16. What’s Happening i am new to this, I stumbled upon this I
    have discovered It positively useful and it has aided me out
    loads. I’m hoping to give a contribution & assist other
    customers like its helped me. Great job.

  17. This is my first time go to see at here and i am in fact pleassant to read all at
    single place.

  18. I used to be suggested this blog by way of my cousin. I am no
    longer positive whether this submit is written via him as no one else recognise such particular
    about my problem. You are wonderful! Thanks!

  19. You really make it seem so easy with your presentation but I find this
    topic to be really something which I think I would never understand.
    It seems too complicated and extremely broad
    for me. I am looking forward for your next post, I’ll try to get the hang of it!

  20. If you are going for best contents like myself, only
    pay a quick visit this web page every day because it gives quality contents,
    thanks

  21. Thankfulness to my father who informed me about this blog,
    this weblog is in fact remarkable.

  22. Nice post. I learn something new and challenging on websites I stumbleupon every day.
    It will always be interesting to read content from
    other writers and practice a little something from other web sites.

  23. There’s definately a great deal to know about this subject.
    I like all of the points you made.

Leave a comment