LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Formatting output of ls (https://www.linuxquestions.org/questions/linux-newbie-8/formatting-output-of-ls-193418/)

doodar 06-14-2004 01:26 PM

Formatting output of ls
 
I want to generate a listing of files & subfolders that includes only the filename, size and date... I figured the easiest way would be similar to this, but I don't know how to remove the other stuff from the output:

ls -lR

david_ross 06-14-2004 01:39 PM

Welcome to LQ.

Try using:
ls -lR | grep "^-"

You could also do it using find:
find ./ -type f -printf "%a %s %f\n"

doodar 06-14-2004 02:05 PM

Thanks for the help, but what i meant by "only include ..." was that i didn't want stuff like attributes, owner and group name. I *do* still want the names of folders and their sizes. Also, I should mention that I am using the darwin kernel on a Mac (no printf?). I think the solution would involve piping to awk, and I can't quite get it right. Spaces in filenames are also difficult.

david_ross 06-14-2004 02:35 PM

I see what you mean now. What about:
ls -lR | awk {'print $5" "$6" "$7" "$8" "$9'}

Tinkster 06-14-2004 02:45 PM

Heh ... I thought of posting that, but it would
omit the leading line with the directory name
for the files underlying and I couldn't be bothered
to build an if-statement ;)
And of course the problem of spaces in filenames.
Your approach would only cover two segments.
cut won't work easily, either, since we don't know
for sure that there's no files with a HUGE size ...
Maybe with a ls -lhR ? :)


Cheers,
Tink

doodar 06-14-2004 02:51 PM

Quote:

Originally posted by Tinkster
but it would omit the leading line with the directory name for the files underlying [...] And of course the problem of spaces in filenames.
Exactly. Awk would really need an if statement to do this properly. It took me forever looking through its man page to figure out the syntax just to print that out. If anyone can tell me how to include the necessary conditional, I'd appreciate it. For now, back to man pages...

Tinkster 06-14-2004 03:00 PM

Code:

ls -lR | awk '{if ( $1 ~ /\./) print $1; else { print $5 " " $8 " "  $9}}'
Build up on that ... the problem of several spaces still exists, though



Cheers,
Tink

doodar 06-14-2004 03:09 PM

Yeah, also the output is uglified when spaces between items replace tabs. :(

david_ross 06-14-2004 03:13 PM

Try just stripping off the first 35 (I think) characters from each line

doodar 06-14-2004 03:18 PM

Quote:

Originally posted by david_ross
Try just stripping off the first 35 (I think) characters from each line
I assume you mean piping to colrm... This would work if columns were all the same size, but they aren't. Also, you lose the folder name that preceeds subfolder listings.

doodar 06-14-2004 03:24 PM

The suggestion of ls -lR | awk '{if ( $1 ~ /\./) print $1; else { print $5 " " $8 " " $9}}' seems pretty close. Just need to change the spaces between items to tabs and figure out filenames with spaces. For the spaced filenames part, is there a way to have awk print out the rest of the line... so that in place of $9, you have $9 through the end?

david_ross 06-14-2004 04:01 PM

Perhaps it would be simper just to write a simple script to do it?

newls.sh:
Code:

#!/bin/bash

for item in `find $1`;do
 if [ -d $item ];then
  echo $item
 elif [ -f $item ];then
  echo -e "`stat --format \"%s\" $item` \\033[10G `stat --format \"%y\" reg.pl | cut -d. -f1`  $item"
 fi
done

Run as:
newls.sh /path/to/dir

Tinkster 06-14-2004 04:09 PM

Quote:

Originally posted by doodar
Yeah, also the output is uglified when spaces between items replace tabs. :(
http://www.gnu.org/software/gawk/man...o/gawk.html.gz

Look at printf ;)

Or this approach ;)

Code:

ls -lR | awk 'BEGIN  { FIELDWIDTHS = "10 5 7 8 12 11 6 128" } {if ( $1 ~ /\./) print $1; else { print $5 " " $8 }}'


Cheers,
Tink

doodar 06-14-2004 04:10 PM

lots and lots of "./newls.sh: line 1: stat: command not found"

doodar 06-14-2004 04:14 PM

Quote:

Originally posted by Tinkster
ls -lR | awk 'BEGIN { FIELDWIDTHS = "10 5 7 8 12 11 6 128" } {if ( $1 ~ /\./) print $1; else { print $5 " " $8 }}'
I fields aren't fixed width :( is there a switch to make it that way?


All times are GMT -5. The time now is 08:29 AM.