bash loop all files in directory, then loop lines of each file
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
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 throughawk
orsed
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
add a comment |
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
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
linux bash shell newlines
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 throughawk
orsed
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
add a comment |
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 throughawk
orsed
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
add a comment |
1 Answer
1
active
oldest
votes
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
Thank you! VERY MUCH
– Born vs. Me
Jan 14 at 15:37
IT doesnt work ./test.sh: line 16: syntax error near unexpected tokenwhile' ./test.sh: line 16:
while read -r line; do'
– Born vs. Me
Jan 14 at 15:53
@Bornvs.Me: sorry I forgot ado
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Thank you! VERY MUCH
– Born vs. Me
Jan 14 at 15:37
IT doesnt work ./test.sh: line 16: syntax error near unexpected tokenwhile' ./test.sh: line 16:
while read -r line; do'
– Born vs. Me
Jan 14 at 15:53
@Bornvs.Me: sorry I forgot ado
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
add a comment |
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
Thank you! VERY MUCH
– Born vs. Me
Jan 14 at 15:37
IT doesnt work ./test.sh: line 16: syntax error near unexpected tokenwhile' ./test.sh: line 16:
while read -r line; do'
– Born vs. Me
Jan 14 at 15:53
@Bornvs.Me: sorry I forgot ado
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
add a comment |
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
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
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 tokenwhile' ./test.sh: line 16:
while read -r line; do'
– Born vs. Me
Jan 14 at 15:53
@Bornvs.Me: sorry I forgot ado
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
add a comment |
Thank you! VERY MUCH
– Born vs. Me
Jan 14 at 15:37
IT doesnt work ./test.sh: line 16: syntax error near unexpected tokenwhile' ./test.sh: line 16:
while read -r line; do'
– Born vs. Me
Jan 14 at 15:53
@Bornvs.Me: sorry I forgot ado
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
orsed
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