bash 使用 shell 脚本时,如何复制名称包含空格和 UNICODE 的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2550791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:53:42  来源:igfitidea点击:

How can I copy files with names containing spaces and UNICODE, when using a shell script?

bashshellunicodefilescripting

提问by LOlliffe

I have a list of files that I'm trying to copy and move (using cp and mv) in a bash shell script. The problem that I'm running into, is that I can't get either command to recognize a huge number of files, seemingly because the filenames contain spaces and/or unicode characters. I couldn't find any switches to decode/re-encode these characters. Instead, for example, if I copy "file name.xml", I get "*.xml" and a script error that the file wasn't found for my result. Does anyone know settings or commands that will deal with these files?

我有一个我试图在 bash shell 脚本中复制和移动(使用 cp 和 mv)的文件列表。我遇到的问题是,我无法使用任一命令来识别大量文件,这似乎是因为文件名包含空格和/或 unicode 字符。我找不到任何开关来解码/重新编码这些字符。相反,例如,如果我复制“文件名.xml”,我会收到“*.xml”和一个脚本错误,提示我的结果找不到该文件。有谁知道将处理这些文件的设置或命令?

EDIT(adding current code): When I run:

编辑(添加当前代码):当我运行时:

MacBookPro:Desktop$ ./script.sh

MacBookPro:桌面$ ./script.sh

#!/bin/sh
dateVar=`date +%Y-%m-%d`
mkdir /Volumes/Documents/SMSarchive/$dateVar
cd /Volumes/Documents/SMSarchive/SMSdrop
for i in *.xml
do
cp $i /Volumes/Documents/SMSarchive/$dateVar/$dateVar-$i
done

I get the message:

我收到消息:

usage: cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] source_file ... target_directory

用法:cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] source_file ... target_directory

...when it hits the "cp" command. There's actually more to the script, that processes the copied files further. With a "regular" file name e.g. 'file.xml', everything works fine. It's only files with spaces, or Unicode characters, where I have problems.

...当它点击“cp”命令时。脚本实际上还有更多内容,可以进一步处理复制的文件。使用“常规”文件名,例如“file.xml”,一切正常。它只是带有空格或 Unicode 字符的文件,我遇到了问题。

回答by Ignacio Vazquez-Abrams

Problems with spaces indicates that insufficient quoting has been done. The following is incorrect:

有空格的问题表明引用不充分。以下是不正确的:

someprogram $file

The correct version is as follows:

正确的版本如下:

someprogram "$file"

回答by The HCD

watch out for code errors when $i is null! This can be the result of fatal rm -Rf errors!

当 $i 为空时注意代码错误!这可能是致命的 rm -Rf 错误的结果!

回答by Christopher Hunter

Sometimes there are easier ways than dealing with shell quoting. In your case I would use find -print0and xargs -0. The null character termination allows you to easily manipulate strings with spaces (i.e lists of filenames).

有时有比处理 shell 引用更简单的方法。在你的情况下,我会使用find -print0and xargs -0。空字符终止允许您轻松操作带空格的字符串(即文件名列表)。

For your example, code would look something like this:

对于您的示例,代码如下所示:

#!/bin/sh
dateVar=`date +%Y-%m-%d`
mkdir /Volumes/Documents/SMSarchive/$dateVar
cd /Volumes/Documents/SMSarchive/SMSdrop
find . -maxdepth 1 -name '*.xml' -print0 | \
xargs -0 -I {} cp {} /Volumes/Documents/SMSarchive/$dateVar/$dateVar-{}