关于目录压缩脚本的快速问题
嘿, 我需要将某个目录中的所有目录打包。我有两个问题,我不断获得隐藏目录,这是我不想要的,并且一旦它们被焦油覆盖,我需要将它们放置在其他地方。
这是我现在正在做的事情:
for file in $(find /data/shares/builds/place1/ -maxdepth 1 -type d \( ! -regex '.*' \) );do tar zcf ../Place2_$1_$timeStamp/$file.tar.gz $file;done
我仍然收到隐藏文件,并且收到第一个位置不存在的错误。我收到消息:
../Place2_1.0.1_1012031412//data/shares/builds/Place1/Project1.tar.gz:无法打开:没有这样的文件或目录
有人看到我可以做些什么来解决此问题吗?
Hey,
I need to tar all the directories in a certain directory. I have two issues, I keep getting the hidden directories, which I don't want, and I need them to be placed somewhere else once they are tarred.
Here is what I am doing now:
for file in $(find /data/shares/builds/place1/ -maxdepth 1 -type d \( ! -regex '.*' \) );do tar zcf ../Place2_$1_$timeStamp/$file.tar.gz $file;done
I am still getting the hidden files, and I am getting an error that the first location does not exist. I am getting the message:
../Place2_1.0.1_1012031412//data/shares/builds/Place1/Project1.tar.gz: Cannot open: No such file or directory
Anyone see anything I can do to fix this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的 find 命令不正确(尝试自行测试)。
此 find 命令应该为您提供所需的目录列表:(
您需要 -mindepth 1 以避免列出包含的目录本身,并且 ! -name 按您的预期工作,而您的正则表达式方法则不然)。
其次,请注意 find 的输出包括每个目录的完整路径名(例如“/data/shares/builds/place1/Project1”,因此整个字符串被分配给您的 $file 变量,从而结束向上您尝试创建的新 tar 文件的目标“文件名”,这可能不是您真正想要的 - 我希望您尝试创建新的 tarball,例如“../Place2_1.0.1_1012031412/” Project1.tar.gz"。假设是这样,请使用
basename
从路径名中删除目录。如下所示:最后,请注意,您需要手动创建 Place2_$1_$timestamp 目录,提前。
First, your find command isn't proper (try testing it by itself).
This find command should give you the list of directories you want:
(you need -mindepth 1 to avoid listing the containing directory itself, and the ! -name works as you intend whereas your regex method doesn't).
Second, notice that the output of find includes the full pathnames of each directory (e.g. "/data/shares/builds/place1/Project1" so that whole string gets assigned to your $file variable and thereby ends up the target "filename" of the new tar file you're trying to create. That's probably not what you actually want -- I expect that you're trying to create the new tarballs as e.g. "../Place2_1.0.1_1012031412/Project1.tar.gz". Assuming so, use
basename
to strip off the directories from the pathname. Something like this:Finally, note that you will need to have created your Place2_$1_$timestamp directory manually, in advance.