9

Is it possible with either find or ls to see, in a list of results, if any given entry is the nth result, what n is?

So, for example, pretend ls -l returns:

total 0 -rw-rw---- 1 bigdog bigdog 0 Jan 3 17:13 a -rw-rw---- 1 bigdog bigdog 0 Jan 3 17:13 b -rw-rw---- 1 bigdog bigdog 0 Jan 3 17:13 c 

Is there a way to get ls to return 1,2,3 for files a,b,c respectively?

Or, find . -type f returns:

./a ./b ./c 

Any way to get something like:

1 ./a 2 ./b 3 ./c 

I am aware of ls | wc -l and the like, but that is not what I'm looking for.

5
  • eh, same solution, but somewhat different problem Commented Jan 4, 2016 at 1:28
  • Not if you see the problem for what it is: "Run some command and get line numbers in output" Commented Jan 4, 2016 at 1:29
  • but how would i go about finding that thread given my question, ie how did you find it? Commented Jan 4, 2016 at 1:30
  • Googled line number output site:unix.stackexchange.com (it was the last result on the first page.) The Stack Exchange site search is notoriously bad, Googling with a site restriction often yields better results. Commented Jan 4, 2016 at 1:35
  • eh ok, i'll accept that Commented Jan 4, 2016 at 1:46

2 Answers 2

18

You can use the following command:

ls | nl 
1
  • Thx. Can't upvote but what I'm looking for. Commented Jan 4, 2016 at 0:26
6

"cat -n" will add line numbers. E.g.: ls | cat -n

1
  • what i'm looking for, thx. probably could find this but my google query was awkward, i think. Commented Jan 4, 2016 at 0:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.