Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to number the ls output in unix?

Tags:

unix

awk

I am trying to write a file with format - "id file_absolute_path" which basically lists down all the files recursively in a folder and give an identifier to each file listed like 1,2,3,4.

I can get the absolute path of the files recursively using the following command:

ls -d -1 $PWD/**/*/*

However, I am unable to give an identifier from the output of the ls command. I am sure this can be done using awk, but can't seem to solve it.

like image 602
Snehal Avatar asked May 27 '10 19:05

Snehal


3 Answers

Pipe the output through cat -n.

like image 160
Matthew Slattery Avatar answered Nov 06 '22 03:11

Matthew Slattery


Assuming x is your command:

x | awk '{print NR, $0}'

will number the output lines

like image 36
viraptor Avatar answered Nov 06 '22 03:11

viraptor


Two posible commands:

ls -d -1 $PWD/**/*/* | cat -n
ls -d -1 $PWD/**/*/* | nl

nl puts numbers to file lines.

I hope this clarifies too.

like image 29
pampanet Avatar answered Nov 06 '22 05:11

pampanet