Files with date and time stamp

Hi Folks,

Need a clarification on files with date and time stamp.

Here is my requirement. There is a file created everyday with the following format "file.txt.YYYYMMDDHHMMSS".

Now i need to check for this file and if it is available then i need to do some task to the file.

I tried "file.txt.'date %Y%m%s'*" to check this, but this doesn't seem to work as the file gets created at different time everyday.

Can someone please help me out with the syntax to look for the file if it is available?

Thanks

if test -f file.txt.$(date +%Y%m%d)*; then
  echo Found!
fi

Example:

$ touch file.txt.$(date +%Y%m%d%H%M%S)                        

$ ls file.txt*
file.txt.20130925030644

$ if test -f file.txt.$(date +%Y%m%d)*; then
>  echo Found!
>fi
Found!
1 Like

You should be OK using: file.txt.$(date +%Y%m%d)*

1 Like

Hi Folks,

Thanks much for the reply !!!

Here is my other request.

If the file with the date and time stamp is found, i need to rename the file with out changing the date and time.

For example, file.txt.20130925065543 should be renamed to file_first.txt.20130925065543

Please help me out with this.

Thanks

$ echo $file
file.txt.20130925030644
$ mv "$file" "${file/./_first.}"

Hi Folks,

To use this command mv "$file" "${file/./_first.}" i need to assign the file name to file variable first right ?

I am in the process of automating the step and I will not know what time the file will be created and it can be at anytime of the day. In this case how do i change the name of the file?

Thanks

Assuming you have only one file created everyday

file=file.txt.$(date +%Y%m%d)*
test -f $file && mv "$file" "${file/./_first.}"

--ahamed

1 Like