LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 06-14-2004, 01:26 PM   #1
doodar
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Rep: Reputation: 0
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
 
Old 06-14-2004, 01:39 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Welcome to LQ.

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

You could also do it using find:
find ./ -type f -printf "%a %s %f\n"
 
Old 06-14-2004, 02:05 PM   #3
doodar
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Original Poster
Rep: Reputation: 0
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.
 
Old 06-14-2004, 02:35 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
I see what you mean now. What about:
ls -lR | awk {'print $5" "$6" "$7" "$8" "$9'}
 
Old 06-14-2004, 02:45 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
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
 
Old 06-14-2004, 02:51 PM   #6
doodar
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Original Poster
Rep: Reputation: 0
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...
 
Old 06-14-2004, 03:00 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
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
 
Old 06-14-2004, 03:09 PM   #8
doodar
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Original Poster
Rep: Reputation: 0
Yeah, also the output is uglified when spaces between items replace tabs.
 
Old 06-14-2004, 03:13 PM   #9
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Try just stripping off the first 35 (I think) characters from each line
 
Old 06-14-2004, 03:18 PM   #10
doodar
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Original Poster
Rep: Reputation: 0
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.
 
Old 06-14-2004, 03:24 PM   #11
doodar
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Original Poster
Rep: Reputation: 0
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?
 
Old 06-14-2004, 04:01 PM   #12
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
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
 
Old 06-14-2004, 04:09 PM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
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
 
Old 06-14-2004, 04:10 PM   #14
doodar
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Original Poster
Rep: Reputation: 0
lots and lots of "./newls.sh: line 1: stat: command not found"
 
Old 06-14-2004, 04:14 PM   #15
doodar
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Original Poster
Rep: Reputation: 0
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?
 
  


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CLI....ls (or similar command)...formatting output Basslord1124 Linux - Newbie 2 02-27-2005 05:13 PM
Via AC'97 5.1 Optical Output or Audigy 4.1 Output Nza Fedora 3 06-01-2004 07:49 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM
Bash Select Function: Formatting Output mooreted Linux - General 2 03-28-2004 06:26 AM
need hlp formatting output from ls test* Evan P. Programming 1 11-06-2003 08:40 PM


All times are GMT -5. The time now is 02:15 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration