0

Can someone with more Linux knowledge answer this correctly for me. On our web server, we host and run ALOT of web scripts. we control these via Datestamp files, So the script is not over ran, or ran more than once.

A lot of files are all 0 KB. I wanted to know if there is a quick way in Linux to locate the files and update them.

I have located the files using:

find /var/www/vhosts/DOMAINNAME.co.uk/httpdocs -name "datestamp.*" -type f -empty

I have a long list of files, Can i update these with a simple datestamp format: i.e. 20150923114046

Kenster
  • 23,465
  • 21
  • 80
  • 106
Dave Hamilton
  • 675
  • 1
  • 12
  • 23
  • What do you mean by "update these with a simple datestamp"? Rename them? – choroba Sep 23 '15 at 11:19
  • These files contain nothing (Due to a failed running of the script) I want to update these log file with this datestamp 20150923114046 Or more accurate, if possible, to update the file and write in the datestamp, the datestamp can be strung using the files last modified date (Indicating when it last ran / corrupted itself) So in simple, i want to update all *.log files with This datestamp: 20150923114046 Or We can build the datestamp from the Files last modified date and time. – Dave Hamilton Sep 23 '15 at 11:23
  • Correct https://www.dropbox.com/s/w1pcsszinyru8x9/correctdatestamp.log?dl=0 I Cannot upload incorrect as its 0kb = Contains nothing – Dave Hamilton Sep 23 '15 at 11:27

1 Answers1

1

You can use the -exec option of find:

find /var/www/vhosts/DOMAINNAME.co.uk/httpdocs -name "datestamp.*" -type f -empty \
    -exec bash -c 'echo 20150923114046 > {}' \;

To get the timestamp dynamically, use date:

bash -c 'echo $(date +%Y%m%d%H%M%S) > {}'

To use the last modified timestamp, use the -r option:

bash -c 'echo $(date +%Y%m%d%H%M%S -r {}) > {}'
choroba
  • 231,213
  • 25
  • 204
  • 289
  • This is perfect thank you very much, Can you if possible, do the exact same, but generate the datestamp from the files last modified date and time in the same format? This would be a more robust way to recover from a server crash, and ensure all script run from when they last crashed. When the server crashes, it fails to write in the datestamp to the file, so it updates the files modified time and date, but never adds in the datestamp. its not required, and your method has worked and got me the 50 scripts up and running again within seconds Thanks – Dave Hamilton Sep 23 '15 at 11:30
  • @DaveHamilton: Check the update. If you've already changed the files, though, it's too late :-) – choroba Sep 23 '15 at 11:37