查找文件并将它们 tar(带空格) [英] Find files and tar them (with spaces)

查看:36
本文介绍了查找文件并将它们 tar(带空格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这里的问题很简单.我正在研究一个简单的备份代码.它工作正常,除非文件中有空格.这就是我查找文件并将它们添加到 tar 存档的方式:

Alright, so simple problem here. I'm working on a simple back up code. It works fine except if the files have spaces in them. This is how I'm finding files and adding them to a tar archive:

find . -type f | xargs tar -czvf backup.tar.gz 

问题是文件名中有空格,因为 tar 认为它是一个文件夹.基本上有没有一种方法可以在 find 的结果周围添加引号?或者用不同的方法来解决这个问题?

The problem is when the file has a space in the name because tar thinks that it's a folder. Basically is there a way I can add quotes around the results from find? Or a different way to fix this?

推荐答案

使用这个:

find . -type f -print0 | tar -czvf backup.tar.gz --null -T -

它会:

  • 处理带有空格、换行符、前导破折号和其他有趣内容的文件
  • 处理无限数量的文件
  • 不会像使用 tar -cxargs 那样重复覆盖您的 backup.tar.gz,这样当您有大量文件时会这样做
  • deal with files with spaces, newlines, leading dashes, and other funniness
  • handle an unlimited number of files
  • won't repeatedly overwrite your backup.tar.gz like using tar -c with xargs will do when you have a large number of files

另见:

  • GNU tar manual
  • How can I build a tar from stdin?, search for null

这篇关于查找文件并将它们 tar(带空格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆