如何在unix中压缩整个文件夹树,但只能压缩某些文件?

时间:2023-01-22 07:09:58

I've been stuck on a little unix command line problem.

我一直被困在一个小的unix命令行问题上。

I have a website folder (4gb) I need to grab a copy of, but just the .php, .html, .js and .css files (which is only a couple hundred kb).

我有一个网站文件夹(4gb)我需要获取一份副本,但只有.php,.html,.js和.css文件(只有几百kb)。

I'm thinking ideally, there is a way to zip or tar a whole folder but only grabbing certain file extensions, while retaining subfolder structures. Is this possible and if so, how?

理想情况下,有一种方法可以压缩或tar整个文件夹,但只能抓取某些文件扩展名,同时保留子文件夹结构。这是可能的,如果是的话,怎么样?

I did try doing a whole zip, then going through and excluding certain files but it seemed a bit excessive.

我确实尝试做一个完整的拉链,然后通过并排除某些文件,但它似乎有点过分。

I'm kinda new to unix.

我是unix的新手。

Any ideas would be greatly appreciated.

任何想法将不胜感激。

6 个解决方案

#1


Switch into the website folder, then run

切换到网站文件夹,然后运行

zip -R foo '*.php' '*.html' '*.js' '*.css' 

You can also run this from outside the website folder:

您也可以从网站文件夹外部运行:

zip -r foo website_folder -i '*.php' '*.html' '*.js' '*.css'

#2


You can use find and grep to generate the file list, then pipe that into zip

您可以使用find和grep生成文件列表,然后将其传递到zip

e.g.

find . | egrep "\.(html|css|js|php)$" | zip -@ test.zip

(-@ tells zip to read a file list from stdin)

( - @告诉zip从stdin读取文件列表)

#3


This is how I managed to do it, but I also like ghostdog74's version.

这就是我设法做到的,但我也喜欢ghostdog74的版本。

tar -czvf archive.tgz `find  test/ | egrep ".*\.html|.*\.php"`

You can add extra extensions by adding them to the regex.

您可以通过将其添加到正则表达式来添加额外的扩展。

#4


I liked Nick's answer, but, since this is a programming site, why not use Ant to do this. :)

我喜欢Nick的答案,但是,由于这是一个编程站点,为什么不使用Ant来做这个。 :)

Then you can put in a parameter so that different types of files can be zipped up.

然后,您可以输入一个参数,以便可以压缩不同类型的文件。

http://ant.apache.org/manual/Tasks/zip.html

#5


you may want to use find(GNU) to find all your php,html etc files.then tar them up

你可能想用find(GNU)找到你所有的php,html等文件。然后把它们搞定

find /path -type f \( -iname "*.php" -o -iname "*.css" -o -iname "*.js" -o -iname "*.ext" \) -exec tar -r --file=test.tar "{}" +;

after that you can zip it up

之后你可以拉上它

#6


You could write a shell script to copy files based on a pattern/expression into a new folder, zip the contents and then delete the folder. Now, as for the actual syntax of it, ill leave that to you :D.

您可以编写一个shell脚本,将基于模式/表达式的文件复制到新文件夹中,压缩内容然后删除该文件夹。现在,至于它的实际语法,生病留给你:D。

#1


Switch into the website folder, then run

切换到网站文件夹,然后运行

zip -R foo '*.php' '*.html' '*.js' '*.css' 

You can also run this from outside the website folder:

您也可以从网站文件夹外部运行:

zip -r foo website_folder -i '*.php' '*.html' '*.js' '*.css'

#2


You can use find and grep to generate the file list, then pipe that into zip

您可以使用find和grep生成文件列表,然后将其传递到zip

e.g.

find . | egrep "\.(html|css|js|php)$" | zip -@ test.zip

(-@ tells zip to read a file list from stdin)

( - @告诉zip从stdin读取文件列表)

#3


This is how I managed to do it, but I also like ghostdog74's version.

这就是我设法做到的,但我也喜欢ghostdog74的版本。

tar -czvf archive.tgz `find  test/ | egrep ".*\.html|.*\.php"`

You can add extra extensions by adding them to the regex.

您可以通过将其添加到正则表达式来添加额外的扩展。

#4


I liked Nick's answer, but, since this is a programming site, why not use Ant to do this. :)

我喜欢Nick的答案,但是,由于这是一个编程站点,为什么不使用Ant来做这个。 :)

Then you can put in a parameter so that different types of files can be zipped up.

然后,您可以输入一个参数,以便可以压缩不同类型的文件。

http://ant.apache.org/manual/Tasks/zip.html

#5


you may want to use find(GNU) to find all your php,html etc files.then tar them up

你可能想用find(GNU)找到你所有的php,html等文件。然后把它们搞定

find /path -type f \( -iname "*.php" -o -iname "*.css" -o -iname "*.js" -o -iname "*.ext" \) -exec tar -r --file=test.tar "{}" +;

after that you can zip it up

之后你可以拉上它

#6


You could write a shell script to copy files based on a pattern/expression into a new folder, zip the contents and then delete the folder. Now, as for the actual syntax of it, ill leave that to you :D.

您可以编写一个shell脚本,将基于模式/表达式的文件复制到新文件夹中,压缩内容然后删除该文件夹。现在,至于它的实际语法,生病留给你:D。