简体   繁体   English

Linux bash Shell脚本-文件名中的空格

[英]Linux bash shell scripts - spaces in file names

It has been a long time since I did much bash script writing. 自从我做了很多bash脚本编写以来已经很长时间了。

This is a bash script to copy and rename files by deleting all before the first period delimiter: 这是一个bash脚本,用于通过删除第一个句点定界符之前的所有内容来复制和重命名文件:

#!/bin/bash

mkdir fullname
mv *.audio fullname
cd fullname

for x in * ;
do
  cp $x ../`echo $x | cut -d "." -f 2-`
done

cd ..
ls

It works well for file names with no embedded spaces but not for those with spaces. 它适用于没有嵌入空格的文件名,但不适用于有空格的文件名。

How can I change the code to fix this simple Linux bash script? 如何更改代码以修复此简单的Linux bash脚本? Any suggestions for improving the code for other reasons would also be welcome. 任何其他出于其他原因改进代码的建议也将受到欢迎。

Example filenames, some with embedded spaces and some not (from link) 示例文件名,有些带有嵌入空格,有些没有(来自链接)

http://www.homenetvideo.com/demo/index.php?/Radio%20%28VLC%29 http://www.homenetvideo.com/demo/index.php?/Radio%20%28VLC%29

Ambient.A6.SOMA Space Station.audio
Blues.B9.Blues Radio U.K.audio
Classical.K3.Radio Stephansdom - Vienna.audio
College.CI.KDVS U of California, Davis.audio
Country.Q1.K-FROG.audio
Easy.G4.WNYU.audio
Eclectic.M2.XPN.audio
Electronica.E2.Rinse.audio
Folk.F1.Radionomy.audio
Hiphop.H1.NPR.audio
Indie.I4.WAUG.audio
Jazz.J6.KCSM.audio
Latin.L3.Mega.audio
Misc.X7.Gaydio.audio
News.N9.KQED.audio
Oldies.O1.Lonestar.audio
OldTime.Y1.Roswell.audio
Progressive.P1.Aural Moon.audio
Rock.R8.WXRT.audio
Scanner.Z3.Montreal.audio
Soul.S1.181.FM.audio
Talk.T2.TWiT.audio
World.W3.Persian.audio

http://lh5.googleusercontent.com/-QjLEiAtT4cw/U98_UFcWvvI/AAAAAAAABv8/gyPhbg8s7Bw/w681-h373-no/homenet-radio.png http://lh5.googleusercontent.com/-QjLEiAtT4cw/U98_UFcWvvI/AAAAAAAABv8/gyPhbg8s7Bw/w681-h373-no/homenet-radio.png

Whenever you deal with file names that might have spaces in them, you must reference them as "$x" rather than just $x . 每当处理可能在其中带有空格的文件名时,都必须将它们引用为"$x"而不是$x That's what's causing your cp command to fail. 这就是导致cp命令失败的原因。

Your echo command is also problematic. 您的echo命令也是有问题的。 Although echo does the right thing for simple spaces - it echoes a file named ABC as ABC - it will still fail if you have more than one consecutive space in the name, or whitespace that isn't a simple space character. 尽管echo对于简单的空格做了正确的事情-它会将名为ABC的文件作为ABC -如果名称中包含多个连续的空格或不是简单空格字符的空白,它将仍然失败。

Instead of passing the file names to external programs for processing, which always requires getting them through the whitespace-hostile command line, you should use bash built-in functions for string manipulations wherever possible, eg ${x%%foo} , ${x#bar} and similar functions. 与其将文件名传递给外部程序进行处理(这总是需要通过空格敌对的命令行来进行处理),不如使用${x%%foo} bash内置函数进行字符串操作,例如${x%%foo}${x#bar}和类似功能。 The man page describes them under "Parameter expansion". 手册页在“参数扩展”下描述了它们。

Here's my suggestion: 这是我的建议:

#!/bin/bash
shopt -s nullglob
mkdir fullname
mv *.audio fullname
(
  cd fullname || exit
  for x in *; do
    cp "$x" "../${x#*.}"
  done
)
ls
  • nullglob prevents * from presenting itself if no file matches it. 如果没有文件匹配,则nullglob防止*出现。 Just optional. 只是可选的。
  • () summons a subshell and saves you from changing back to another directory. ()召唤一个子Shell,使您不必再切换回另一个目录。
  • || exit || exit terminates the subshell if cd fails to change directory. 如果cd无法更改目录,则|| exit终止子外壳。
  • ${x#*.} removes the <first>. ${x#*.}删除<first>. from $x and expands it. $x扩展它。

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

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