Date to number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date to number
# 1  
Old 11-21-2013
Date to number

Hi,

When running ls -larth command, we get the output as follows.
Code:
-rwxrwx---1 user root 15K Nov 21 10:21 abc.txt

Now, i want to convert the date "Nov 21" into a number, in the form of YYYYMMDD.

How can i do that, do i need to put values as in Jan=1, Feb=2, March =3 and so on... or there is some simple and short way to do so..

Please help.

Thanks,
J

Last edited by Don Cragun; 11-21-2013 at 04:05 AM.. Reason: Change Bold tags to CODE tags and add ICODE tags.
# 2  
Old 11-21-2013
Code:
ls -l --time-style=long-iso

# 3  
Old 11-21-2013
How will this help??

what exactly is this style think..

Can you explain..!!!
# 4  
Old 11-21-2013
On Linux
Code:
ls --help

produces a help screen
Code:
      --time-style=STYLE     with -l, show times using style STYLE:
                             full-iso, long-iso, iso, locale, +FORMAT.
                             FORMAT is interpreted like `date'; if FORMAT is
                             FORMAT1<newline>FORMAT2, FORMAT1 applies to
                             non-recent files and FORMAT2 to recent files;
                             if STYLE is prefixed with `posix-', STYLE
                             takes effect only outside the POSIX locale

Using this information you make the output of ls -l show the data in LOTS of ways
You want a number to sort on. So, you can get the file modification time in epoch seconds -number of seconds since Jan 1, 1970.
Code:
ls -l --time-style=+%s

Or a fudged up number made of yearmonthday e.g., 20131121 which is human readable
Code:
ls -l --time-style=+%Y%m%d

which is what you wanted -IF you are on Linux. Aside: you should tell us your system andshell when you post, it makes a huge difference.
# 5  
Old 11-21-2013
Quote:
Originally Posted by jim mcnamara
On Linux
Code:
ls --help

produces a help screen
Code:
      --time-style=STYLE     with -l, show times using style STYLE:
                             full-iso, long-iso, iso, locale, +FORMAT.
                             FORMAT is interpreted like `date'; if FORMAT is
                             FORMAT1<newline>FORMAT2, FORMAT1 applies to
                             non-recent files and FORMAT2 to recent files;
                             if STYLE is prefixed with `posix-', STYLE
                             takes effect only outside the POSIX locale

Using this information you make the output of ls -l show the data in LOTS of ways
You want a number to sort on. So, you can get the file modification time in epoch seconds -number of seconds since Jan 1, 1970.
Code:
ls -l --time-style=+%s

Or a fudged up number made of yearmonthday e.g., 20131121 which is human readable
Code:
ls -l --time-style=+%Y%m%d

which is what you wanted -IF you are on Linux. Aside: you should tell us your system andshell when you post, it makes a huge difference.
I tried this command in my server. it gave me error. my server is sun4v sparc SUNW,SPARC-Enterprise-T5220.

Thanks

Last edited by ken6503; 11-21-2013 at 02:28 PM..
# 6  
Old 11-22-2013
Hi,

I am using Solaris server.. its not working on linux..
What can be the soloution for solaris..


THanks,
J
# 7  
Old 11-22-2013
You need the GNU coretuils version of ls. Solaris DOES NOT HAVE that by default.
You would have to install coreutils. Try http://www.opencsw.org to download it.

On linux please post the output of
Code:
# example --

$ ls --version
ls (GNU coreutils) 8.15
Packaged by Cygwin (8.15-1)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

If it a recent version of coreutils then something else is wrong
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Derive date and its Volume number

Hi All, I have a list of date number (YYYYMMDD) and its own number in a file. Here is the example 20170320 D100001 D100002 D100003 20170321 D200001 D200002 D200004 D200005 20170322 D200005 D200006 D200007 (4 Replies)
Discussion started by: ckwan123
4 Replies

2. Shell Programming and Scripting

Filename with sequence number and date

Hi Unix Gurus, I have a requirement to create a filename in the following format: file.out_1_mmddyyyyhhmiss (0 to 3 hrs) file.out_2_mmddyyyyhhmiss (4 to 7 hrs) file.out_3_mmddyyyyhhmiss (8 to 11 hrs) I tried, 1)touch file.out_`date '+%m%d%Y%H%M%S'` 2)expr `date +%H` / 4 + 1 ... (2 Replies)
Discussion started by: bi.infa
2 Replies

3. Shell Programming and Scripting

Week number from a date.

Hi, How can we get the week number from any paricular date. lets say date is 20120404 (YYYYMMDD) then how to get the week number? date +%W --- Thic command gives the week number for current date only. Thanks. (13 Replies)
Discussion started by: 46019
13 Replies

4. Shell Programming and Scripting

Know the number of the week for a date

Hi, I tried to find the solution on the forum without success. datecalc from Perderabo doesn't solve my problem. I would like to know how to do the same thing that date +%U but for a specific date. For example: 2011 08 27 => 39 Thinks a lot (8 Replies)
Discussion started by: Castelior
8 Replies

5. Shell Programming and Scripting

Number of days between the current date and user defined date

I am trying to find out the number of days between the current date and user defined date. I took reference from here for the date2jd() function. Modified the function according to my requirement. But its not working properly. Original code from here is working fine. #!/bin/sh... (1 Reply)
Discussion started by: hiten.r.chauhan
1 Replies

6. UNIX for Dummies Questions & Answers

Number of files in a directory modified on a date

Hi How to list all the files in a directory that are modified on a particular date? Also need to know the count,i.e number of files modified on a particular date. Thanks Ashok (1 Reply)
Discussion started by: ashok.k
1 Replies

7. Shell Programming and Scripting

Sorting string with date and number

Hi, We have files coming in the system and we want to sort it in ascending order with date and sequence. The file pattern are inbound_crp_date_sequence.xml example we have file as below: inbound_crp_20100422_10.xml inbound_crp_20100422_2.xml inbound_crp_20100422_3.xml... (2 Replies)
Discussion started by: sreejitnair123
2 Replies

8. Shell Programming and Scripting

unique number for a date

Hello, In korn-shell, how can I do to have an unique number for a date done. I want to use it to have the number of days between two dates. Thanks in advance. (4 Replies)
Discussion started by: madmat
4 Replies

9. Shell Programming and Scripting

calcuate the week number of a given date

Hi All, can any one help me fix the error in this - i am still a novice in shell programming. I got this code after some googling now the code works with all the dates( as much as i know) except for 08 th and 09th of every month. can any one of you please help me fix this issue? Thanks in... (11 Replies)
Discussion started by: ahmedwaseem2000
11 Replies

10. Shell Programming and Scripting

Date & NUmber Validation Query

Hi Do you have any pointers how to validate numbers (not to contain alphabets and special characters) and date(MM/DD/YYYY) format. I used following regular expression to validate integer, which is not working in the default shell: nodigits="$(echo $testvalue | sed 's/]//g')" ... (4 Replies)
Discussion started by: alok_jax
4 Replies
Login or Register to Ask a Question