-1

I have written a command but it displays only the total lines. I want total lines with numbering and their name. If I remove wc -l it only shows the numbering with their name. I want all this command to be execute at ones.

alias abc='ls -a | nl | wc -l'

  • I don't understand what you mean by "total lines with numbering and their name" ... can you edit your post and add some (mockup?) sample output? – tink Jan 21 '21 at 03:38
  • 1
    You can use a temp file, e.g. `alias abc='ls -al | tee /tmp/tmpdl | nl; wc -l < /tmp/tmpdl && rm /tmp/tmpdl'`. Not pretty, but it works `:)` Though with `nl` outputting the total number for the last line -- it is unclear why you want the `wc -l` output as well -- but it is doable. – David C. Rankin Jan 21 '21 at 03:39

1 Answers1

0

Awk is another option for this:

alias abc="ls -a | awk '{ printf cnt++\"\t\"\$0\"\n\" } END { print cnt }'"

Pipe ls -a to awk and increment a count. Print count along with each line of output and then at the end, print cnt on a separate line.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18