# Date format in a filename



## Anjly (Dec 5, 2003)

Hi,

I am trying to insert the date to a file name, by using the following date command: date +%Y_%m_%d to generate a tar file:

tar cvf filename_`date +%Y_%m_%d`.tar originalfiles*

The output of this date command for Dec.5/2003 will yield:
filename_2003_12_05.tar

The PROBLEM IS THIS: I need the filename to be of this format:
filename_2003_12_5.tar


How do I get rid of the zero padding for dates 1 to 9 ??

If I do: date +%Y_%m_%e , it simply replaces the zero padding with a space ... that doesn't help me.

Thanks so much for your help!!! I really can't figure it out....


----------



## Squashman (Apr 4, 2003)

Read the man page.

By default, date pads numeric fields with zeroes. GNU date recognizes
the following modifiers between % and a numeric directive.

- (hyphen) do not pad the field

date +%Y_%m_%-d
2003_12_5


----------



## Anjly (Dec 5, 2003)

Thanks for the quick reply!

I had tried the "hyphen" option as well, but unfortunately that is not working for me... When I try that, I get:

date +%Y_%m_%-d
2003_12_-d

Is there a way to fix this so that it recognizes "-d" ?

Thanks again...


----------



## Squashman (Apr 4, 2003)

I am using Red hat 9 and it works over and over again for me. Dont know what you are doing wrong.


----------



## Anjly (Dec 5, 2003)

I should ask my question more directly. What I am really trying to do is search through a list of files and if the date on it is from yesterday, I want to tar the file.

For example, there are some files that look like this:
done.txt__2003_12_8_123
done.txt__2003_12_8_456
done.txt__2003_12_8_789

What I was doing originally was:
alias yesterday="TZ=TZ+24 date +"%Y_%m_%d""
tar cvf done_`yesterday`.tar done.txt__`yesterday`_*

But this didn't work, obviously (because of reasons explained above).

So now I am thinking of doing something like this:
find done.txt__* -mtime 1 -exec <insert tar command here>

Can you please tell me if my new idea is correct? If it is, how do I execute the tar command?

Thanks so much again.


----------



## codejockey (Feb 11, 2002)

I think you're definitely on the right track. The find command writes output to stdout, so you could capture this output in a file and use the contents of that file as an argument to the tar command:

find directory-name -type f -mtime 1 > tar.list
tar cvf output-filename.tar `cat tar.list`

This has the advantage that you can review the contents of tar.list to verify them before creating the archive. If you'd rather do it all at once, you could use a command such as:

tar cvf output-filename.tar `find directory-name -type f -mtime 1`

You may also be able to use the -N (newer than) option to select the files tar archives; this would allow you to use just the tar command to select and archive files. Your tar command might look something like:

tar cvf output-filename.tar -N 2003-12-10 directory-name

Hope this helps.


----------



## Anjly (Dec 5, 2003)

Thanks so much!! 

I will let you know if I get it working.


----------



## Anjly (Dec 5, 2003)

I tried ur suggestions, and I keep getting an error: 'arg list too long'. 

> tar cvf done.tar `find done.txt* -type f -mtime 1`
ksh: /usr/bin/find: arg list too long
tar: Missing filenames

I'm assuming this means that there are too many files in the directory and it doesn't like to find so many files. How can I get around this?


----------



## codejockey (Feb 11, 2002)

You're more than welcome for any help I've provided. I think the first thing to do is to run your find command alone, so you can see the output (if any). You might want to capture the output in a file, so that you can review it. 

I have never seen the find command complain about too many arguments, but that only says that I haven't seen it. Generally, there are only two reasons for this error: (1) there really are too many arguments supplied to the command, and (2) the pathnames of the individual files are too long. Problem #1 can often be solved by using the xargs command; problem #2 can be solved by changing to the directory that contains all the files (cd directory-name) and then running the find command. Since you do not provide a directory name in your find command, find will use the current directory -- which suggests that you have already changed directory to the one with the data files before you run the find command (correct?). 

What version/revision of Linux are you running?

Hope this helps.


----------



## Anjly (Dec 5, 2003)

I tried implemeting your suggestions. Running the find command alone, I get the same error. I ran the following commands after changing to the appropriate directory.

The following two commands worked nicely. Output was printed to the screen as expected:

>find done.txt__2003_12_21_* -type f 
>find done.txt__2003_12_21_* -type f -mtime 1

This command did not produce any output, got an error:
>find done.txt__* -type f -mtime 1
ksh: /usr/bin/find: arg list too long

The output of the third command should be the same as the output of the first two. I don't understand why the third one doesn't work if the first two do work. 

Is the mtime option trying to display ALL files that are dated as yesterday AND older, or does it display all files that are dated as yesterday?


----------



## codejockey (Feb 11, 2002)

I suspect the problem is that find has an internal limit on the total length of all files satisfying the search conditions. The "total length" is a product of the number of files and the length of each filename. In your first two commands, the number of files is restricted to those that match the pattern: done.txt__2003_12_21_*. Your third command selects all files matching the pattern done.txt__*, which presumably includes files from all dates -- a much larger number than just those from 12/21/2003. Given the larger number of files (and the relatively long filename of each file), your third command is more likely to exceed the internal limitations of the find command than either of your first two commands.

If you know the date you want to use in your selection, you might try another approach such as: tar cvf done.tar `ls done.txt__2003_12_21_*` which avoids the find command altogether. From what you've described, however, you could also use the find command: tar cvf done.tar `find done.txt__2003_12_21_*`

The real question boils down to whether you can specify the value of "yesterday" ahead of time, or whether you want your script to use file modification time (or some other measure) to determine "yesterday" (possibly dangerous).

Hope this helps.


----------

