我正在尝试在 bash 脚本中复制文件名中包含空格和$
,@
符号的一些文件,但该脚本无法复制文件,说明找不到该文件。我可以看到它将文件名中的每个空格分隔的单词视为另一个文件名,这就是它失败的原因。以下是我的代码:
cp "$TRX_SOURCE_PATH/*TRX*" $DEST_PATH
错误:
cp: cannot stat `/pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/*TRX*': No such file or directory
如果我这样做,ls
我可以看到文件名:
# ls -lrt /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/*TRX*
-rw-r--r--. 1 root root 856064 Jul 27 11:54 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 856064 Jul 27 11:54 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 856064 Jul 27 11:54 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 1254400 Aug 1 04:43 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 2770944 Aug 1 04:48 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 1707008 Aug 1 04:57 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 1204736 Aug 1 09:42 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 1204736 Aug 1 09:44 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 3048448 Aug 1 10:24 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 1294336 Aug 1 10:40 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 1153536 Aug 1 10:45 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 1108992 Aug 1 11:20 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 1108992 Aug 1 11:33 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 1302016 Aug 1 11:48 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
-rw-r--r--. 1 root root 1150976 Aug 1 11:57 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report [email protected]
该目录有很多文件,我只想提取具有以下名称的文件:
Bharti Blocked TRX Report [email protected]
TN 和日期戳发生变化的地方。
我该如何解决这个问题以使cp
命令在脚本中工作bash
?
编辑:我阅读了另一个问题,脚本中的空格和特殊字符令人窒息,发现我可以使用双引号。我已经尝试过,但行不通。此外,该脚本对于以下命令也会失败:
cp: cannot stat `/pmautomation/PM/StaticUpload/20150801/2G_SITEDB_*.csv': No such file or directory
其中这些文件中没有任何空格:
ls -lrt /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_*.csv
-rw-r--r--. 1 root root 4850694 Aug 2 06:51 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_KL.csv
-rw-r--r--. 1 root root 4743676 Aug 2 06:55 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_PB.csv
-rw-r--r--. 1 root root 2812108 Aug 2 07:05 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_AS.csv
-rw-r--r--. 1 root root 1934089 Aug 2 07:15 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_CH.csv
-rw-r--r--. 1 root root 2360597 Aug 2 07:30 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_JK.csv
-rw-r--r--. 1 root root 1685844 Aug 2 07:35 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_NE.csv
-rw-r--r--. 1 root root 8355408 Aug 2 07:47 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_TN.csv
-rw-r--r--. 1 root root 8356293 Aug 2 07:51 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_UE.csv
-rw-r--r--. 1 root root 3422073 Aug 2 11:04 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_DL.csv
-rw-r--r--. 1 root root 6989514 Aug 2 17:34 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_RJ.csv
-rw-r--r--. 1 root root 1276063 Aug 2 18:35 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_HP.csv
-rw-r--r--. 1 root root 2585368 Aug 2 18:50 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_HR.csv
-rw-r--r--. 1 root root 5975056 Aug 2 19:18 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_UW.csv
-rw-r--r--. 1 root root 6558770 Aug 2 19:29 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_KK.csv
-rw-r--r--. 1 root root 10222883 Aug 2 19:33 /pmautomation/PM/StaticUpload/20150801/2G_SITEDB_AP.csv
答案1
该 glob 必须不加引号才能被视为 glob。变量应该被引用:
cp -- "$TRX_SOURCE_PATH"/*TRX* "$DEST_PATH"
答案2
#!/bin/bash
TRX_SOURCE_PATH='/src/path'
DEST_PATH='/your/dest'
cp $TRX_SOURCE_PATH/Bharti\ Blocked\ TRX\ Report\ \ Morning\$*@* $DEST_PATH
应该管用