Is there a limit on the number of words in bash command for file in words

0 votes

 am encountering strange behavior when executing the following bash script:

#! /bash/bin
dirs=$(ls .)
for dir in $dirs ; do
    files=$(ls $dir)
    for file in $files ; do
        line=$(head -n -1 $dir/$file)
        echo $line
    done
done

Instead of the echo of $line, I am getting the results of a ls / command, followed by the echo of $line, followed by the contents of $files. My guess is that I am exceeding some limit in bash: I have 171 directories, each with 500 files. When I add the line:

echo ${#line}

to the script, I get the right answer (68); but the echo of $line fills my terminal window. What can I do now?

May 27, 2022 in Linux Administration by Rahul
• 3,380 points
662 views

1 answer to this question.

0 votes
Anyway, you need to print everything except the last line of all grinds one registry even out down the ongoing level? As remarks propose, never parse the ls yield, it is for people, not really for computerization. Use special cases, all things being equal:

for f in */*; do
  head - n - 1 "$f"
done
Anyway there are two expected issues:

There are no such records. Of course there will be one cycle of the circle with variable f set to strict worth */*. You can stay away from this by empowering the nullglob choice before the circle and handicapping it again after the circle:

shopt - s nullglob
for f in */*; do
  head - n - 1 "$f"
done
shopt - u nullglob
Some "records" are false documents (joins, directories...). In any case, we can test this prior to executing head:

shopt - s nullglob
for f in */*; do
  on the off chance that [[ - f "$f" ]];
    head - n - 1 "$f"
  fi
done
shopt - u nullglob
As indicated by @JohnKugelman there is more straightforward than a circle:

head - n - 1 */*
In any case, sadly there is no basic method for settling the subsequent issue referenced previously. Thus, on the off chance that you are not 100 percent sure that all records in */* are truly documents, the circle with a test is more secure...

... except if you utilize a committed utility like find:

find . - mindepth 2 - maxdepth 2 - type f - executive head - n - 1 {} \;
The - type f test holds just genuine documents. The - executive activity takes one contention which is an order to execute on each tracked down document, where {} represents the way of the ongoing record. This order should be ended by a got away from semicolon (\;).

Note: assuming you have great many records and you print their full satisfied however the last line, it isn't is business as usual that it "fills your terminal window". In the event what you need is to print just the main line of these documents, supplant head - n - 1 by head - n1.
answered May 27, 2022 by Korak
• 5,820 points

Related Questions In Linux Administration

0 votes
0 answers

Is there a way to determine the amount of free video RAM in Linux?

We believe that we are running out ...READ MORE

Apr 26, 2022 in Linux Administration by Edureka
• 13,690 points
426 views
0 votes
2 answers

how to loop through the content of a file using bash

#!/bin/bash for i in  `cat peptides.txt` do echo $i done READ MORE

answered Sep 5, 2020 in Linux Administration by Prakash K. Aithal
3,018 views
0 votes
1 answer

How to check permissions of a file in the Linux system?

Hi@akhtar, If you prefer using the command line, ...READ MORE

answered Aug 13, 2020 in Linux Administration by MD
• 95,460 points
1,775 views
0 votes
1 answer

What is the linux command to find Memory and CPU usage in percent for last 30 days?

Try the following: Cat proc/meminfo top top -i less /proc/memin ...READ MORE

answered Oct 14, 2020 in Linux Administration by Kim
2,532 views
0 votes
1 answer

How to find the first field from a file in Bash Shell?

Hi@akhtar, You can extract text from a file. ...READ MORE

answered Oct 20, 2020 in Linux Administration by MD
• 95,460 points
1,093 views
0 votes
0 answers

Linux Command for getting a dump of hex values bytes from Pcap File

I require a Linux command to display ...READ MORE

Apr 13, 2022 in Linux Administration by Rahul
• 9,680 points
971 views
0 votes
0 answers

Linux Command for getting a dump of hex values bytes from Pcap File

I require a linux command to display ...READ MORE

Apr 20, 2022 in Linux Administration by Edureka
• 13,690 points
636 views
0 votes
0 answers

How can I convert a series of images to a PDF from the command line on linux?

I have a scanning server I wrote ...READ MORE

Apr 21, 2022 in Linux Administration by Edureka
• 13,690 points
619 views
0 votes
1 answer

Is there a better way to run a command N times in bash?

In the event that your reach has ...READ MORE

answered Jun 13, 2022 in Linux Administration by Korak
• 5,820 points
727 views
0 votes
1 answer

Is there a better way to run a command N times in bash?

In the event that your reach has ...READ MORE

answered Jun 16, 2022 in Linux Administration by Korak
• 5,820 points
538 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP