List files appended with an order number

Is there a quick way to list all the files from a location having an appended order number?

something like:

ls -a

generates the result:

1 file a

2 file b

...

100 file whatever

Thanks.

P.S. I need this because I am exporting a list to a file, and I have to analyse those files, and it would help me to be able to identify the files.


Solution 1:

From the terminal use the following command:

ls -a | cat -b

Solution 2:

yes there is. just pipe nl to the end of the your command:

In this case you should write in this way:

ls -a | nl

and the result is :

  1. foo
  2. bar
  3. baz

nl refers to number line of files.

also look at here :) http://www.sitepoint.com/15-little-known-unix-commands/?

Solution 3:

Karel's answer is fine for most cases but it will break in the unlikely case where your file names contain newlines. For a more robust approach use stat instead:

stat --printf "%N\n" * | cat -n

or findand awhile` loop:

find . -maxdepth 1 -printf '"%f"\0' | 
while IFS=$'\0' read -r -d $'\0' file; do 
  let c++; printf "%d\t%s\n" $c "$file"; 
done