[英]Shell script issue with directory and filenames containing spaces
I understand that one technique for dealing with spaces in filenames is to enclose the file name with single quotes: "'"
.I have a directory and a filename with space. 我知道一种处理文件名中空格的技术是用单引号将文件名括起来:
"'"
。我有一个目录和一个带空格的文件名。 I want a shell script to read all the files along with the posted time and directory name. 我希望shell脚本读取所有文件以及发布的时间和目录名称。 I wrote the below script:
我写了以下脚本:
#!/bin/bash
CURRENT_DATE=`date +'%d%m%Y'`
Temp_Path=/appinfprd/bi/infogix/IA83/InfogixClient/Scripts/IRP/
find /bishare/IRP_PROJECT/SFTP/ -type f | xargs ls -al > $Temp_Path/File_Posted_$CURRENT_DATE.txt
which is partially working. 这是部分工作。 It is not working for the directory and files that has a space in it.
它不适用于目录和其中有空格的文件。
Use find -print0 | xargs -0
使用
find -print0 | xargs -0
find -print0 | xargs -0
to reliably handle file names with special characters in them, including spaces and newlines. find -print0 | xargs -0
可以可靠地处理其中包含特殊字符(包括空格和换行符)的文件名。
find /bishare/IRP_PROJECT/SFTP/ -type f -print0 |
xargs -0 ls -al > "$Temp_Path/File_Posted_$CURRENT_DATE.txt"
Alternatively, you can use find -exec
which runs the command of your choice on every file found. 另外,您可以使用
find -exec
在每个找到的文件上运行您选择的命令。
find /bishare/IRP_PROJECT/SFTP/ -type f -exec ls -al {} + \
> "$Temp_Path/File_Posted_$CURRENT_DATE.txt"
In the specific case of ls -l
you could take this one step further and use the -ls
action. 在
ls -l
的特定情况下,您可以更进一步,并使用-ls
操作。
find /bishare/IRP_PROJECT/SFTP/ -type f -ls > "$Temp_Path/File_Posted_$CURRENT_DATE.txt"
You should also get in the habit of quoting all variable expansions like you mentioned in your post. 您还应该养成引用所有变量扩展的习惯,就像您在帖子中提到的那样。
You can change the IFS variable for a moment ( Internal Fields Separator ): 您可以暂时更改IFS变量( Internal Fields Separator ):
#!/bin/bash
# Backing up the old value of IFS
OLDIFS="$IFS"
# Making newline the only field separator - spaces are no longer separators
# NOTE that " is the last character in line and the next line starts with "
IFS="
"
CURRENT_DATE=`date +'%d%m%Y'`
Temp_Path=/appinfprd/bi/infogix/IA83/InfogixClient/Scripts/IRP/
find /bishare/IRP_PROJECT/SFTP/ -type f | xargs ls -al > $Temp_Path/File_Posted_$CURRENT_DATE.txt
# Restore the original value of IFS
IFS="$OLDIFS"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.