Link to home
Start Free TrialLog in
Avatar of Pothina_info
Pothina_info

asked on

How to append sysdate to .CSV file inunix and remove them

How to append sysdate to file name in UNIX shell scripting  and remove the file .

Eg: file_name.CSV

Requirement: file_name_sysdate.CSV
File_name_yyyy-mm-dd.CSV
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

mv file_name.CSV file_name_$(date "+%Y-%m-%d").CSV

If you plan to put the above directly (not inside a script) into the crontab please be aware that you'll have to escape the percent signs with a backslash ("\%").
Hi,

While executing any command at any operating systems follow:
          Use full path of the command (since alias or functions can overwrite the actions it is performing)
Hence here goes related commands:
1. using back tick
/bin/mv -i file_name.CSV file_name_''`/bin/date "+%Y-%m-%d"`''.CSV
2. As per message from woolmilkporc including full path:
/bin/mv -i file_name.CSV file_name_$(/bin/date "+%Y-%m-%d").CSV
Handling exception to overwrite existing file"
1. /bin/mv -i file_name.CSV file_name_''`/bin/date "+%Y-%m-%d"`''.CSV
2. handling file name using hour/min/second/nanoseconds....
#!/bin/ksh
CurFile=''`/bin/ls -ltr file_name*.CSV | /usr/bin/tail -1 | /bin/awk '{ print $N
F}'`''
/bin/sleep 1
NextFile="file_name_"''`/bin/date "+%Y-%m-%d_%h_%M_%S__%N"`''".CSV"
if test -f $CurFile
then
        echo File $CurFile is present
        echo hence moving in following format
        echo file_name_YYY-mm-dd_hr_min_sec_Nanosec.CSV
        /bin/mv -i $CurFile \
        $NextFile
        echo if
else
        CurFile=file_name.CSV
        if test -f $CurFile
        then
                /bin/mv -i $CurFile \
                $NextFile
        else
                echo $$CurFile file not found
        fi
fi

Open in new window

Avatar of Pothina_info
Pothina_info

ASKER

Problem solved
 thanks for your support and help
Hi Pothina_info,

welcome for the same. You can provide assisted/accepted solution for the same.

Regards,
Murugesan
Hi Pothina_info,

Previous comment posted on on 2016-05-20.
Waiting for accepted or assisted solutions.

cmd.exe /c ECHO %DATE% | find "2016"
25-05-2016


Regards,
Murugesan
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.