File timestamping issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File timestamping issue
# 1  
Old 08-20-2012
File timestamping issue

Hello,

I am working on moving a data file to archive dir from the processing directory using the following lines in my FTP script. Sometimes the mv command does not work as the timestamp is is changed by seconds as the current date in the following code is changed.

I have tried to use ts='date +%C%y%m%d%I%M%S` variable to append to the file name but that does not work.

Here is the code that is relevant:
Code:
 
files=$(ls $intfiles.pgp 2> /dev/null | wc -l)
if [ $files -ne 0 ]
then
echo "Creating a copy of the file receivied in the Archive dir"
for a in $mvfiles
do
cp "$a" "$a.`date +%C%y%m%d%I%M%S`"
mv "$a.`date +%C%y%m%d%I%M%S`" $ARCHIVE_DIR
done
else
echo "The data files were not generated."
exit
fi

Moderator's Comments:
Mod Comment code tags please

I am working on Linux ver. 2.6.18. Thanks!

Last edited by jim mcnamara; 08-20-2012 at 11:10 AM..
# 2  
Old 08-20-2012
can you explain the below line

Sometimes the mv command does not work as the timestamp is is changed by seconds as the current date in the following code is changed.


Also, what is in the $mvfiles ?
# 3  
Old 08-20-2012
mvfiles are the file I am trying to move to archive dir.

mvfiles=path to my current files that have been generated by a program
$ARCHIVE_DIR=path to the archive dir where the file(s) are supposed to be moved so there is a timestamped copy.


If the timestamped file created is SomeDataFile.TXT.20120820103149 by the cp command the mv command might be looking for SomeDataFile.TXT.20120820103209 as the system date from the 'date +%C%y%m%d%I%M%S` might have changed between the cp and mv command.

Last edited by vidyab35; 08-20-2012 at 12:40 PM..
# 4  
Old 08-20-2012
If your problem is that the two date commands return slightly different results, then the most obvious solution is to only run it once and store the value.

Regards,
Alister
# 5  
Old 08-20-2012
try the below code b/w the do and done

Code:
currenttime=$(date +%C%y%m%d%I%M%S)
newfilename="${a}.${currenttime}"
cp "${a}" "${newfilename}"
mv "${newfilename}" $ARCHIVE_DIR

instead of copying to the same directory and move it to archive directory, you can use the cp command directly.

Code:
newfilename="${a}.$(date +%C%y%m%d%I%M%S)"
cp "${a}" "${ARCHIVE_DIR}/${newfilename}"

# 6  
Old 08-20-2012
itkamaraj,

wow! That worked perfectly.

I have tried the direct copy before with the cp to archive/newfilename in the second set of example code, but it does not seem to work on linux.

Thanks again!
# 7  
Old 08-20-2012
Can u post the code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Listing file issue

I have the three following files available in the directory. But the job should be able to read only the first two files. Could any one help me in writing command to list only the first two files and omit the last file. I used ls -1 LSM_REP* > final.lst. It is copying all the three files. But I... (5 Replies)
Discussion started by: Ram Nukavarapu
5 Replies

2. Cybersecurity

UNIX files timestamping - Need experts opinion as testimonial

Hi I am requesting your help to obtain opinions and testimonials in order to be be able to make my own opinion since I am definetly not a unix expert. Say we have a UNIX server. On this server there is a specific directory let us call it "DIR" A security incident have been reported... (10 Replies)
Discussion started by: docflied
10 Replies

3. Shell Programming and Scripting

Need assistance with a file issue and a terminal issue

Hello everyone, I'm in need of some assistance. I'm currently enrolled in an introductory UNIX shell programming course and, well halfway through the semester, we are receiving our first actual assignment. I've somewhat realized now that I've fallen behind, and I'm working to get caught up, but for... (1 Reply)
Discussion started by: MrMagoo22
1 Replies

4. Shell Programming and Scripting

Issue with sudoers file.

Hi All, I am new to sudoers file. I am asked to troubleshoot why a particular user (alandhi) is not able to run a script as a different user(scmtg). I have the following line in my sudoers file and the user's name added to the group. User_Alias QA_USERS = alandhi, testuser1, qauser3 ... (3 Replies)
Discussion started by: Tuxidow
3 Replies

5. IP Networking

Issue File Extension is file Size

I am currently experiencing the file size being added to the file extension when transfering information from Command Line Client to a UNIX server. Does anyone know why this is happening and how do I stop the file size being added to the file extension. Example: football.pqt.11108... (1 Reply)
Discussion started by: Skeeterrock
1 Replies

6. Shell Programming and Scripting

Unzipping a file in Solaris - Issue with xls file

Hi, I have an excel file generated by system in windows. I am zipping it, transfering to unix and unzipping there. But i'm getting below output while unzipping. $ /usr/bin/unzip -a 123.zip -d . Archive: 123.zip inflating: ./123/Index.xls When i copy this unzipped xls file to... (0 Replies)
Discussion started by: ajaykumarb
0 Replies

7. Shell Programming and Scripting

UNIX File handling -Issue in reading a file

I have been doing automation of daily check activity for a server, i have been using sqls to retrive the data and while loop for reading the data from the file for several activities. BUT i got a show stopper the below one.. where the data is getting store in $temp_file, but not being read by while... (1 Reply)
Discussion started by: KuldeepSinghTCS
1 Replies

8. Shell Programming and Scripting

Issue with a file that contains CRLF

I have a nawk that reads in a log file and outputs a file that matches my search. IFS=" " while read record do `echo $record | nawk 'BEGIN { FS=" " } { type_record=substr($0, 1, 1); if (... (14 Replies)
Discussion started by: Pablo_beezo
14 Replies

9. Shell Programming and Scripting

issue with ^B characters in the file

Hello- I have a csv file with japan email ids as the last column(pipe delimited file). One of the email ids has some special characters which are not displayed on my UNIX box, however when ftped to local(Windows machine) I can see the email id terminated with a square box instead of pipe. ... (2 Replies)
Discussion started by: pasupuleti81
2 Replies

10. Shell Programming and Scripting

Performance issue in UNIX while generating .dat file from large text file

Hello Gurus, We are facing some performance issue in UNIX. If someone had faced such kind of issue in past please provide your suggestions on this . Problem Definition: /Few of load processes of our Finance Application are facing issue in UNIX when they uses a shell script having below... (19 Replies)
Discussion started by: KRAMA
19 Replies
Login or Register to Ask a Question