I have thousands of *csv files in a certain subdirectory.

There's a simple in line executable which I use to work with these files, which I pipe into a new file:

executable file1.csv standard.csv > output_file1.csv

I would like to create a for loop to do this not just for file1.csv, but for all files in that subdirectory.

I would try something like this:

for file in *.csv
    # run executable on "$file" and output 
    executable $file standard.csv > output

I think this will work, but how do I name each output output_ + $file +.csv?

  • My answer has been edited out of recognition, so I have deleted it – Bruno9779 Mar 7 '17 at 16:50
  • The entire point of SE is that the community can improve answers by editing them for form, content, edge cases, and reliability. And the comment you ascribe to me was not of my origin in the first place. – DopeGhoti Mar 7 '17 at 16:57
  • @Bruno9779 I saw nothing wrong with your original answer – ShanZhengYang Mar 7 '17 at 17:06

Credit to Bruno9779 for the original draft of this answer. Not sure why it was self-deleted, as it was a pretty good answer:

You have pretty much done it yourself:

if cd "$destinationDir"; then
    for file in *.csv;  do
        # run executable on "$file" and output 
        executable "$file" standard.csv > "${destinationDir}/output_${file}.csv"
    echo "Unable to change to working directory."

Just remember to quote filenames with variables.

| improve this answer | |
  • I didn't see anything wrong with @Bruno9779's answer – ShanZhengYang Mar 7 '17 at 17:06
  • 1
    Generally speaking, no, but there were small issues that were immediately apparent. For example, he or she or other admonished to quote filenames, but left the first instance of $file unquoted. Those sorts of things are why I edited the answer - it was a pretty good answer, but had room for improvement, which is the entire point of this site. Since the original author saw fit to delete the answer entirely, I opted to add a little sanity-checking and the like as I reposted it -- with credit to its origin, I would like to point out. – DopeGhoti Mar 7 '17 at 17:09
  • Yes, I was improving the answer, when a "realized" changed into a "Relize" tipped me off and I just deleted everything. – Bruno9779 Mar 7 '17 at 17:12
  • "Unable to to" might well be a typo. – dhag Mar 7 '17 at 18:13
  • Well spotted; fixed. – DopeGhoti Mar 7 '17 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.