Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > ls date format changed 10.3 to 10.4

ls date format changed 10.3 to 10.4
Thread Tools
Dedicated MacNNer
Join Date: Nov 2005
Status: Offline
Reply With Quote
Jan 21, 2007, 08:36 PM
 
This is a minor fix for anyone who uses scripts to capture the output from ls and use that between Mac OS X (10.3 and 10.4). The problem was that the default date format changed from day/month to month/day. I vaguely remember a similar change in the past.

I run a home-made sync between a 10.3 PowerMac and a 10.4 MacPro, and this relies on the output of ls -l. I used sed to parse the columns of the outupt and then rearrange the columns. I created pipe-separated output for my script, but I will present a simpler version here:

ls -lT|sed -e 's/^\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)/\1\2\3\4\5\7\6\8/'


The sed command contains many copies of the string: \([^\ ]*[\ ]*\)
The first caret (^) means to find this string at the start of the line. The [^\ ]* is a regular expression (regexp) to find "not a space" one or more times. The [\ ]* finds one or more spaces in a row (the space is preceded by a slash). These two expressions are inside "slash-prefixed parentheses." Each repetition of the stuff in the parentheses can be output at the end of the statement where \1 represents the first original column and \7 represents the seventh original column. You can rearrange or delete the numbers on the right to alter the output. One side effect is that the column alignment might look funny. Note that I reversed 7 and 6.
     
drp
Fresh-Faced Recruit
Join Date: Jul 2006
Status: Offline
Reply With Quote
Jan 22, 2007, 12:19 PM
 
The order that ls and other command line utilities format dates and numbers is based on your current locale settings.

If you type "locale" at the command prompt, it'll show you your current settings. As and example, a typical system will be unset and default to the "C" type. Similar to this:

LANG=
LC_COLLATE="C"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL="C"

So an ls will use a format similar to:

-rwxr-xr-x 1 root wheel 1068844 Jul 1 2006 bash

Changing the locale/language will change the date to the correct local format. As an example, Great Britain uses the inverted format:

export LANG=en_GB.UTF-8

Will change the format to:

-rwxr-xr-x 1 root wheel 1068844 1 Jul 2006 bash

As a result, if your scripts are relying on the output from system commands, it's best to set the locale environment variables in the script so you're guaranteed what the output will look like.
     
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Jan 22, 2007, 07:53 PM
 
Originally Posted by rehoot View Post
but I will present a simpler version here:

ls -lT|sed -e 's/^\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)\([^\ ]*[\ ]*\)/\1\2\3\4\5\7\6\8/'
Goodgawd man! What are you trying to do? (Surely "finding spaces" isn't the ultimate goal).

If that's the "simple" version, I guarantee there's probably an even simpler version that will work.

(try awk maybe... or stat -f even)
(Last edited by Hal Itosis; Jan 22, 2007 at 08:02 PM. )
-HI-
     
rehoot  (op)
Dedicated MacNNer
Join Date: Nov 2005
Status: Offline
Reply With Quote
Jan 22, 2007, 11:35 PM
 
thx.

The "locale" program works on 10.4 but not 10.3. I could change 10.4 to have the date format that I don't want but that matches the format from 10.3. The ls program in 10.3 does not seem to respond to the international settings in System Preference, and I don't see a locale program. I realize that my duct-tape solution is fragile.
     
rehoot  (op)
Dedicated MacNNer
Join Date: Nov 2005
Status: Offline
Reply With Quote
Jan 22, 2007, 11:46 PM
 
Originally Posted by Hal Itosis View Post
Goodgawd man! What are you trying to do? (Surely "finding spaces" isn't the ultimate goal).

If that's the "simple" version, I guarantee there's probably an even simpler version that will work.

(try awk maybe... or stat -f even)
Yes, it is an ugly and fragile script. The ugly sed thing is as much of a UNIX oddity as it is a solution to anything. I use awk for similarly crazy scripts. The space delimited output from ls is reliable (within a given version of the OS) becuse I stop parsing before I get to the file name, which is on the far right when you use the -T option. My main home-made archiving system feeds data into MySQL, and that has been working well.
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:26 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2