bash 在子目录中查找和 gzip 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18649393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:28:58  来源:igfitidea点击:

Find and gzip files in subdirectories

bash

提问by Ubuntuser

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

我在 /home/Docs/Calc 位置有 *.xls 文件。该文件夹中有多个子目录。例如

/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 命令对子目录下的每个文件进行 gzip,

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

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

但是我怎样才能 gzip 每个子目录中的所有文件?例如。

/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

我必须补充一点,/home/Docs/Calc 是许多文件夹之一,例如 Calc-work、calc-tax、calc-bills。所有这些都有年份子文件夹

回答by icktoofay

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

由于我们不必递归,我不会使用find而是使用通配符和for循环来处理它。如果我们在Calc目录中,echo *会给我们所有的目录名称:

~/Docs/Calc$ echo *
2011 2012 2013

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

碰巧我们可以使用for循环来迭代这些并以通常的方式将它们压缩:

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 mvafter the tarcommand. 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.

如果您希望在年份目录中生成 tarball,您可以mvtar命令后添加一个。我会犹豫是否从一开始就将 tarball 放在目录中,否则 tar 可能会开始尝试将自己的输出 tar 到自身中。

回答by Lei Mou

Try this:

尝试这个:

find /home/Docs/Calc -type d -exec tar cvzf {}.tar.gz {} \;

回答by konsolebox

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

回答by Radu R?deanu

You can use the following shell script:

您可以使用以下 shell 脚本:

#!/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

回答by Kevin

I set up a simple function in my .bashrc:

我在我的 .bashrc 中设置了一个简单的函数:

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

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

当您调用该函数时,它会$@自动替换为之后的任何内容gzdp。现在,在您的命令窗口中,您可以导航到该/home/Docs/Calc/文件夹并调用:

gzdp *.txt

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

它应该压缩所有较低子目录中的所有 .txt 文件。

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

不知道这是否有帮助,我在这个网站上的第一篇文章。小心不要意外损坏gzip.txt 文件。