You are not logged in.

#1 2010-12-27 22:46:17

karabaja4
Member
From: Croatia
Registered: 2008-09-14
Posts: 1,001
Website

Help with bash - ls command

Hi,

I would like to write up a command (this is the default version):

ls -o -h --color=auto --group-directories-first

that turns default output:

total 60K
drwxr-xr-x 3 karabaja4 4.0K Dec 27 22:06 Downloads
drwxr-xr-x 6 karabaja4 4.0K Dec 24 11:42 projects
drwxr-xr-x 3 karabaja4 4.0K Dec  4 22:30 scripts
drwxr-xr-x 2 karabaja4 4.0K Dec 21 00:43 shared
drwxr-xr-x 2 karabaja4  20K Dec 12 00:15 shots
drwxr-xr-x 2 karabaja4 4.0K Oct 28 00:50 vmshared
drwxr-xr-x 3 karabaja4 4.0K Oct 19 14:29 vmware
drwxr-xr-x 3 karabaja4  12K Dec 24 16:29 wallpapers

into this

total 60K
Downloads   3 karabaja4 4.0K Dec 27 22:06 drwxr-xr-x
projects    6 karabaja4 4.0K Dec 24 11:42 drwxr-xr-x
scripts     3 karabaja4 4.0K Dec  4 22:30 drwxr-xr-x
shared      2 karabaja4 4.0K Dec 21 00:43 drwxr-xr-x
shots       2 karabaja4  20K Dec 12 00:15 drwxr-xr-x
vmshared    2 karabaja4 4.0K Oct 28 00:50 drwxr-xr-x
vmware      3 karabaja4 4.0K Oct 19 14:29 drwxr-xr-x
wallpapers  3 karabaja4  12K Dec 24 16:29 drwxr-xr-x

is it possible? I know very little sed, and this look troublesome because of tabs and different string sizes...

Last edited by karabaja4 (2010-12-27 22:48:06)

Offline

#2 2010-12-27 22:50:17

Awebb
Member
Registered: 2010-05-06
Posts: 6,662

Re: Help with bash - ls command

Then you should add a little awk, which is perfect when dealing with rows. Google might give you more information, try the search term "awk sort rows".

Offline

#3 2010-12-27 22:51:43

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Help with bash - ls command

Do not do this by modifying ls's output. Use GNU stat(1) or the printf option to find(1).

Offline

#4 2010-12-27 23:06:29

skunktrader
Member
From: Brisbane, Australia
Registered: 2010-02-14
Posts: 1,580

Re: Help with bash - ls command

ls -o -h --color=auto --group-directories-first | awk '{if (NF == 2) print; else print sprintf("%-20s",$8),$2,$3,$4,$5,$6,$7,$1;}'

Specifying column widths need more love

Last edited by skunktrader (2010-12-27 23:07:08)

Offline

#5 2010-12-27 23:07:01

Ashren
Member
From: Denmark
Registered: 2007-06-13
Posts: 1,229
Website

Re: Help with bash - ls command

Here's a way:

 ls -o -h --color=auto --group-directories-first | awk -F " " '{ print $8" "$2" "$3" "$4" "$5" "$6" "$7" "$1}' | column -t

Last edited by Ashren (2010-12-27 23:10:32)

Offline

#6 2010-12-27 23:22:21

karabaja4
Member
From: Croatia
Registered: 2008-09-14
Posts: 1,001
Website

Re: Help with bash - ls command

LOL, 30 seconds apart smile Thanks!

Last edited by karabaja4 (2010-12-27 23:22:42)

Offline

#7 2010-12-28 00:03:52

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Help with bash - ls command

Following up on falconindy's point: http://mywiki.wooledge.org/ParsingLs

Offline

Board footer

Powered by FluxBB