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.
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?
-
- Posts: 1100
- Joined: 2016-01-07 12:25
- Has thanked: 5 times
- Been thanked: 16 times
Re: What is wrong with this cat?
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.
-
- Posts: 1394
- Joined: 2007-03-04 21:10
- Location: U.S.A. - WI.
Re: What is wrong with this cat?
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:
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
for i in *.png; do echo "$i" >> /MY/TEMPFILE/DIRECTORY/file.tmp; done && cat -n /MY/TEMPFILE/DIRECTORY/file.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.
How did it know?
The U.S. uses the metric system too, we have tenths, hundredths and thousandths of inches
Your love life will be... interesting.
How did it know?
The U.S. uses the metric system too, we have tenths, hundredths and thousandths of inches