Anand28
October 15, 2008, 10:58am
1
Hi ,
i need to write a simple to script in KSh to move some files ,
but these files have spaces in there name, resulting in error in transfering
eg i have following files
30222_Deal Ticket_20071227203.csv
30222_Deal Ticket_20071227204.csv
30222_Deal Ticket_20071227205.csv
and i want to move them to some other folder ,
i am using follwing script
for i in `ls *20071227*`
do
cp "$i" ./NewFolder
done
this script gives error
cp: cannot access 30222_Deal
cp: cannot access Ticket_20071227203.csv
Please help
danmero
October 15, 2008, 11:10am
2
Look like you don't have permissions to read that files.
Anand28
October 15, 2008, 11:13am
3
Permissions are available,
just try to replicate the scenario mentioned above
danmero
October 15, 2008, 11:22am
4
You don't have any space in your file names and you don't really need ls
$ for i in *20071227*; do echo cp $i ./NewFolder;done
cp Ticket_20071227203.csv ./NewFolder
cp Ticket_20071227204.csv ./NewFolder
cp Ticket_20071227205.csv ./NewFolder
$ ls NewFolder/
Ticket_20071227203.csv Ticket_20071227204.csv Ticket_20071227205.csv
Your problem is:
Not only is ls unnecessary, it is breaking your script.
Use wildcard expansion directly:
cp *20071227* ./NewFolder
Similarly, when you need to use a loop (which you don't in this case):
for i in *20071227*
do
cp "$i" ./NewFolder
done
Anand28
October 15, 2008, 11:28am
6
actually these are 3 files
1.) 30222_Deal Ticket_20071227203.csv
2.) 30222_Deal Ticket_20071227204.csv
3.) 30222_Deal Ticket_20071227205.csv
and they have spaces in between
Anand28
October 15, 2008, 11:32am
7
oh, yes cp is working without ls
but can any one explian how and why this is creating a problem
danmero
October 15, 2008, 12:07pm
8
Please post your sample data with the code tags to preserve indentation. This makes it much easier to read.
And here is the output using your example:
$ ls
30222_Deal Ticket_20071227203.csv 30222_Deal Ticket_20071227205.csv
30222_Deal Ticket_20071227204.csv NewFolder
$ ls NewFolder/
$ for i in *20071227*;do cp "$i" ./NewFolder;done
$ ls
30222_Deal Ticket_20071227203.csv 30222_Deal Ticket_20071227205.csv
30222_Deal Ticket_20071227204.csv NewFolder
$ ls NewFolder/
30222_Deal Ticket_20071227203.csv 30222_Deal Ticket_20071227205.csv
30222_Deal Ticket_20071227204.csv