1752477 Members
5674 Online
108788 Solutions
New Discussion юеВ

script to move files

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

script to move files

i am trying to write a function that checks the date of files in a directory and if the files are not equal to today then create the dir:

set -x
for i in `ls -l |awk '{print $6,$7}' |uniq`
do
if [ $i != $TODAY ] ; then
DIR=$($i |sed s/' '//)
mkdir $DIR
fi
done
+ ls -l
+ uniq
+ awk {print $6,$7}
+ [ 06 != 20 Jul ]
+ + sed s/ //
+ 06


how can i parse "06 Jun" into [ $i != $TODAY ]as one string?

Manyb Thanks
hello
13 REPLIES 13
Jeff_Traigle
Honored Contributor

Re: script to move files

I don't see where you set TODAY. Setting it as such, should do the trick for you:

TODAY=$(date +"%d %b")
--
Jeff Traigle
Enrico P.
Honored Contributor
Solution

Re: script to move files

Hi,
try with this:

ls -l |awk '{print $6,$7}' |uniq|while read i
do
echo $i
done

Enrico
Enrico P.
Honored Contributor

Re: script to move files

If you want exclude the directory from the ls command:

ls -l |grep -v ^d|awk '{print $6,$7}' |uniq|while read i
do
if [ $i != $TODAY ] ; then
DIR=$($i |sed s/' '//)
mkdir $DIR
fi
done

Enrico
Enrico P.
Honored Contributor

Re: script to move files

And you need to quote the variables in the:

if [ "$i" != "$TODAY" ]

Sorry
Enrico
Victor Fridyev
Honored Contributor

Re: script to move files

Hi,

If I understand you correctly, you need something like that:

#!/bin/sh
TODAY=$(date +%d%b)
/bin/ls -lt |grep -v ^d |while read FN; do
DATE=$(echo $FN|awk '{printf("%02d%s\n",$7,$6)}')
if [ "$DATE" != $TODAY ]; then
if [ ! -d $DATE ] ; then
mkdir $DATE
fi
fi
done
This works if all files in the current directory have been created on this year, otherwise you have to lok at $8, which is either year or HH:MM, but these are details.

HTH
Entities are not to be multiplied beyond necessity - RTFM
Sandman!
Honored Contributor

Re: script to move files

Hi Lawrenzo,

Change the awk statement on the for-loop line so that it outputs a string w/o any embedded spaces. This also helps in compacting code since you don't need the sed statement anymore, which basically removes embedded spaces in $i, and make sure $TODAY contains the same date format as $i. Here's your code with the suggested modifications.

change -> awk '{print $6,$7}'
to -> awk '{print $6$7}'

remove -> DIR=$($i |sed s/' '//)

set -x
TODAY=$(date +"%b%d")
for i in `ls -l |awk '{print $6$7}' |uniq`
do
if [ $i != $TODAY ]; then
mkdir $i
fi
done
lawrenzo_1
Super Advisor

Re: script to move files

ok thats great to start my script off the main challenge I have here:

I want to list all files in the directory - if the directory doesn't exist create it, then move the relevant date files to the directory

if the directory does exist just move the files there.

TY
hello
James R. Ferguson
Acclaimed Contributor

Re: script to move files

Hi Lawrenzo:

I'd archive this way:

# touch -amt 07170000 /tmp/myref #...today
# mkdir /tmp/myarchive
# cd /tmp/myarchive
# find /path ! -newer /tmp/me|cpio -padmxv .

...This will find all files (and directories) that have been modified *before* today and copy then to your archive directory. A listing of the files copied is automatically generated. Modification and access times of the files are preserved.

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: script to move files

ok getting there now here is the function within the script

#!/bin/ksh

DIR=/usr/dump/JDE/data/out/ARCHIVE
LOGFILE=/sysadmin/logs/mvdata_JDE.log
REMOVE=/usr/dump/JDE/toberemoved
TODAY=$(date +"%d %b")



for a in `find $DIR -ctime 1 -type f`
do

month=`ls -l $a | awk -F' ' '{print $6$7}'`
file=`ls -l $a | awk -F' ' '{print $9}'`


if [ ! -d $month ] ; then

mkdir /usr/dump/JDE/data/out/ARCHIVE/$month
mv $file /usr/dump/JDE/data/out/ARCHIVE/$month

else
mv $file /usr/dump/JDE/data/out/ARCHIVE/$month
fi
done


this will create the directory and move the file.

The issue I have now is when the script runs the second day it still attempts to move all files that where moved the previous day.

how can i get find just to search the directory /usr/dump/JDE/data/out/ARCHIVE and not /usr/dump/JDE/data/out/ARCHIVE/day.mnth

thanks
hello