Shellscript Looping Through All Files in a Folder
I want to write a shellscript that will loop through all the files in a folder and echo "put ${filename}". Can anyone point me in the right direction?
bash shell loops
add a comment |
I want to write a shellscript that will loop through all the files in a folder and echo "put ${filename}". Can anyone point me in the right direction?
bash shell loops
2
What have you tried? What part of thefor
statement and the*
operator confuse you? Can you be more specific about what you know and what you don't know about the shell?
– S.Lott
Dec 14 '11 at 22:15
Just came across this -- a warning to anyone using this as a reference -- the answers do not handle filenames with spaces properly... refer to stackoverflow.com/questions/7039130/… for a better solution!!!
– blackghost
Jun 2 '17 at 15:59
add a comment |
I want to write a shellscript that will loop through all the files in a folder and echo "put ${filename}". Can anyone point me in the right direction?
bash shell loops
I want to write a shellscript that will loop through all the files in a folder and echo "put ${filename}". Can anyone point me in the right direction?
bash shell loops
bash shell loops
asked Dec 14 '11 at 22:14
sklineskline
120127
120127
2
What have you tried? What part of thefor
statement and the*
operator confuse you? Can you be more specific about what you know and what you don't know about the shell?
– S.Lott
Dec 14 '11 at 22:15
Just came across this -- a warning to anyone using this as a reference -- the answers do not handle filenames with spaces properly... refer to stackoverflow.com/questions/7039130/… for a better solution!!!
– blackghost
Jun 2 '17 at 15:59
add a comment |
2
What have you tried? What part of thefor
statement and the*
operator confuse you? Can you be more specific about what you know and what you don't know about the shell?
– S.Lott
Dec 14 '11 at 22:15
Just came across this -- a warning to anyone using this as a reference -- the answers do not handle filenames with spaces properly... refer to stackoverflow.com/questions/7039130/… for a better solution!!!
– blackghost
Jun 2 '17 at 15:59
2
2
What have you tried? What part of the
for
statement and the *
operator confuse you? Can you be more specific about what you know and what you don't know about the shell?– S.Lott
Dec 14 '11 at 22:15
What have you tried? What part of the
for
statement and the *
operator confuse you? Can you be more specific about what you know and what you don't know about the shell?– S.Lott
Dec 14 '11 at 22:15
Just came across this -- a warning to anyone using this as a reference -- the answers do not handle filenames with spaces properly... refer to stackoverflow.com/questions/7039130/… for a better solution!!!
– blackghost
Jun 2 '17 at 15:59
Just came across this -- a warning to anyone using this as a reference -- the answers do not handle filenames with spaces properly... refer to stackoverflow.com/questions/7039130/… for a better solution!!!
– blackghost
Jun 2 '17 at 15:59
add a comment |
6 Answers
6
active
oldest
votes
For files and directories, not recursive
for filename in *; do echo "put ${filename}"; done
For files only (excludes folders), not recursive
for file in *; do
if [ -f "$file" ]; then
echo "$file"
fi
done
For a recursive solution, see Bennet Yee's answer.
1
If the current directory happens to be empty, this outputs "put *" rather than correctly outputting nothing. Can it be fixed?
– JWWalker
Apr 27 '13 at 1:47
@ThisClark - Indeed it does. But I'm not sure that an answer not doing exactly what you want is a good reason to downvote.
– Oliver Charlesworth
Sep 3 '18 at 16:52
@ThisClark - True. But being generous to myself (!), one could argue that from a *nix perspective, a folder is is a file. (Possibly that's what I was thinking when I wrote the answer 7 years ago, but who knows...)
– Oliver Charlesworth
Sep 3 '18 at 17:07
What does nix even stand for? Disregarding the distinction of files from folders is not helpful.
– ThisClark
Sep 3 '18 at 17:38
add a comment |
recursively, including files in subdirectories?
find dir -type f -exec echo "put {}" ;
only files in that directory?
find dir -maxdepth 1 -type f -exec echo "put {}" ;
1
I get the errorfind 'dir': No such file or directory
when trying this.
– gbmhunter
Dec 19 '13 at 22:44
1
Silly me, by dir you meant replace with the directory you want. Still, slightly confusing!
– gbmhunter
Dec 19 '13 at 22:45
Recursively for files in the current directory, replacedir
with*
.
– ThisClark
Sep 3 '18 at 17:45
add a comment |
For all folders and files in the current directory
for file in *; do
echo "put $file"
done
Or, if you want to include subdirectories and files only:
find . -type f -exec echo put {} ;
If you want to include the folders themselves, take out the -type f
part.
add a comment |
If you don't have any files, then instead of printing * we can do this.
format=*.txt
for i in $format;
do
if [[ "$i" == "$format" ]]
then
echo "No Files"
else
echo "file name $i"
fi
done
add a comment |
One more alternative using ls
and sed
:
$ ls -1 <dir> | sed -e 's/^/put /'
and using ls
and xargs
:
$ ls -1 <dir> | xargs -n1 -i%f echo 'put %f'
+1, but -1 is not needed and you can do sed -e 's/^/put /'
– William Pursell
Dec 15 '11 at 1:12
@WilliamPursell Thanks, I've updated my response. Somehow, I misunderstood and made the braces and the dollar sign part of the solution. Also, I'ved another solution withxargs
andecho
, but thesed
one is still more concise.
– jcollado
Dec 15 '11 at 6:05
add a comment |
this will work also recursively if you have any sub directories and files inside them:
find . -type f|awk -F"/" '{print "put ",$NF}'
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f8512462%2fshellscript-looping-through-all-files-in-a-folder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
For files and directories, not recursive
for filename in *; do echo "put ${filename}"; done
For files only (excludes folders), not recursive
for file in *; do
if [ -f "$file" ]; then
echo "$file"
fi
done
For a recursive solution, see Bennet Yee's answer.
1
If the current directory happens to be empty, this outputs "put *" rather than correctly outputting nothing. Can it be fixed?
– JWWalker
Apr 27 '13 at 1:47
@ThisClark - Indeed it does. But I'm not sure that an answer not doing exactly what you want is a good reason to downvote.
– Oliver Charlesworth
Sep 3 '18 at 16:52
@ThisClark - True. But being generous to myself (!), one could argue that from a *nix perspective, a folder is is a file. (Possibly that's what I was thinking when I wrote the answer 7 years ago, but who knows...)
– Oliver Charlesworth
Sep 3 '18 at 17:07
What does nix even stand for? Disregarding the distinction of files from folders is not helpful.
– ThisClark
Sep 3 '18 at 17:38
add a comment |
For files and directories, not recursive
for filename in *; do echo "put ${filename}"; done
For files only (excludes folders), not recursive
for file in *; do
if [ -f "$file" ]; then
echo "$file"
fi
done
For a recursive solution, see Bennet Yee's answer.
1
If the current directory happens to be empty, this outputs "put *" rather than correctly outputting nothing. Can it be fixed?
– JWWalker
Apr 27 '13 at 1:47
@ThisClark - Indeed it does. But I'm not sure that an answer not doing exactly what you want is a good reason to downvote.
– Oliver Charlesworth
Sep 3 '18 at 16:52
@ThisClark - True. But being generous to myself (!), one could argue that from a *nix perspective, a folder is is a file. (Possibly that's what I was thinking when I wrote the answer 7 years ago, but who knows...)
– Oliver Charlesworth
Sep 3 '18 at 17:07
What does nix even stand for? Disregarding the distinction of files from folders is not helpful.
– ThisClark
Sep 3 '18 at 17:38
add a comment |
For files and directories, not recursive
for filename in *; do echo "put ${filename}"; done
For files only (excludes folders), not recursive
for file in *; do
if [ -f "$file" ]; then
echo "$file"
fi
done
For a recursive solution, see Bennet Yee's answer.
For files and directories, not recursive
for filename in *; do echo "put ${filename}"; done
For files only (excludes folders), not recursive
for file in *; do
if [ -f "$file" ]; then
echo "$file"
fi
done
For a recursive solution, see Bennet Yee's answer.
edited Sep 3 '18 at 17:43
ThisClark
8,42874868
8,42874868
answered Dec 14 '11 at 22:16
Oliver CharlesworthOliver Charlesworth
229k25469599
229k25469599
1
If the current directory happens to be empty, this outputs "put *" rather than correctly outputting nothing. Can it be fixed?
– JWWalker
Apr 27 '13 at 1:47
@ThisClark - Indeed it does. But I'm not sure that an answer not doing exactly what you want is a good reason to downvote.
– Oliver Charlesworth
Sep 3 '18 at 16:52
@ThisClark - True. But being generous to myself (!), one could argue that from a *nix perspective, a folder is is a file. (Possibly that's what I was thinking when I wrote the answer 7 years ago, but who knows...)
– Oliver Charlesworth
Sep 3 '18 at 17:07
What does nix even stand for? Disregarding the distinction of files from folders is not helpful.
– ThisClark
Sep 3 '18 at 17:38
add a comment |
1
If the current directory happens to be empty, this outputs "put *" rather than correctly outputting nothing. Can it be fixed?
– JWWalker
Apr 27 '13 at 1:47
@ThisClark - Indeed it does. But I'm not sure that an answer not doing exactly what you want is a good reason to downvote.
– Oliver Charlesworth
Sep 3 '18 at 16:52
@ThisClark - True. But being generous to myself (!), one could argue that from a *nix perspective, a folder is is a file. (Possibly that's what I was thinking when I wrote the answer 7 years ago, but who knows...)
– Oliver Charlesworth
Sep 3 '18 at 17:07
What does nix even stand for? Disregarding the distinction of files from folders is not helpful.
– ThisClark
Sep 3 '18 at 17:38
1
1
If the current directory happens to be empty, this outputs "put *" rather than correctly outputting nothing. Can it be fixed?
– JWWalker
Apr 27 '13 at 1:47
If the current directory happens to be empty, this outputs "put *" rather than correctly outputting nothing. Can it be fixed?
– JWWalker
Apr 27 '13 at 1:47
@ThisClark - Indeed it does. But I'm not sure that an answer not doing exactly what you want is a good reason to downvote.
– Oliver Charlesworth
Sep 3 '18 at 16:52
@ThisClark - Indeed it does. But I'm not sure that an answer not doing exactly what you want is a good reason to downvote.
– Oliver Charlesworth
Sep 3 '18 at 16:52
@ThisClark - True. But being generous to myself (!), one could argue that from a *nix perspective, a folder is is a file. (Possibly that's what I was thinking when I wrote the answer 7 years ago, but who knows...)
– Oliver Charlesworth
Sep 3 '18 at 17:07
@ThisClark - True. But being generous to myself (!), one could argue that from a *nix perspective, a folder is is a file. (Possibly that's what I was thinking when I wrote the answer 7 years ago, but who knows...)
– Oliver Charlesworth
Sep 3 '18 at 17:07
What does nix even stand for? Disregarding the distinction of files from folders is not helpful.
– ThisClark
Sep 3 '18 at 17:38
What does nix even stand for? Disregarding the distinction of files from folders is not helpful.
– ThisClark
Sep 3 '18 at 17:38
add a comment |
recursively, including files in subdirectories?
find dir -type f -exec echo "put {}" ;
only files in that directory?
find dir -maxdepth 1 -type f -exec echo "put {}" ;
1
I get the errorfind 'dir': No such file or directory
when trying this.
– gbmhunter
Dec 19 '13 at 22:44
1
Silly me, by dir you meant replace with the directory you want. Still, slightly confusing!
– gbmhunter
Dec 19 '13 at 22:45
Recursively for files in the current directory, replacedir
with*
.
– ThisClark
Sep 3 '18 at 17:45
add a comment |
recursively, including files in subdirectories?
find dir -type f -exec echo "put {}" ;
only files in that directory?
find dir -maxdepth 1 -type f -exec echo "put {}" ;
1
I get the errorfind 'dir': No such file or directory
when trying this.
– gbmhunter
Dec 19 '13 at 22:44
1
Silly me, by dir you meant replace with the directory you want. Still, slightly confusing!
– gbmhunter
Dec 19 '13 at 22:45
Recursively for files in the current directory, replacedir
with*
.
– ThisClark
Sep 3 '18 at 17:45
add a comment |
recursively, including files in subdirectories?
find dir -type f -exec echo "put {}" ;
only files in that directory?
find dir -maxdepth 1 -type f -exec echo "put {}" ;
recursively, including files in subdirectories?
find dir -type f -exec echo "put {}" ;
only files in that directory?
find dir -maxdepth 1 -type f -exec echo "put {}" ;
edited Nov 16 '12 at 10:07
perilbrain
6,46712132
6,46712132
answered Dec 14 '11 at 22:17
Bennet YeeBennet Yee
36615
36615
1
I get the errorfind 'dir': No such file or directory
when trying this.
– gbmhunter
Dec 19 '13 at 22:44
1
Silly me, by dir you meant replace with the directory you want. Still, slightly confusing!
– gbmhunter
Dec 19 '13 at 22:45
Recursively for files in the current directory, replacedir
with*
.
– ThisClark
Sep 3 '18 at 17:45
add a comment |
1
I get the errorfind 'dir': No such file or directory
when trying this.
– gbmhunter
Dec 19 '13 at 22:44
1
Silly me, by dir you meant replace with the directory you want. Still, slightly confusing!
– gbmhunter
Dec 19 '13 at 22:45
Recursively for files in the current directory, replacedir
with*
.
– ThisClark
Sep 3 '18 at 17:45
1
1
I get the error
find 'dir': No such file or directory
when trying this.– gbmhunter
Dec 19 '13 at 22:44
I get the error
find 'dir': No such file or directory
when trying this.– gbmhunter
Dec 19 '13 at 22:44
1
1
Silly me, by dir you meant replace with the directory you want. Still, slightly confusing!
– gbmhunter
Dec 19 '13 at 22:45
Silly me, by dir you meant replace with the directory you want. Still, slightly confusing!
– gbmhunter
Dec 19 '13 at 22:45
Recursively for files in the current directory, replace
dir
with *
.– ThisClark
Sep 3 '18 at 17:45
Recursively for files in the current directory, replace
dir
with *
.– ThisClark
Sep 3 '18 at 17:45
add a comment |
For all folders and files in the current directory
for file in *; do
echo "put $file"
done
Or, if you want to include subdirectories and files only:
find . -type f -exec echo put {} ;
If you want to include the folders themselves, take out the -type f
part.
add a comment |
For all folders and files in the current directory
for file in *; do
echo "put $file"
done
Or, if you want to include subdirectories and files only:
find . -type f -exec echo put {} ;
If you want to include the folders themselves, take out the -type f
part.
add a comment |
For all folders and files in the current directory
for file in *; do
echo "put $file"
done
Or, if you want to include subdirectories and files only:
find . -type f -exec echo put {} ;
If you want to include the folders themselves, take out the -type f
part.
For all folders and files in the current directory
for file in *; do
echo "put $file"
done
Or, if you want to include subdirectories and files only:
find . -type f -exec echo put {} ;
If you want to include the folders themselves, take out the -type f
part.
edited Sep 3 '18 at 17:08
ThisClark
8,42874868
8,42874868
answered Dec 14 '11 at 22:18
KevinKevin
39.4k1079113
39.4k1079113
add a comment |
add a comment |
If you don't have any files, then instead of printing * we can do this.
format=*.txt
for i in $format;
do
if [[ "$i" == "$format" ]]
then
echo "No Files"
else
echo "file name $i"
fi
done
add a comment |
If you don't have any files, then instead of printing * we can do this.
format=*.txt
for i in $format;
do
if [[ "$i" == "$format" ]]
then
echo "No Files"
else
echo "file name $i"
fi
done
add a comment |
If you don't have any files, then instead of printing * we can do this.
format=*.txt
for i in $format;
do
if [[ "$i" == "$format" ]]
then
echo "No Files"
else
echo "file name $i"
fi
done
If you don't have any files, then instead of printing * we can do this.
format=*.txt
for i in $format;
do
if [[ "$i" == "$format" ]]
then
echo "No Files"
else
echo "file name $i"
fi
done
edited Sep 26 '14 at 18:41
answered Sep 24 '14 at 20:30
Mad-DMad-D
2,06873769
2,06873769
add a comment |
add a comment |
One more alternative using ls
and sed
:
$ ls -1 <dir> | sed -e 's/^/put /'
and using ls
and xargs
:
$ ls -1 <dir> | xargs -n1 -i%f echo 'put %f'
+1, but -1 is not needed and you can do sed -e 's/^/put /'
– William Pursell
Dec 15 '11 at 1:12
@WilliamPursell Thanks, I've updated my response. Somehow, I misunderstood and made the braces and the dollar sign part of the solution. Also, I'ved another solution withxargs
andecho
, but thesed
one is still more concise.
– jcollado
Dec 15 '11 at 6:05
add a comment |
One more alternative using ls
and sed
:
$ ls -1 <dir> | sed -e 's/^/put /'
and using ls
and xargs
:
$ ls -1 <dir> | xargs -n1 -i%f echo 'put %f'
+1, but -1 is not needed and you can do sed -e 's/^/put /'
– William Pursell
Dec 15 '11 at 1:12
@WilliamPursell Thanks, I've updated my response. Somehow, I misunderstood and made the braces and the dollar sign part of the solution. Also, I'ved another solution withxargs
andecho
, but thesed
one is still more concise.
– jcollado
Dec 15 '11 at 6:05
add a comment |
One more alternative using ls
and sed
:
$ ls -1 <dir> | sed -e 's/^/put /'
and using ls
and xargs
:
$ ls -1 <dir> | xargs -n1 -i%f echo 'put %f'
One more alternative using ls
and sed
:
$ ls -1 <dir> | sed -e 's/^/put /'
and using ls
and xargs
:
$ ls -1 <dir> | xargs -n1 -i%f echo 'put %f'
edited Dec 15 '11 at 6:02
answered Dec 14 '11 at 22:21
jcolladojcollado
30.1k577118
30.1k577118
+1, but -1 is not needed and you can do sed -e 's/^/put /'
– William Pursell
Dec 15 '11 at 1:12
@WilliamPursell Thanks, I've updated my response. Somehow, I misunderstood and made the braces and the dollar sign part of the solution. Also, I'ved another solution withxargs
andecho
, but thesed
one is still more concise.
– jcollado
Dec 15 '11 at 6:05
add a comment |
+1, but -1 is not needed and you can do sed -e 's/^/put /'
– William Pursell
Dec 15 '11 at 1:12
@WilliamPursell Thanks, I've updated my response. Somehow, I misunderstood and made the braces and the dollar sign part of the solution. Also, I'ved another solution withxargs
andecho
, but thesed
one is still more concise.
– jcollado
Dec 15 '11 at 6:05
+1, but -1 is not needed and you can do sed -e 's/^/put /'
– William Pursell
Dec 15 '11 at 1:12
+1, but -1 is not needed and you can do sed -e 's/^/put /'
– William Pursell
Dec 15 '11 at 1:12
@WilliamPursell Thanks, I've updated my response. Somehow, I misunderstood and made the braces and the dollar sign part of the solution. Also, I'ved another solution with
xargs
and echo
, but the sed
one is still more concise.– jcollado
Dec 15 '11 at 6:05
@WilliamPursell Thanks, I've updated my response. Somehow, I misunderstood and made the braces and the dollar sign part of the solution. Also, I'ved another solution with
xargs
and echo
, but the sed
one is still more concise.– jcollado
Dec 15 '11 at 6:05
add a comment |
this will work also recursively if you have any sub directories and files inside them:
find . -type f|awk -F"/" '{print "put ",$NF}'
add a comment |
this will work also recursively if you have any sub directories and files inside them:
find . -type f|awk -F"/" '{print "put ",$NF}'
add a comment |
this will work also recursively if you have any sub directories and files inside them:
find . -type f|awk -F"/" '{print "put ",$NF}'
this will work also recursively if you have any sub directories and files inside them:
find . -type f|awk -F"/" '{print "put ",$NF}'
answered Dec 15 '11 at 5:58
VijayVijay
32.9k77190297
32.9k77190297
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f8512462%2fshellscript-looping-through-all-files-in-a-folder%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
2
What have you tried? What part of the
for
statement and the*
operator confuse you? Can you be more specific about what you know and what you don't know about the shell?– S.Lott
Dec 14 '11 at 22:15
Just came across this -- a warning to anyone using this as a reference -- the answers do not handle filenames with spaces properly... refer to stackoverflow.com/questions/7039130/… for a better solution!!!
– blackghost
Jun 2 '17 at 15:59