1

This question already has an answer here:

I am trying to copy some files with spaces and $, @ symbols in their file names in a bash script but the script fails to copy the files stating it cannot find the file. I can see that it is treating each space separated word in file name as another file name which is why it is failing. Following is my code:

cp "$TRX_SOURCE_PATH/*TRX*" $DEST_PATH

Error:

cp: cannot stat `/pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/*TRX*': No such file or directory

If i do a ls i can see the file names:

# 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  Morning$UW@20150801.xls
-rw-r--r--. 1 root root  856064 Jul 27 11:54 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$KK@20150801.xls
-rw-r--r--. 1 root root  856064 Jul 27 11:54 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$HP@20150801.xls
-rw-r--r--. 1 root root 1254400 Aug  1 04:43 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$AP@20150801.xls
-rw-r--r--. 1 root root 2770944 Aug  1 04:48 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$DL@20150801.xls
-rw-r--r--. 1 root root 1707008 Aug  1 04:57 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$UE@20150801.xls
-rw-r--r--. 1 root root 1204736 Aug  1 09:42 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$AS@20150801.xls
-rw-r--r--. 1 root root 1204736 Aug  1 09:44 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$NE@20150801.xls
-rw-r--r--. 1 root root 3048448 Aug  1 10:24 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$RJ@20150801.xls
-rw-r--r--. 1 root root 1294336 Aug  1 10:40 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$CH@20150801.xls
-rw-r--r--. 1 root root 1153536 Aug  1 10:45 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$KL@20150801.xls
-rw-r--r--. 1 root root 1108992 Aug  1 11:20 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$HR@20150801.xls
-rw-r--r--. 1 root root 1108992 Aug  1 11:33 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$JK@20150801.xls
-rw-r--r--. 1 root root 1302016 Aug  1 11:48 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$PB@20150801.xls
-rw-r--r--. 1 root root 1150976 Aug  1 11:57 /pmautomation/PM/Report_Output/CFBLOCKTRUMNG/2015-08-01/Bharti Blocked TRX Report  Morning$TN@20150801.xls

This directory has many files and i am interested in pulling only files with the following names:

Bharti Blocked TRX Report  Morning$TN@20150801.xls

where the TN and datestamps changes.

How do i fix this to make the cp command work in the bash script?

EDIT: I read the other question with the script choking on whitespace and special characters and found that i can use double quotes for it. I have tried it but it won't work. Also, the script also fails for the following command:

cp: cannot stat `/pmautomation/PM/StaticUpload/20150801/2G_SITEDB_*.csv': No such file or directory

where These files do not have any spaces in them:

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

marked as duplicate by roaima, chaos, G-Man, Drav Sloan, cuonglm Aug 2 '15 at 16:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • The answer mentions to use quotes for the variable which i am already using and it won't work. – Ankit Vashistha Aug 2 '15 at 14:24
  • see the "How do I process a list of file names?" section – Evgeny Vereshchagin Aug 2 '15 at 14:25
8

The glob must be left unquoted for it to be treated as a glob. The variables should be quoted:

cp -- "$TRX_SOURCE_PATH"/*TRX* "$DEST_PATH"
  • 1
    Thanks a lot. This one worked. However, i used just cp "$TRX_SOURCE_PATH"/*TRX* "$DEST_PATH". Could you please explain the significance of -- here? – Ankit Vashistha Aug 3 '15 at 4:43
  • You need to google and understand how quotes allow a special character to be special or not. It is choking because it is parsing your characters as if they had special meaning and were not just characters in your file name string. – Baazigar Aug 3 '15 at 16:24
  • @Stéphane Chazelas What is -- for? – Nikhil Sep 16 '18 at 12:21
0
#!/bin/bash

TRX_SOURCE_PATH='/src/path'
DEST_PATH='/your/dest'

cp $TRX_SOURCE_PATH/Bharti\ Blocked\ TRX\ Report\ \ Morning\$*@* $DEST_PATH

should work

Not the answer you're looking for? Browse other questions tagged or ask your own question.