What is wrong with this cat?

New to Debian (Or Linux in general)? Ask your questions here!

What is wrong with this cat?

Postby pata » 2020-03-18 13:59

for i in *; do echo $i | cat -n ; done

1 0.png
1 1.png
1 2.png
1 3.png
1 4.png
1 5.png

Why is cat not numbering the files like below?

1 0.png
2 1.png
3 2.png
4 3.png
5 4.png
6 5.png

I know that 'ls * | cat -n' can achive the desire result. But I need the first method for a loop. Also to understand the behaviour of cat.
pata
 
Posts: 4
Joined: 2020-03-09 19:05

Re: What is wrong with this cat?

Postby Dai_trying » 2020-03-18 17:05

cat is getting one command at a time and so only has one file to display (meaning each one is the 1st in your example). You could probably change your loop to be more efficient.
Dai_trying
 
Posts: 881
Joined: 2016-01-07 12:25

Re: What is wrong with this cat?

Postby pendrachken » 2020-03-22 23:12

No idea why you feel like you have to do it in a loop but... your best bet that I can think of for something quick and dirty is to use a temp file then run cat on that to get the output you want:

Code: Select all
for i in *.png; do echo "$i" >> /MY/TEMPFILE/DIRECTORY/file.tmp; done && cat -n /MY/TEMPFILE/DIRECTORY/file.tmp


Conveniently Linux has a directory specifically for storing temp files, /tmp.

Here's the output I get from just dumping output to a temp file in the directory:

Code: Select all
 rm tmp && for i in *.png; do echo "$i" >> tmp; done && cat -n tmp
     1  0.png
     2  1.png
     3  2.png
     4  3.png
     5  4.png
     6  5.png
     7  6.png

fortune -o
Your love life will be... interesting.
:twisted: How did it know?

The U.S. uses the metric system too, we have tenths, hundredths and thousandths of inches :-P
pendrachken
 
Posts: 1363
Joined: 2007-03-04 21:10
Location: U.S.A. - WI.


Return to Beginners Questions

Who is online

Users browsing this forum: No registered users and 9 guests

fashionable