5

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.

Snehal
  • 7,266
  • 2
  • 32
  • 42
  • 2
    ls -d $PWD/**/*/* seems really, really fragile. Too many files and the glob will fail. Pathnames with special characters will cause a failure. A directory structure more than 3 deep won't be captured. Better to use find. – William Pursell May 31 '10 at 18:37

6 Answers6

20

Pipe the output through cat -n.

Matthew Slattery
  • 45,290
  • 8
  • 103
  • 119
9

Assuming x is your command:

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

will number the output lines

viraptor
  • 33,322
  • 10
  • 107
  • 191
7

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.

pampanet
  • 131
  • 6
6

There is a tool named nl for that.

ls -la | nl

Sebastian
  • 603
  • 1
  • 6
  • 6
2

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
  • 74,869
  • 16
  • 134
  • 187
0

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

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50