Iterating over files in Bash and obtaining the index and the counts
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
The proper way to iterate over files in a directory in Bash is using "for loop" and glob as following:
for f in *.jpg; do
echo "- Processing file: $f"
done
But how can I retrieve the total count of the files and the current index of the loop interaction? I need the total count, not the cumulative count to show the progress.
shell-script
add a comment |
The proper way to iterate over files in a directory in Bash is using "for loop" and glob as following:
for f in *.jpg; do
echo "- Processing file: $f"
done
But how can I retrieve the total count of the files and the current index of the loop interaction? I need the total count, not the cumulative count to show the progress.
shell-script
add a comment |
The proper way to iterate over files in a directory in Bash is using "for loop" and glob as following:
for f in *.jpg; do
echo "- Processing file: $f"
done
But how can I retrieve the total count of the files and the current index of the loop interaction? I need the total count, not the cumulative count to show the progress.
shell-script
The proper way to iterate over files in a directory in Bash is using "for loop" and glob as following:
for f in *.jpg; do
echo "- Processing file: $f"
done
But how can I retrieve the total count of the files and the current index of the loop interaction? I need the total count, not the cumulative count to show the progress.
shell-script
shell-script
edited Jan 4 at 3:02
bman
asked Jan 4 at 2:49
bmanbman
13019
13019
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I would store the list of files in an array, so that you don't have to read the file system twice, increasing performance and reducing potential race conditions. Then use another variable as the index.
files=(*.jpg)
total=${#files[@]}
i=0
for f in "${files[@]}"; do
i=$(( i + 1 ))
echo index $i
echo total $total
echo "- Processing file: $f"
done
Explanation
files=(*.jpg)
: store the glob into the array$files
total=${#files[@]}
: read the total into$total
i=0
: initialise$i
to 0.
i=$(( i + 1 ))
: add 1 to$i
each loop
This presumes that the "first" loop is 1. Depending on your opinion, you might want to start at 0 instead.
I need to know the count while the for loop is running to show the progress, not at the end.
– bman
Jan 4 at 2:55
@bman I'm not sure what you mean. This will display the count during each loop, not at the end.
– Sparhawk
Jan 4 at 2:56
It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files:ls -1q *.jpg | wc -l
– bman
Jan 4 at 2:58
@bman Ah got it. that might work generally, but (as you suggest in your question), parsingls
is not great. Give me a minute and I'll edit.
– Sparhawk
Jan 4 at 2:59
@bman Okay done.
– Sparhawk
Jan 4 at 3:05
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%2f492366%2fiterating-over-files-in-bash-and-obtaining-the-index-and-the-counts%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
I would store the list of files in an array, so that you don't have to read the file system twice, increasing performance and reducing potential race conditions. Then use another variable as the index.
files=(*.jpg)
total=${#files[@]}
i=0
for f in "${files[@]}"; do
i=$(( i + 1 ))
echo index $i
echo total $total
echo "- Processing file: $f"
done
Explanation
files=(*.jpg)
: store the glob into the array$files
total=${#files[@]}
: read the total into$total
i=0
: initialise$i
to 0.
i=$(( i + 1 ))
: add 1 to$i
each loop
This presumes that the "first" loop is 1. Depending on your opinion, you might want to start at 0 instead.
I need to know the count while the for loop is running to show the progress, not at the end.
– bman
Jan 4 at 2:55
@bman I'm not sure what you mean. This will display the count during each loop, not at the end.
– Sparhawk
Jan 4 at 2:56
It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files:ls -1q *.jpg | wc -l
– bman
Jan 4 at 2:58
@bman Ah got it. that might work generally, but (as you suggest in your question), parsingls
is not great. Give me a minute and I'll edit.
– Sparhawk
Jan 4 at 2:59
@bman Okay done.
– Sparhawk
Jan 4 at 3:05
add a comment |
I would store the list of files in an array, so that you don't have to read the file system twice, increasing performance and reducing potential race conditions. Then use another variable as the index.
files=(*.jpg)
total=${#files[@]}
i=0
for f in "${files[@]}"; do
i=$(( i + 1 ))
echo index $i
echo total $total
echo "- Processing file: $f"
done
Explanation
files=(*.jpg)
: store the glob into the array$files
total=${#files[@]}
: read the total into$total
i=0
: initialise$i
to 0.
i=$(( i + 1 ))
: add 1 to$i
each loop
This presumes that the "first" loop is 1. Depending on your opinion, you might want to start at 0 instead.
I need to know the count while the for loop is running to show the progress, not at the end.
– bman
Jan 4 at 2:55
@bman I'm not sure what you mean. This will display the count during each loop, not at the end.
– Sparhawk
Jan 4 at 2:56
It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files:ls -1q *.jpg | wc -l
– bman
Jan 4 at 2:58
@bman Ah got it. that might work generally, but (as you suggest in your question), parsingls
is not great. Give me a minute and I'll edit.
– Sparhawk
Jan 4 at 2:59
@bman Okay done.
– Sparhawk
Jan 4 at 3:05
add a comment |
I would store the list of files in an array, so that you don't have to read the file system twice, increasing performance and reducing potential race conditions. Then use another variable as the index.
files=(*.jpg)
total=${#files[@]}
i=0
for f in "${files[@]}"; do
i=$(( i + 1 ))
echo index $i
echo total $total
echo "- Processing file: $f"
done
Explanation
files=(*.jpg)
: store the glob into the array$files
total=${#files[@]}
: read the total into$total
i=0
: initialise$i
to 0.
i=$(( i + 1 ))
: add 1 to$i
each loop
This presumes that the "first" loop is 1. Depending on your opinion, you might want to start at 0 instead.
I would store the list of files in an array, so that you don't have to read the file system twice, increasing performance and reducing potential race conditions. Then use another variable as the index.
files=(*.jpg)
total=${#files[@]}
i=0
for f in "${files[@]}"; do
i=$(( i + 1 ))
echo index $i
echo total $total
echo "- Processing file: $f"
done
Explanation
files=(*.jpg)
: store the glob into the array$files
total=${#files[@]}
: read the total into$total
i=0
: initialise$i
to 0.
i=$(( i + 1 ))
: add 1 to$i
each loop
This presumes that the "first" loop is 1. Depending on your opinion, you might want to start at 0 instead.
edited Jan 4 at 3:05
answered Jan 4 at 2:54
SparhawkSparhawk
10.3k744101
10.3k744101
I need to know the count while the for loop is running to show the progress, not at the end.
– bman
Jan 4 at 2:55
@bman I'm not sure what you mean. This will display the count during each loop, not at the end.
– Sparhawk
Jan 4 at 2:56
It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files:ls -1q *.jpg | wc -l
– bman
Jan 4 at 2:58
@bman Ah got it. that might work generally, but (as you suggest in your question), parsingls
is not great. Give me a minute and I'll edit.
– Sparhawk
Jan 4 at 2:59
@bman Okay done.
– Sparhawk
Jan 4 at 3:05
add a comment |
I need to know the count while the for loop is running to show the progress, not at the end.
– bman
Jan 4 at 2:55
@bman I'm not sure what you mean. This will display the count during each loop, not at the end.
– Sparhawk
Jan 4 at 2:56
It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files:ls -1q *.jpg | wc -l
– bman
Jan 4 at 2:58
@bman Ah got it. that might work generally, but (as you suggest in your question), parsingls
is not great. Give me a minute and I'll edit.
– Sparhawk
Jan 4 at 2:59
@bman Okay done.
– Sparhawk
Jan 4 at 3:05
I need to know the count while the for loop is running to show the progress, not at the end.
– bman
Jan 4 at 2:55
I need to know the count while the for loop is running to show the progress, not at the end.
– bman
Jan 4 at 2:55
@bman I'm not sure what you mean. This will display the count during each loop, not at the end.
– Sparhawk
Jan 4 at 2:56
@bman I'm not sure what you mean. This will display the count during each loop, not at the end.
– Sparhawk
Jan 4 at 2:56
It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files:
ls -1q *.jpg | wc -l
– bman
Jan 4 at 2:58
It displays the index, not the total count of files. I think I should use something like this for obtaining the count of files:
ls -1q *.jpg | wc -l
– bman
Jan 4 at 2:58
@bman Ah got it. that might work generally, but (as you suggest in your question), parsing
ls
is not great. Give me a minute and I'll edit.– Sparhawk
Jan 4 at 2:59
@bman Ah got it. that might work generally, but (as you suggest in your question), parsing
ls
is not great. Give me a minute and I'll edit.– Sparhawk
Jan 4 at 2:59
@bman Okay done.
– Sparhawk
Jan 4 at 3:05
@bman Okay done.
– Sparhawk
Jan 4 at 3:05
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%2f492366%2fiterating-over-files-in-bash-and-obtaining-the-index-and-the-counts%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