Computing.Net > Forums > Unix > Moving recent files to another dire

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Moving recent files to another dire

Reply to Message Icon

Name: kanaka
Date: March 3, 2008 at 06:20:11 Pacific
OS: unix
CPU/Ram: n/a
Product: n/a
Comment:

HI Gurus,

I have more than one file in a directory with different dates like file1-2212008.txt,file2-2222008.txt,file3-3012008.txt .what I need to do is I have to move most recent file (file3-3012008.txt )to one directoy and all others to another directory.Can you please tell me how to writ unix script

Thanks
Kanaka




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: March 3, 2008 at 09:13:37 Pacific
Reply:

I'm making a big assumption: namely, that the unix timestamps on the files follow your file naming convention. If they do, use the -t switch of the ls command. If they don't, you'll have to rip the date from the file name, and that's more work than I care to do:


#!/bin/ksh

cd /to/my/directory
i=0
ls -1t|while read ff
do
((i=i+1))
if [[ $i -eq 1 ]]
then
cp "$ff" /my/one/directory
else
cp "$ff" /my/other/directory
fi
done



Change the directory listing to suit your environment, and don't change cp to mv until you are sure the script is correct.

0

Response Number 2
Name: kanaka
Date: March 3, 2008 at 09:42:38 Pacific
Reply:

Thanks Nails for your reply.

Mean while I can use the logic that you send to move files

Unfortunately some times unix timestamp and the date on file doesn't match.Clients may send 2 files (today's and yesterday's ) on same day If their yesterday's file is not ready.Can you please give me some idea (like Pseudocode) I will try that

Thanks
Kanaka


0

Response Number 3
Name: nails
Date: March 3, 2008 at 11:39:01 Pacific
Reply:

Here's what I would do:

1) Strip out the date from the the file name. Given that the file always starts with the structure you gave, this sed command should do it:


fname="file1-3012008.txt"
# strip off the beginning file.. and the ending .txt:
echo "$fname"|sed -e 's/^file[0-9]*-//' -e 's/\.txt$//'

2) You can NOT sort the date in the format 3012008. Use the cut command to get the Year, month and day, i.e. 2008301:

datestr="3012008"
# get the length of the datestr using ksh pattern
dlen=${#datestr}

# get the year
yr=$(echo $datestr|cut -c $((dlen-3))-$dlen)
# get the month/day
md=$(echo $datestr|cut -c 1-$((dlen-3)) )

# create the newdate string
newdatestr="${yr}${md}"
echo $newdatestr

3) Now, if you cycle thru the directory as I showed you, for each file, strip out the date as in step 1).

4) Create a newdatestring as in step 2).

5) Build a string with the filename and the newdatestring:

# make sure a space exists between the strings:
string="$ff $newdatestring"

and echo it to a newfile:

echo $string >> /tmp/newfile

6) Now, sort /tmp/newfile on the second field which is the newstring sending the output to a different file.

7) different file should now have your files in the correct order. Cycle thru different file and perform your move.


I've done the hard part stripping out the date and reformatting it. I'll leave putting to together to you.


0

Response Number 4
Name: kanaka
Date: March 4, 2008 at 05:06:08 Pacific
Reply:

Thank you very much nails for your help.I will try it today


Thanks
Kanaka


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


only want the interger date command



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Moving recent files to another dire

rm files > 30 days to another dir www.computing.net/answers/unix/rm-files-30-days-to-another-dir/7255.html

variables from one file to another www.computing.net/answers/unix/variables-from-one-file-to-another/7378.html

Copy Print Job To Another Queue www.computing.net/answers/unix/copy-print-job-to-another-queue/6867.html