Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6843357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:14:07+00:00 2026-05-27T00:14:07+00:00

I have a set of files, each containing a single (integer) number, which is

  • 0

I have a set of files, each containing a single (integer) number, which is the number of files in the directory of the same name (without the .txt suffix) – the result of a wc on each of the directories.

I would like to sum the numbers in the files. I’ve tried:

i=0; 
find -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | while read j; do i=$i+`cat $j.txt`; done
echo $i

But the answer is 0. If I simply echo the output of cat:

i=0; find -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | while read j; do echo `cat $j.txt`; done

The values are there:

1313
1528
13465
22258
7262
6162
...

Presumably I have to cast the output of cat somehow?

[EDIT]

I did find my own solution in the end:

i=0; 
for j in `find -mindepth 1 -maxdepth 1 -type d -printf '%f\n'`; do 
    expr $((i+=$(cat $j.txt))); 
done; 

28000
30250
...
...
647185
649607

but the accepted answer is neater as it doesn’t output along the way

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:14:08+00:00Added an answer on May 27, 2026 at 12:14 am

    The way you’re summing the output of cat should work. However, you’re getting 0 because your while loop is running in a subshell and so the variable that stores the sum goes out of scope once the loop ends. For details, see BashFAQ/024.

    Here’s one way to solve it, using process substitution (instead of pipes):

    SUM=0
    while read V; do
        let SUM="SUM+V" 
    done < <(find -mindepth 1 -maxdepth 1 -type d -exec cat "{}.txt" \;)
    

    Note that I’ve taken the liberty of changing the find/cat/sum operations, but your approach should work fine as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of files in a single directory named: OneThing-x.extension OneThing-y.extension OneThing-z.extension
I have a txt file and each line has a set of random strings,
I have a large set of files, some of which contain special characters in
I have a set of zip files which contains several ieee-be encoded binary and
I have a set of binary configuration files with three versions each -- an
I have a directory full of files containing records like: FAKE ORGANIZATION 799 S
I have a data structure made of Jobs each containing a set of Tasks.
Context When iterating over a set of Rdata files (each containing a character vector
I have a set of files. The set of files is read-only off a
I have a set of html files that I want to modify by replacing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.