-2

I'm running ls -l which brings the following output:

-rw-r----- 1 webadm webgrp 168 Jul 19 16:00 SYNCHRO_20120719_1600.csv

Is there a way to have just get the last two fields? i.e:

16:00 SYNCHRO_20120719_1600.csv

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Mercer
  • 9,736
  • 30
  • 105
  • 170

3 Answers3

1

try this:

 ls -l | awk '{print $(NF-1), " ", $NF}'

It will display the next-to-last and last fields on the line, which is the time and the name of the directory entry and is not dependent on a specific number for a field.

May create problems with names that contains blanks however.

See this page regarding use of 'ls' for processing, provided by @CharlesDuffy which somewhat undermines the approach above :-) .. so the quick solution presented ought to be viewed in that context.

Levon
  • 138,105
  • 33
  • 200
  • 191
  • Not just names with blank spaces -- how `ls` munges names in general (including ones with other forms of non-printable characters) isn't well-defined across implementations. Much better to use shell globbing to get the names, and something else (which _could_ be ls) to read their times. See http://mywiki.wooledge.org/ParsingLs – Charles Duffy Jul 19 '12 at 14:28
  • @CharlesDuffy Thanks for the feedback, useful information and so noted. I agree, this is an awkward (and quite possibly "iffy") solution. – Levon Jul 19 '12 at 14:31
1

This should do what you want:

ls -l | awk '{print $8" "$9}'
Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
1

On a system with GNU date:

for f in *; do
    printf '%s %s\n' "$(date +%H:%M -r "$f")" "$f"
done

This will work correctly with filenames with spaces -- or even filenames with newlines, though it won't be possible to distinguish between filename-embedded newlines and record-separated ones. (Use null delimiters if this is important).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441