How to count ls output in unix?

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

I can get the absolute file path recursively using the following command:

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

However, I cannot give the identifier from the output of the ls command. I am sure that this can be done with awk, but it seems it cannot solve it.

+3
source share
6 answers

Pull the exit through cat -n.

+13
source

Assuming xyour command is:

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

will indicate output lines

+6
source

:

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

nl .

, .

+5

If you follow ls -i, you will get an inode number that matches perfectly as id.

The only potential problem with using inodes is that if your folder spans multiple file systems like inodes, only uniqueness in the file system is guaranteed.

+1
source

There is a tool called nl for this.

ls -la | P

+1
source

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

0
source

Source: https://habr.com/ru/post/1747431/


All Articles