cp: cannot access

Hi all,

I have this statement:

cp `ls -lrt |awk '{if($6=="Feb") print $9}'|wc -l` /smartapp/reports/archive_jan_feb_march/

However it fails with:

cp: cannot access 948

The owner for this script is not a concern, May I know which part went wrong?

Hi,

Because there is no file with the name 948, try it without the "|wc -l";

cp `ls -lrt |awk '{if($6=="Feb") print $9}'` /smartapp/reports/archive_jan_feb_march/

Regards

Dave

Firstly, you parse ls. This will fail with filenames containing whitespace. Then it pipe it to wc so you'd just get a count. Seems you've 948 files from Feb, probably..

cp 948 /smartapp/reports/archive_jan_feb_march/ won't work if you've no file named 948. Likely, you would want at least something like

ls -lrt | awk '$6 == "Feb" { print $9 }' | while read file
do
    cp "$file" /smartapp/reports/archive_jan_feb_march/
done

which still suffers from errors of white space.

with bash you can do something like this.

touch -t 201201010000 start
touch -t 201204010000 end

for f in *; do
  if [[ $f -nt start ]] && [[ $f -ot end ]]; then
    cp "$f" /smartapp/reports/archive_jan_feb_march/
  fi
done

awk '{if($6=="Feb") print $9}'
returns all files modified in February.
I think the problem is in 'wc -l'

edit: guess I'm too slow, oh well.

dear all thanks for your replay

i have tired following code

cp `ls -lrt  *.txt | awk  '{if($6 == "Feb") print $9}'` /smartapp/reports/archive_jan_feb_march/

it copies the files but it copies only today's files not the month of Feb.
please help as soon as possible.

Given you have a find installed compatible to

find (GNU findutils) 4.4.2

that allows for the -newerXY test, try

find .   -newermt "12-07-27" ! -newermt "12-07-29" -exec cp {} /destdir + 

You may have to fiddle around with the X char in -newerXY, the date strings, and also with the cp params. Good luck!

Creating sentinels with touch is a nice approach, imo. A lot less messy and less error prone than trying to parse ls output (whose date format is locale dependent).

The -nt and -ot tests will also work with ksh.

For a more portable approach, find with -newer start and ! -newer end should work nicely. pax could then be used to handle all the copying without having to resort to many cp invocations.

find . -type f -name '.*txt' -newer start ! -newer end | pax -rw "$dest"

If recursing into subdirectories is undersirable, -maxdepth can be used. If not available, a slightly more cumbersome use of -prune can do the job.

Regards,
Alister

---------- Post updated at 10:37 AM ---------- Previous update was at 10:28 AM ----------

To my knowledge, all implementations of find which support -exec ... + require {} to be the word immediately preceding + .

-exec cp {} /destdir + will not work with most (if not all) implementations. If using GNU coreutils, cp can be made to work with the -t option.

-exec cp -t /destdir {} +

Regards,
Alister

@ Alister: Your absolutely right, + needs to follow {} immediately. So your cp -t proposal should apply. Sorry for not testing with cp, only echo... btw, how do you get those inline code tags?

Using icode tags.

Regards,
Alister

1 Like