-6

I have

urls1.txt
urls2.txt
urls3.txt 

And so on.

How can I loop all files in this directory then loop each line of these files?

  • 5
    What would you want to do to each line of each file? It's very seldom necessary to read in lines from a file in a shell script. It's more common to process each file through awk or sed or some such tool. See e.g. unix.stackexchange.com/questions/169716 – Kusalananda Jan 14 at 15:20
  • 2
    This seems simple enough to do even notwithstanding the truth of @Kusalananda's comment. What have you tried? How did it not work as expected or intended? – DopeGhoti Jan 14 at 15:21
1

To answer your question as it is written you can do the following:

#!/bin/bash

for file in /path/to/files/*.txt; do
    # This will loop through each .txt file in the directory
    # setting the full file path to $file
    # I recommend adding the .txt if you know all your files
    # will have that extension otherwise the following is fine:
    # for file in /path/to/files/*; do
    while read -r line; do
        # This will loop through each line of the current file
        # and set the full line to the $line variable
        command "$line"
    done < "$file"
done

Explanations are in comments within the code

  • IT doesnt work ./test.sh: line 16: syntax error near unexpected token while' ./test.sh: line 16: while read -r line; do' – Born vs. Me Jan 14 at 15:53
  • @Bornvs.Me: sorry I forgot a do in the for loop, see if that fixes it. – Jesse_b Jan 14 at 15:55
  • 2
    It'll trim whitespace. – Kusalananda Jan 14 at 15:57

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.