1

I have *.xls files in the location /home/Docs/Calc. There are multiple subdirectories inside that folder. Eg

/home/Docs/Calc/2011
/home/Docs/Calc/2012
/home/Docs/Calc/2013

I can gzip each file under the subdirectories using the find command,

find /home/Docs/Calc -iname "*.xls" -exec gzip {} \;

but how can I gzip all the files in each subdirectory ? eg.

/home/Docs/Calc/2011/2011.tar.gz
/home/Docs/Calc/2012/2012.tar.gz
/home/Docs/Calc/2013/2013.tar.gz

I must add that /home/Docs/Calc is one of the many folders eg Calc-work, calc-tax, calc-bills. All of these have the year subfolders in them

Ubuntuser
  • 375
  • 2
  • 6
  • 14

5 Answers5

5

Since we don't have to recurse, I'd approach it not with find but with globbing and a for loop. If we're in the Calc directory, echo * will give us all the directory names:

~/Docs/Calc$ echo *
2011 2012 2013

It just so happens that we can use a for loop to iterate over these and tar them up in the usual way:

for year in *; do
    tar czf $year.tar.gz $year
done

If you want the resulting tarballs in the year directories, you could add an mv after the tar command. I'd be hesitant to put the tarball in the directory from outset or tar might start trying to tar its own output into itself.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • Thanks @icktoofay. Apologies for not mentioning that I am looking for searching across multiple folders. I have multiple folders under Doc, one of them is calc. eg. ~/Docs/Calc,~/Docs/Calc-work/.. and so on. These have .xls files sorted in year format. This is why I was trying to use the find command – Ubuntuser Sep 06 '13 at 04:26
  • 1
    @icktoofay I don't understand why so many votes, because this doesn't work at all... – Radu Rădeanu Sep 06 '13 at 04:49
  • @RaduRădeanu Please elaborate. This does what it says it does (modulo the bug that directory names with whitespace or wildcards in them will not work; [proper quoting](http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable) would fix that). – tripleee Dec 07 '15 at 05:23
  • It would be more robust to restrict to just directories, with `for year in */` (notice the slash). – tripleee Dec 07 '15 at 05:27
3

I set up a simple function in my .bashrc:

function gzdp () {
find . -type f -name "$@" -exec gzip {} \;
}

The $@ automatically gets replaced with whatever comes after gzdp when you call the function. Now, in your command window you can navigate to the /home/Docs/Calc/ folder and just call:

gzdp *.txt

and it should zip all .txt files in all lower subdirectories.

Not sure if this helps or not, my first post on this website. Careful that you don't accidentally gzip unwanted .txt files.

Kevin
  • 39
  • 1
  • No; if the wildcard matches a single file in the current directory, it will find only files with precisely that name. If multiple files match, it will cause a syntax error from the `find` command. `gzdp '*.txt'` should do what you say, though. – tripleee Dec 07 '15 at 05:20
2

Try this:

find /home/Docs/Calc -type d -exec tar cvzf {}.tar.gz {} \;
Lei Mou
  • 2,562
  • 1
  • 21
  • 29
1

Try this script as well:

#!/bin/bash
find /home/Docs/Calc/ -mindepth 1 -type d | while read -r DIR; do
    NAME=${DIR##*/}
    pushd "$DIR" >/dev/null && {
        tar -cvpzf "${NAME}.tar.gz" *
        popd >/dev/null
    }
done
konsolebox
  • 72,135
  • 12
  • 99
  • 105
1

You can use the following shell script:

#!/bin/bash

cd /home/Docs/Calc/
find=`find . -type d`
for f in $find; do
    cd $f   
    tar -cvz *.xls >> ${f##*/}.tar.gz
    cd -
done
Radu Rădeanu
  • 2,642
  • 2
  • 26
  • 43