bash loop all files in directory, then loop lines of each file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












-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?










share|improve this question



















  • 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















-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?










share|improve this question



















  • 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













-6












-6








-6


0






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?










share|improve this question
















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?







linux bash shell newlines






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 14 at 19:49









peterh

4,456103057




4,456103057










asked Jan 14 at 15:16









Born vs. MeBorn vs. Me

46




46







  • 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












  • 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







5




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






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




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





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 Answer
1






active

oldest

votes


















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






share|improve this answer

























  • Thank you! VERY MUCH

    – Born vs. Me
    Jan 14 at 15:37











  • 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








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494444%2fbash-loop-all-files-in-directory-then-loop-lines-of-each-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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






share|improve this answer

























  • Thank you! VERY MUCH

    – Born vs. Me
    Jan 14 at 15:37











  • 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















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






share|improve this answer

























  • Thank you! VERY MUCH

    – Born vs. Me
    Jan 14 at 15:37











  • 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













1












1








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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 14 at 20:27









Ralf

3678




3678










answered Jan 14 at 15:34









Jesse_bJesse_b

12.4k23066




12.4k23066












  • Thank you! VERY MUCH

    – Born vs. Me
    Jan 14 at 15:37











  • 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

















  • Thank you! VERY MUCH

    – Born vs. Me
    Jan 14 at 15:37











  • 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
















Thank you! VERY MUCH

– Born vs. Me
Jan 14 at 15:37





Thank you! VERY MUCH

– Born vs. Me
Jan 14 at 15:37













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





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





@Bornvs.Me: sorry I forgot a do in the for loop, see if that fixes it.

– Jesse_b
Jan 14 at 15:55




2




2





It'll trim whitespace.

– Kusalananda
Jan 14 at 15:57





It'll trim whitespace.

– Kusalananda
Jan 14 at 15:57

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494444%2fbash-loop-all-files-in-directory-then-loop-lines-of-each-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay