Enumerate ls -l output

Hi, I'm using normally ls -l to get the content of a directoy.

But, it's possible to enumerate the ls output ? (like put the line number at the begining of each line)

If it wasn't possible (I don't hope so ...), there's a way to get the line number the line had after doing a grep:
ls -l /directory | grep word ----> and then get the line number it had in the ls output ...

Thanks in advance

Try:

j=1; for i in *; do  echo $(( j++ )) $i; done
j=1; for i in *; do  echo $(( j++ )) $(ls -l $i); done

Try awk

ls -l | awk '/word/{print ++c, $0}'
ls -l | pr -Tn

Why would you use grep instead of giving ls a pattern?

Hi, the program nl enumerates lines so You can pipe the output through it, like

ls -l | nl

/Lakris

PS If You use

ls -l | nl -v 0

it will actually enumerate the actual files if that is desirable.

or..

# ls -1 | grep -n .
1:dir_7851
2:dir_license_7851_20081017
3:bin
4:cdrom
5:dev
6:devices
7:test
8:esm
9:etc
10:export
11:home
12:hosts
13:javahost.pid
14:java_install
15:kernel
16:lib
17:lost+found
18:media
19:mnt
20:net
21:netegrity
22:opt
23:platform
24:proc
25:root
26:sbin
27:system
28:tmp
29:u01
30:unused
31:usr
32:var
33:vol
34:xfn

or..

ls -l | awk '{print NR,$0}'

or even cat has this functionality:

#  ls -l | cat -n

:slight_smile:

Great !

Your help is really valuable. The thing is that I have a list of directories like this:

lrwxrwxrwx 1 root root 39 2009-08-30 22:44 000-default -> /path
lrwxrwxrwx 1 root root 39 2009-08-30 22:44 001-default1 -> /path
lrwxrwxrwx 1 root root 39 2009-08-30 22:44 002-default2 -> /path

And I'm writing a script, that in case of removing one of that links, for example the 001 one, the other links below must decrease it's name (in this example, if I'm removing the 001-default1, the 002-default2 must move its name to 001-default2, and so on). And I thought I would do it with some kind of loop within the line numbers.

So, thank you!