简体   繁体   English

压缩文件列表时,文件名中包含空格

[英]Compressing a list of files contain spaces in their file-names

I've been trying to compress a set of files contain spaces in thier file-names using a bash file. 我一直在尝试使用bash文件压缩一组文件中包含空格的文件。 The bash file is: bash文件是:

#!/bin/bash
tar -cPf 'myconfigs.tar' `cat myconfigs.list`

The content of myconfigs.list file is: myconfigs.list文件的内容为:

/home/anas/.config/chromium/Default/Bookmarks
/home/anas/.config/chromium/Default/Login Data
/home/anas/.config/chromium/Default/Login Data-journal

The problem is that files contain spaces in their file-names don't be included in the result TAR archive. 问题是文件的文件名中包含空格,但不包含在结果TAR归档文件中。 I tried '', "", %20... but didn't work. 我尝试了“”,“”,%20 ...,但是没有用。

Thanks for your help in advance. 感谢您的帮助。

Anas, 阿纳斯

I think you need to use: 我认为您需要使用:

tar -cPf myconfigs.tar -T myconfigs.list

instead of your "cat" . 而不是您的“猫”。 cat should work too if you properly escape the filenames inside, but -T is better. 如果您正确地转义其中的文件名,cat也应该起作用,但是-T更好。

UPDATED (to address your question in question's comments): I cannot comment in your question (don't have enough reputation), so I decided to improve my answer instead. 更新 (以解决您对问题的评论中的问题):我无法对您的问题发表评论(信誉不高),因此我决定改进答案。

The tilde (~) expansion is a shell thing, tar does not support it. 代字号(〜)扩展名是shell,而tar不支持。 However, to achieve what you want to achieve, you can use the following trick: 但是,要实现您想要的目标,可以使用以下技巧:

  • use relative paths in your myconfigs.list file (relative to the home directory): 在myconfigs.list文件中使用相对路径(相对于主目录):
.config/chromium/Default/Bookmarks
    .config/chromium/Default/Login Data
    .config/chromium/Default/Login Data-journal
  • run tar in such way that it changes directory to your home on startup: 以以下方式运行tar,以便在启动时将目录更改为您的主目录:
tar -cPf myconfigs.tar -C ~/ -T myconfigs.list

OK, I went ahead and created a sample session illustrating it: 好的,我继续创建了一个示例会话来说明这一点:

root@web:~ # useradd -m galaxy
root@web:~ # su - galaxy
galaxy@web:~ $ mkdir -p {1,2}/{3,4}/{5,6,7}
galaxy@web:~ $ find . -xdev -type d -exec touch '{}/file.txt' \;
galaxy@web:~ $ cat << EOF > include.lst
> 1/3
> 1/4/5/file.txt
> 1/4/7
> 2/file.txt
> EOF
galaxy@web:~ $ cd 2/3/6
galaxy@web:~/2/3/6 $ tar cjSpf ~/sample.tar.bz2 -C ~/ -T ~/include.lst
galaxy@web:~/2/3/6 $ cd
galaxy@web:~ $ tar tjvf sample.tar.bz2
drwx------ galaxy/galaxy     0 2014-02-19 04:10 1/3/
-rw------- galaxy/galaxy     0 2014-02-19 04:10 1/3/file.txt
drwx------ galaxy/galaxy     0 2014-02-19 04:10 1/3/7/
-rw------- galaxy/galaxy     0 2014-02-19 04:10 1/3/7/file.txt
drwx------ galaxy/galaxy     0 2014-02-19 04:10 1/3/6/
-rw------- galaxy/galaxy     0 2014-02-19 04:10 1/3/6/file.txt
drwx------ galaxy/galaxy     0 2014-02-19 04:10 1/3/5/
-rw------- galaxy/galaxy     0 2014-02-19 04:10 1/3/5/file.txt
-rw------- galaxy/galaxy     0 2014-02-19 04:10 1/4/5/file.txt
drwx------ galaxy/galaxy     0 2014-02-19 04:10 1/4/7/
-rw------- galaxy/galaxy     0 2014-02-19 04:10 1/4/7/file.txt
-rw------- galaxy/galaxy     0 2014-02-19 04:10 2/file.txt
galaxy@web:~ $

This should give you a start :) 应该给您一个开始:)

Don't use cat's output like that otherwise space is considered a delimiter and a separete argument to the tar command. 不要像这样使用cat的输出,否则空格将被视为tar命令的定界符和分离参数。

You can use -T tar option : 您可以使用-T tar选项

tar -cPv -T myconfigs.list -f myconfigs.tar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM