2

My OS is Solaris 10 (64 bit) and I have many files in a logs directory where we receive 40-50 logs every day. I have last 20 days file present in this directory and I want to move each day file to a particular directory whose name is appended with the date of file.

For example

File_name  date
1.txt      31-jul-20122 
2.txt      01-Aug-2012

then create a directory for old files belongs to 31-jul-2012 and move to new directory 31-jul RIO_31-Jul-2012 and move them and like wise for 01-aug files.

I have written a scripts which is running fine for current date but i don't know how t modify it to accomdate old date.

My scripts is as below

NEWDIR=RIO_31-Jul-2012
mkdir /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/$NEWDIR
cd /var/opt/ers/logs/rio
touch -t `date +%Y%m%d0000` dummy
find . -newer dummy -type f  |
while read fname
do
    mv $fname /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/$NEWDIR
done

2 Answers 2

1

Under Solaris, you can get a file's date in a usable format with ls -E.

Iterate over all the files in the log directory. For each file, obtain the date, create the target directory if needed, and move the file.

for f in /var/opt/ers/logs/rio/*; do
  ymd=$(ls -Eog "$f" | awk '{print $4}')
  target=/ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_$ymd
  mkdir -p "$target"
  mv "$f" "$target"
done

Note that the archive directories will have names like RIO_2012-07-31 rather than RIO_31-Jul-2012. I recommend the Y-M-D format because it sorts in chronological order. If you really need to have abbreviated month names and the DMY order, use ls -e instead of ls -E and change the awk call accordingly:

  mdy=$(ls -eog "$f" | awk '{print $5 "-" $4 "-" $7}')

If you need to look for files in subdirectories recursively, call find to iterate over all the files and, again, execute the shell snippet for each file.

find /var/opt/ers/logs/rio -type f -exec sh -c '
  ymd=$(ls -Eog "$0" | awk "{print \$4}")
  target=/ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_$ymd
  mkdir -p "$target"
  mv "$0" "$target"
' {} \;

With find, you can add ! -newer somefile before -exec to only match files that are older than somefile. You can add -mtime 42 to only match files that were modified at least 42 days ago.

0

You could find files by ctime o atime, then move the listed files in the right dir:

find . -iname "*.txt" -ctime -<daysago> -type f

"days ago" is an integer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .