Operating System - HP-UX
1826417 Members
3837 Online
109692 Solutions
New Discussion

commands (or script) to capture specific files.

 
Thomas J Dicks
Occasional Contributor

commands (or script) to capture specific files.

Greetings fellow SA's,
As scripting is not a strong point for me, I need some help from you learned people. I want to use a series of commands (or need a script)
to capture files/directories that were creatred in the year 2001 for archival. I want to move them to a directory called: 2001_files
I was going to use the following at the command line from within the appropriate directory (for this eg the dir is /data/abcdef):
grep '2001' | mv -i * /data/abcedf/2001_files
Can anyone tell me if this is the best way to do it, if not can you suggest a way?


Best regards,
To
5 REPLIES 5
James Beamish-White
Trusted Contributor

Re: commands (or script) to capture specific files.

Hiya!

First, the grep statement you use would not work, as it expects a stdin or file arguement.

A find would be a good start:

find /

But then to get the 2001 bit you'd need to add:

find / -exec ls -ld {} \; | grep 2001

Better performance would be:

find / | xargs ls -ld | grep 2001

Then you want to trim off the file names

find / | xargs ls -ld | grep 2001 | awk '{print $9}'


Then to move enclose the above in grav quotes:

mv `above command` /your/destination/dir



HOWEVER - DO NOT do this from /, as a lot of system files are 2001 dated. Start it off with

find /your/file/directory


Cheers!
James

If it's hard, you're doing it wrong.
GARDENOFEDEN> create light
Christopher McCray_1
Honored Contributor

Re: commands (or script) to capture specific files.

Hello,

Try this:
DIR=/data/abcdef
DESTDIR=/data/abcedf/2001_files

ll $DIR | while read LINE
do
date=`echo $LINE|awk '{print $8}'`
file=`echo $LINE|awk '{print $9}'`

if [ $date = "2001" ]
then
mv "$DIR/$file" $DESTDIR
fi
done

One way of many and probably more eleagant solutions, bu it works

Good luck

Chris
It wasn't me!!!!
Sajid_1
Honored Contributor

Re: commands (or script) to capture specific files.

I have two options, the best one would be find with newer and -atime option. First select the oldest file which was created on 2001, then:
# find . \( -newer filename -a -atime +210 \) -exec ll {} \;

This will list the files whiche were created after 'filename' and which were not accessed for last 210 days (approx. 2002 Jan). if you find the list is correct, then replace ll with 'mv' command.

you can also check a simple command:
# cd dir
# ls -la | grep 2001 | mv ...
learn unix ..
Hai Nguyen_1
Honored Contributor

Re: commands (or script) to capture specific files.

Thomas,

This should work as well.

----- script starts ------
#!/bin/sh

find /data/abcedf -type f | xargs ll | while read $LINE
do
if [ `echo $LINE | awk '{print $8}'` = 2001 ]; then
mv `echo $LINE | awk '{print $9}'` /data/abcedf/2001_files
fi
done
------ script ends ------
Hai
Nick Wickens
Respected Contributor

Re: commands (or script) to capture specific files.

if you really had your heart set on using grep then you could try -

for FILE in $(ll |grep " 2001 "|tr -s " "|cut -d" " -f9)
do
mv $FILE /data/abcdef/2001_files
done

Or to ensure that the file is not actually a directory -

for FILE in $(ll |grep " 2001 "|tr -s " "|cut -d" " -f9)
do
if [ -f $FILE ]
then
mv $FILE /data/abcdef/2001_files
fi
done

Then again as already mentioned find may be better and theres an option to find called -mtime which would locate files more than a specified number of days ago - eg

find /data/abcdef -mtime +365

You could do something like this then -

DAY=$(date +%j)
for FILE in $(find /data/abcdef -mtime +$DAY)
do
mv $FILE /data/abcdef/2001_files
done

This would find all files older than the current year.

Hats ? We don't need no stinkin' hats !!