关于目录压缩脚本的快速问题

发布于 2024-10-05 18:22:08 字数 439 浏览 5 评论 0原文

嘿, 我需要将某个目录中的所有目录打包。我有两个问题,我不断获得隐藏目录,这是我不想要的,并且一旦它们被焦油覆盖,我需要将它们放置在其他地方。

这是我现在正在做的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界和平 2024-10-12 18:22:08

首先,您的 find 命令不正确(尝试自行测试)。

此 find 命令应该为您提供所需的目录列表:(

find /data/shares/builds/place1/ -mindepth 1 -maxdepth 1 -type d ! -name '.*'

您需要 -mindepth 1 以避免列出包含的目录本身,并且 ! -name 按您的预期工作,而您的正则表达式方法则不然)。

其次,请注意 find 的输出包括每个目录的完整路径名(例如“/data/shares/builds/place1/Project1”,因此整个字符串被分配给您的 $file 变量,从而结束向上您尝试创建的新 tar 文件的目标“文件名”,这可能不是您真正想要的 - 我希望您尝试创建新的 tarball,例如“../Place2_1.0.1_1012031412/” Project1.tar.gz"。假设是这样,请使用 basename 从路径名中删除目录。如下所示:

for file in $(
    find /data/shares/builds/place1/ -mindepth 1 -maxdepth 1 -type d ! -name '.*'
  ); do tar zcf  ../Place2_$1_$timeStamp/$(basename $file).tar.gz $file;done

最后,请注意,您需要手动创建 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:

find /data/shares/builds/place1/ -mindepth 1 -maxdepth 1 -type d ! -name '.*'

(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:

for file in $(
    find /data/shares/builds/place1/ -mindepth 1 -maxdepth 1 -type d ! -name '.*'
  ); do tar zcf  ../Place2_$1_$timeStamp/$(basename $file).tar.gz $file;done

Finally, note that you will need to have created your Place2_$1_$timestamp directory manually, in advance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文