I'm learning to work in unix using Terminal on a Mac and I'd like to set the default listing for the timestamp to display in 12 hour time, using AM and PM, instead of 24 hour time (ie: 1:00 PM, not 13:00). I'm somewhat familiar with setting preferences, so to speak, using the ~/.bashrc file, where I've set a couple of aliases, including one with the options for my preferred ls display. I've looked at the man pages and elsewhere online, but I haven't had any luck finding what I'm looking for time-wise though. Any suggestions would be appreciated.

  • You mean you want to set the time format on a UNIX global level so that all commands and output displays the time as per your format of choice? – ismail Feb 18 '12 at 14:49
  • Yes, that sums up what I'd like to do. – BennyB23 Feb 19 '12 at 17:13
  • I wish I knew a way to format ls datetimes (I'd rather see ISO-8601). Apple's BSD manual for ls seems to offer nothing for formatting datetimes, other than changing timezone and sorting by datetime rather than alpha. developer.apple.com/library/mac/documentation/Darwin/Reference/… – 2540625 Aug 12 '14 at 5:27

Using GNU ls, on Linux, there is a --time-style flag you can use to specify one of a few pre-defined time types, or specifying a completely custom +FORMAT style like the date command accepts. However, BSD ls, which Apple ships, does not support this flag.

I've seen similar discrepancies in commands like date and tar as well, all rooted in the differences between the GNU and BSD variants.

| improve this answer | |

The Mac OS terminal uses a bash shell. The prompt is controlled by the PS1 environment variable described more here. Try this for example.

export PS1='\t$ '

It will give you a prompt looking something like this

| improve this answer | |
  • This didn't accomplish what I was trying to do, however it tipped me off to how I could customize the prompt (I'm not sure if that's what it's called) which is something I wanted to do, but wasn't going to hassle about. I came up with a partial answer by way of a video tutorial and through a concentrated look at the man pages for strftime. I'll start a new post to try to make my rephrasing of the question more useful to the community. Thanks~B – BennyB23 Dec 17 '11 at 20:47
  • Actually, I changed my mind; this is probably the appropriate place to post, so I'll continue with a bit of extra space...<br/> </br/> In the tutorial I mentioned above, there was information about configuring – BennyB23 Dec 17 '11 at 21:01
  • Oi... this is getting to be a pain. I overshot my time for editing the last mistake (over 5 minutes). I'm not really getting the formatting on here and my patience for fussing with it at the moment has worn out. I'll give it a rest for now... – BennyB23 Dec 17 '11 at 21:25

man date

Check out the "+format" option. You could create a shell alias to display the date using a custom format.

If you're referring to the time format used by 'ls', I don't believe there is any way to change that.

| improve this answer | |
  • Thanks, you may be right about ls. I was able to customize the history format using the HISTTIMEFORMAT environment variable. HISTTIMEFORMAT='%b %d %I:%M:%S %p ' formats the date to show as: Jan 17 12:00:00 PM. I opted not to include the year for practical purposes, since it's just history, but I don't see any sort of straightforward way to format the string for time globally. Probably something with TZ, but that looks a bit more complicated than I can figure out at the moment. ~B – BennyB23 Jan 17 '12 at 5:33

to get 12:00 AM/PM use

  export PS='\@'

I hope this helps.

P.S. Welcome to S.O. Please remember to accept the answer that best solves your problem, if any, by pressing the checkmark sign, http://i.imgur.com/uqJeW.png. When you see good Q&A, vote them up by using the gray triangles, http://i.imgur.com/kygEP.png. Note that 'giving' reputation points to others does not mean a deduction to your reputation points.

| improve this answer | |
  • This didn't seem to do anything (at least not that I'm aware of) but thanks for the suggestion – BennyB23 Dec 17 '11 at 20:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .