tar: 无法将目录更改为 <dir>/<file>: 不是目录

tar: can't change directories to <dir>/<file>: not a directory

我需要 tar 这个目录 - 到tar:

$ ls -l totar/
total 61388
 292 -rw-r--r--    1 wasext   was          298867 Apr 13 16:44 application.2015-01-19.0.log
 108 -rw-r--r--    1 wasext   was          109623 Apr 14 13:32 application.2015-01-20.0.log
 788 -rw-r--r--    1 wasext   was          805468 Apr 14 13:32 application.2015-01-21.0.log
1080 -rw-r--r--    1 wasext   was         1104861 Apr 14 13:32 application.2015-01-22.0.log
2052 -rw-r--r--    1 wasext   was         2098782 Apr 14 13:32 application.2015-01-23.0.log
   4 -rw-r--r--    1 wasext   was             455 Apr 14 13:32 application.2015-01-24.0.log
21076 -rw-r--r--    1 wasext   was        21581073 Apr 14 13:32 application.2015-01-26.0.log
9296 -rw-r--r--    1 wasext   was         9519026 Apr 14 13:32 application.2015-01-27.0.log
17912 -rw-r--r--    1 wasext   was        18341302 Apr 14 13:32 application.2015-01-28.0.log
8780 -rw-r--r--    1 wasext   was         8989019 Apr 14 13:32 application.2015-01-29.0.log

我用参数“-C”创建了tar球文件:

$ tar cvf t.tar -C totar/*
tar: can't change directories to totar/application.2015-01-19.0.log: Not a directory
a totar/application.2015-01-20.0.log 215 blocks
a totar/application.2015-01-21.0.log 1574 blocks
a totar/application.2015-01-22.0.log 2158 blocks
a totar/application.2015-01-23.0.log 4100 blocks
a totar/application.2015-01-24.0.log 1 blocks
a totar/application.2015-01-26.0.log 42151 blocks
a totar/application.2015-01-27.0.log 18592 blocks
a totar/application.2015-01-28.0.log 35823 blocks
a totar/application.2015-01-29.0.log 17557 blocks

我收到这个错误: tar: 无法将目录更改为 totar/application.2015-01-19.0.log: 不是目录

在创建的 tar 文件中缺少文件:totar/application.2015-01-19.0.log:

$ tar tf t.tar
totar/application.2015-01-20.0.log
totar/application.2015-01-21.0.log
totar/application.2015-01-22.0.log
totar/application.2015-01-23.0.log
totar/application.2015-01-24.0.log
totar/application.2015-01-26.0.log
totar/application.2015-01-27.0.log
totar/application.2015-01-28.0.log
totar/application.2015-01-29.0.log

你能帮我看看我错在哪里吗?

我看看能否说得更清楚:当您使用 * 输入任何内容时,您的 shell 会立即将其扩展为与模式匹配的文件列表。

假设您有一个更简单的目录:totar 仅包含 3 个文件:foobarfoobar

当您输入:

tar cvf t.tar -C totar/*

扩展到

tar cvf t.tar -C totar/foo totar/bar totar/foobar

由于-C接受一个参数,tar将下一个参数totar/foo解释为-C的参数。并且只有 totar/bartotar/foobar 保留为要存档的文件。

你真的需要-C吗?因为我建议完全离开 -C

$ tar cvf t.tar totar/*

更新:

如果你想保留 -C 的行为,我强烈建议你看看下面@charles-duffy 的评论。

干杯

通过 tar-C 选项,您告诉 tar 在将文件添加到存档之前先切换到该目录。这非常方便,可以避免必须更改到该目录才能开始。假设您在 $HOME 目录中并且想要将文件存档在 /path/to/totar 中,那么您只需将 -C 选项 提供给 totar[ 上方的目录即可=35=] 和目标目录。如果你想存档 /path/to/totar,那么你想:

tar -C /path/to cvf somearchive.tar totar

注意 上面的文件规范 totar 是一个 目录名 而不是文件名,并且将包括 totar目录。您包括通配符,但您可以包括特定的文件名。仅备份 /path/to/totar:

中的部分文件
tar -C /path/to/totar cvf somearchive.tar filename

filenametotar 目录中。 (不允许使用通配符)