LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-20-2011, 11:28 PM   #1
Abunai
LQ Newbie
 
Registered: May 2011
Posts: 5

Rep: Reputation: Disabled
ls -l Output Date/Time Format Question


I am used to seeing the date/time displayed in the output of a long format "ls" command in three columns. Either "month day time" or "month day year".

However, I'm now working with SuSE Linux Enterprise Server 11.1, and the date/time displayed in the output of the "ls -l" command is contained in only two columns. "yyyy-mm-dd time". This is the same regardless of which shell I choose to run.

This is a problem because I have a script that I have to run, that I am not allowed to modify, that uses awk on the output of "ls -l" to grab the file name, which is normally in the ninth column of the ls output. If there are only two columns that make up the date/time, then the ls output is only eight columns wide, and my script fails.

Can anyone tell me if it is possible to modify the date/time format displayed in the output of "ls -l", and if so, how it is done?
 
Old 05-20-2011, 11:32 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005
Hello,

After looking at the man page, I came across the following:
Code:
--time=WORD
          with -l, show time as WORD instead of modification time: atime -u, access -u, use -u, ctime -c, or status -c; use specified time as sort key if --sort=time

--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
Would that be what you are talking about?

Cheers,

Josh
 
Old 05-20-2011, 11:35 PM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,145

Rep: Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496
I'm gussing the output of the following is different on the two systems
Code:
echo $LANG
This is one reason that it is often considered a bad idea to try to parse the output of 'ls -l' instead you should consider using the 'stat' command.
For example
Code:
stat -c %x *
HTH,

Evo2.
 
1 members found this post helpful.
Old 05-21-2011, 12:27 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
From info coreutils 'ls invocation' output: "You can specify the default value of the `--time-style' option with
the environment variable `TIME_STYLE'
".
 
Old 05-21-2011, 12:44 AM   #5
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005Reputation: 1005
Quote:
Originally Posted by catkin View Post
From info coreutils 'ls invocation' output: "You can specify the default value of the `--time-style' option with
the environment variable `TIME_STYLE'
".
Cool, thanks for that. I thought I was on the right track before... LOL
 
Old 05-21-2011, 01:20 AM   #6
Abunai
LQ Newbie
 
Registered: May 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
I'm gussing the output of the following is different on the two systems
Code:
echo $LANG
Thank you very much. You are correct. POSIX vs. en_US.UTF8.

I can change the value of LANG from the command line and see that this does change the format of the ls -l output. This is only good for the current terminal session.

How do I change this permanently?
 
Old 05-21-2011, 03:24 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by Abunai View Post
How do I change this permanently?
A wrapper script:
Code:
#! /bin/bash
export LANG=<whatever>
<path to the script you are not allowed to modify>
exit $?
 
1 members found this post helpful.
Old 05-21-2011, 08:41 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722
Quote:
Originally Posted by Abunai View Post
This is a problem because I have a script that I have to run, that I am not allowed to modify, that uses awk on the output of "ls -l" to grab the file name
I just wanted to say that parsing ls output with a script is a really, really bad idea and you should never do it. First of all, ls output is meant for human reading, which means it might not be consistent. Second, what if the filenames have spaces?

There are many other ways to get info about files. For example, you can use shell globs to list files, du to get the size of a file, and stat to get info about a file.

Last edited by MTK358; 05-22-2011 at 08:12 AM. Reason: typo
 
Old 05-22-2011, 05:48 AM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,145

Rep: Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496
Exclamation

Quote:
Originally Posted by Abunai View Post
How do I change this permanently?
Depends on your shell, but for bourne type shells you could put it in your .profile.

However, I want to reiterate that it is really not good practice to pars the output of 'ls'.

Cheers,

Evo2.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need date command to output in format 08-Dec-2010 Glenn D. Linux - Software 3 12-09-2010 11:13 AM
Changing the Time/Date format WilliaMITCHELL Ubuntu 3 09-28-2008 12:34 PM
convert date time format given in string suchi_s Programming 6 01-10-2005 04:06 AM
Bug report: wrong date/time format in last visited wipe LQ Suggestions & Feedback 0 07-29-2004 06:21 AM
loading data from a flat file with a date and time having a different format spyghost Programming 2 11-02-2003 04:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration