Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to cp all files from November. Any ideas? 1

Status
Not open for further replies.

Drill

Technical User
May 2, 2001
32
GB
I just need some assitence with a small task i need to do. I have a directory from which all the files that were created in Nov i need to copy to another directory.

I have tried combinations of grep etc but cant seem to get it right.
Can anyone assist please?

Thanks :)
 
Hi,
How about this:
for i in `ls -la | grep Nov`
do
cp $i newdir
done
(this presumes none of the file names contain "Nov")
 
Tried that and get :
cp: cannot access ls: No such file or directory
cp: cannot access -la: No such file or directory
cp: cannot access |grep: No such file or directory
cp: cannot access Nov: No such file or directory

I take it that it is not reading in the 'ls....' section correctly.

I have been playing with the below:

for i in $(ls -l|grep Nov ); do
cat $i >/tmp/junk/output
cp $i /tmp/old
done

I get errors when it tries to copy , and this is because it only seems to grep the date entry , not the file.; so it tries to copy the date!
Sorry to be so lame mrregan, but can you give me any more guidence??
 
Sorry ,read the `` as '' .
But it still fails with:
cp: illegal option -- w
Usage: cp [-f|-i] [-p] [-e warn|force|ignore] source_file target_file
cp [-f|-i] [-p] [-e warn|force|ignore] source_file ... target_directory
cp [-f|-i] [-p] -R|-r [-e warn|force|ignore] source_directory ... target
_directory
cp: cannot access root: No such file or directory
cp: cannot access root: No such file or directory
cp: cannot access 29: No such file or directory
cp: cannot access Nov: No such file or directory
cp: cannot access 20: No such file or directory
cp: cannot access 14:42: No such file or directory
 
Hi - are you sure you've included the backquotes (` - probably above the tab key on your keyboard, not the usual single quotes) around the ls -la | grep Nov part of the script?

These enable the script to interpret this command as a whole and not as it's constituent parts, which I think is what's happening.

Hope this helps.
 
Our last posts must have crossed. Anyway, the following works (replace my echo $i with your cp $i newdir line):

for i in `ls -la | grep Nov | tr -s ' ' ' ' | cut -f9 -d' '`
do
echo $i
done

Good luck.
 
Many thanks Ken & mmregan , the last post cracked it!!!
I was just reading up on 'tr' so you have saved me hours!!!

Thanks Again.
 
for i in `ls -la|awk '{if ( $6 == "Nov" ) print $NF}'`
do
cp $i newdir
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top