-2

hi i have my UNIX file date in particular format 2017-02-01 i want to convert it in integer value like 20170201. What can i do get this output.My UNIX box is(SunOS 5.10).I tried to check below command to see what they output.But i am not getting anything.Can anyone help?

bash-3.2$ date +'%s'
%s
bash-3.2$ date +"%s"
%s
bash-3.2$ date +%s
%s

When i try date -d "Filename" +%Y%m%d option it error out saying:-

date: bad conversion
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
Randomguy
  • 192
  • 1
  • 11

3 Answers3

2

You can get the date you like with this line of code (bash):

date +"%Y%m%d"

you can use it as a filename like this:

_now=$(date +"%Y%m%d")
_file="/tmp/$_now.ext"

then use $_file for your filename

Oliver
  • 893
  • 6
  • 17
  • @Oliver it is working fine but it only gives me current date value as integer value.How to make it work for a particular file??? – Randomguy Feb 07 '17 at 08:45
  • @ghoti it only gives me current system date in integer value . I want to do it for a particular file.??? – Randomguy Feb 07 '17 at 08:47
  • @Oliver can you explain me how it works? Like i have a file called "crontest.sh" now how can i use this in above code to get its creation date as INTEGER value.Thanks – Randomguy Feb 07 '17 at 09:57
  • @TusharSharma could you explain a little bit more, what you want to archive? Do you want to create a file by your crontest.sh? Perhaps an sql-dump or something like this? – Oliver Feb 07 '17 at 10:00
  • @Oliver file is already there with name for ex: "abc.txt" ignore the above example.I have this file creation time and date.I want to convert its creation date to integer value like ( this 2017-02-01 to 20170201). – Randomguy Feb 07 '17 at 10:05
  • @Oliver in your solution (date +"%Y%m%d") where should i specify the file name to get its creation date in integer value?? Currently if i execute only this(date +"%Y%m%d") then it shows current date in integer value ex:(2017-02-07 to 20170207).How to make it work for my file "abc.txt". – Randomguy Feb 07 '17 at 10:21
  • ok, now I think I have understood what you want. And I have bad news for you. This is not possible in nearly all kind of unix systems. You can check that in your sun-os by using the stat command (e.g. stat -c '%w' abc.txt), but this does not work in most LINUX-systems and it depends on your filesystem. Check also the POSIX - Standard: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html – Oliver Feb 07 '17 at 11:26
  • @Oliver i used substring to get year,month,date in different variable and then concatenate them that solved the issue. Even though not a good practice.BTW thanks a lot for your help and support. – Randomguy Feb 07 '17 at 12:05
  • @Oliver, while the `stat` command only exists in more modern OSs, the system call it wraps around, [`fstat(2)`](https://www.freebsd.org/cgi/man.cgi?query=fstat&sektion=2) is part of POSIX.1, so it's available in virtually *every* unix. Have a look at my answer for how to leverage the system call in a tiny C program. – ghoti Feb 08 '17 at 16:25
  • @ghoti You are right, but the problem was not a missing function. It is the fact that most Linux-Systems don't use the "birthdate"-value of a file. Thats why stat or fstat does not give back the creation-date of a file. – Oliver Feb 10 '17 at 09:27
0

SunOS 5.10 (Solaris 10) is pretty old. There are some things it doesn't do.

In particular, I note that its date command relies on strftime() for formatting, and according to the man page, Solaris 10's strftime does not support %s. Which explains the results in the test you did in your question.

If you really just want a YYYYMMDD format, the following should work:

$ date '+%Y%m%d'

If you're looking for an epoch second, then you might have to use some other tool. Nawk, for example, should be installed on your system:

$ nawk 'BEGIN{print srand}'

You can man nawk and search for srand to see why this works.

Alternately, you could use Perl, if you've installed it:

$ perl -e 'print time . "\n";'

These are strategies to get the current epoch second, but your initial date command is the correct way to get the date formatted the way you suggested.


Based on other comments, it also appears you're looking to get the timestamp of certain files. That's not something the date command will do. In other operating systems, like Linux or FreeBSD, you'd use the stat command, which does not appear as a separate shell command in Solaris 10.

But you can do it yourself in a pretty short C program that uses the fstat(2) system call. While it may be beyond the scope required for this question, you can probably compile this using /usr/sfw/bin/gcc:

#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>

int main(int argc, char **argv)
{
    struct tm info;
    char buf[80];
    struct stat fileStat;
    time_t epoch;

    if(argc != 2)
        return 1;

    int file=0;
    if((file=open(argv[1],O_RDONLY)) < -1)
        return 1;

    if(fstat(file,&fileStat) < 0)
        return 1;

    info = *localtime( &fileStat.st_mtime );

    epoch = mktime(&info);
    printf("%s:%ld\n", argv[1], (long) epoch );

    return 0;
}

For example:

$ gcc -o filedate filedate.c
$ ./filedate /bin/ls
/bin/ls:1309814136
$ perl -e 'use POSIX; print POSIX::strftime("%F %T\n", gmtime(1309814136));'
2011-07-04 21:15:36
$ ls -l /bin/ls
-r-xr-xr-x   1 root     bin        18700 Jul  4  2011 /bin/ls
$
ghoti
  • 45,319
  • 8
  • 65
  • 104
  • how can this command (date '+%Y%m%d') can be used with a file? – Randomguy Feb 08 '17 at 15:15
  • Can you explain what you mean by "with a file"? Are you trying to programatically determine the date of a file and store it in a variable? Or are you trying to make a filename that is based on the current date? Or something else? – ghoti Feb 08 '17 at 16:20
0

SOLVED IT WITH THIS COMMAND-:

browserDate="2016-11-21"
dateConversion="${browserDate//'-'}"
Randomguy
  • 192
  • 1
  • 11