views:

117

answers:

6

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.

+1  A: 

If you do ls -i, you'll get the inode number which is great as an id.

The only potential issue with using inodes is if you folder spans multiple file systems as an inode is only guaranteed to be unique within a file system.

R Samuel Klatchko
Presumably, the OP wants to create a numbered menu or something similar. I don't think inode numbers are suitable for that use.
Dennis Williamson
+7  A: 

Pipe the output through cat -n.

Matthew Slattery
+1 for a new `cat` flag I was not aware of. Cool!
Ukko
+1 Thanks for answer. Both your answer and Viraptor answer works.
Snehal
+4  A: 

Assuming x is your command:

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

will number the output lines

viraptor
+1 Thanks for answer. Both your answer and Mattew Slattery answer works
Snehal
A: 

ls -d -1 $PWD/*//* | awk ' {x = x + 1} {print x " " $0} '

Amardeep
+3  A: 

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.

pabiagioli
A: 

There is a tool named nl for that.

ls -la | nl

Sebastian