Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

What is wrong with this cat?

New to Debian (Or Linux in general)? Ask your questions here!
Post Reply
Message
Author
pata
Posts: 5
Joined: 2020-03-09 19:05

What is wrong with this cat?

#1 Post by pata »

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.

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: What is wrong with this cat?

#2 Post by Dai_trying »

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.

pendrachken
Posts: 1394
Joined: 2007-03-04 21:10
Location: U.S.A. - WI.

Re: What is wrong with this cat?

#3 Post by pendrachken »

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

Post Reply