Script to rename file that was generated today and which starts with date

hello,

can someone please suggest a script to rename a file that was generated today and filename that being generated daily starts with date, its a xml file.

here is example.

[root@hostname sftpdata]# find . -type f -mtime -1
./20130529_4995733057260357019.xml
[root@hostname sftpdata]#

this finename should be renamed to this format.
renamedfeed_20130529.xml

Thanks,

Here is one way of doing it:

c=$( find . -name "*.xml" -type f -mtime -1 | tee tmp | wc -l )

if [ $c -eq 1 ]
then
        file=$( cat tmp )
        mv "$file" "renamedfeed_$(date +%Y%m%d).xml"
fi

In case you are not aware, -mtime -1 is not constrained to "a file that was generated today." It will match anything that's less than 24 hours old, which usually includes a portion of yesterday.

If the file must have been created on the current calendar day, perhaps you'd be better off using the date command to generate a filename pattern argument for the -name primary.

Regards,
Alister

Thanks for your reply.

I just tried this format, but not working, please suggest here

$ ls -lrt
total 168
-rw-r----- 1 z504783 ctlab_admin 166593 May 29 10:17 20130529_4995733057260357019.xml
$
$ file =$(`find . -name `date +%Y%m%d`_*.xml`) ; mv $file renamedfeed_`date +%Y%m%d`.xml
find: missing argument to `-name'
-bash: _*.xml: command not found
=20130529: ERROR: cannot open `=20130529' (No such file or directory)
mv: missing destination file operand after `renamedfeed_20130529.xml'
Try `mv --help' for more information.
$

There should be no space after file , and you don't need both $( ... ) and ` ... `.

date=$(date +%Y%m%d)
file=$(find . -name "$date"_*.xml)
mv "$file" renamedfeed_"$date".xml
1 Like

I changed the format of the scirpt, and added to crontab its not working, I see this error on the cron logs

someone please suggest.

#crontab -e
30 16 * * * mv $(find /data/sftp -name `date +%Y%m%d`_*.xml) Partner_`date +%Y_%m_%d.%H-%M-%S`.xml
#
#tail -f /var/log/cron
Jul 26 16:30:01 crond[20857]: (root) CMD (mv $(find /data/sftp -name `date +)
Jul 26 16:30:01 crond[20858]: (root) CMD (/usr/lib64/sa/sa1 1 1)

Thanks,

In a crontab, percent signs must be escaped.