0

I have a script and i'm using this to find and list files that end with .txt in the tmp folder

find /tmp -type f \( -name '*.txt' \) |cut -c14-

The output is something like this:

xpto.txt
abc.txt
xyz.txt

But i want to add an index at the beggining of each line

1 - xpto.txt   
2 - abc.txt
3 - xyz.txt

Can anyone help me? i'm still learning bash, started 1 month ago.

2
  • 2
    man nl Commented Nov 19, 2013 at 20:33
  • bashref Commented Nov 19, 2013 at 20:34

2 Answers 2

2

You could do:

find /tmp -type f \( -name '*.txt' \) |cut -c14- | nl

But the find command does not always works very friendly with pipes in my experience.

so you could do also this:

cd /tmp ; ls *.txt | nl

or this (for a different output layout)

cd /tmp ; ls *.txt | grep -n ""
Sign up to request clarification or add additional context in comments.

2 Comments

But find will also list files that are in a subdirectory, while ls won't.
@pfnuesel, that is a very correct remark... to let it work with find however,the cut command also needs some tinkering to let it work. Also he will probably need xargs to process the output of find further. No harm in trying different approaches :-)
2

Try piping command output to following awk

awk -v OFS="-" '{print NR, $0}'

perl variant

perl -lne 'print $.,"-",$_'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.