Loop through files excluding directories
Clash Royale CLAN TAG#URR8PPP
I need my script to do something to every file in the current directory excluding any sub-directories.
For example, in the current path, there are 5 files, but 1 of them is a folder (a sub-directory).
My script should activate a command given as arguments when running said script. I.e. "bash script wc -w" should give the word count of each file in the current directory, but not any of the folders, so that the output never has any of the "/sub/dir: Is a directory" lines.
My current script:
#!/bin/bash
dir=`pwd`
for file in $dir/*
do
$* $file
done
I just need to exclude directories for the loop, but I don`t know how.
bash for
add a comment |
I need my script to do something to every file in the current directory excluding any sub-directories.
For example, in the current path, there are 5 files, but 1 of them is a folder (a sub-directory).
My script should activate a command given as arguments when running said script. I.e. "bash script wc -w" should give the word count of each file in the current directory, but not any of the folders, so that the output never has any of the "/sub/dir: Is a directory" lines.
My current script:
#!/bin/bash
dir=`pwd`
for file in $dir/*
do
$* $file
done
I just need to exclude directories for the loop, but I don`t know how.
bash for
add a comment |
I need my script to do something to every file in the current directory excluding any sub-directories.
For example, in the current path, there are 5 files, but 1 of them is a folder (a sub-directory).
My script should activate a command given as arguments when running said script. I.e. "bash script wc -w" should give the word count of each file in the current directory, but not any of the folders, so that the output never has any of the "/sub/dir: Is a directory" lines.
My current script:
#!/bin/bash
dir=`pwd`
for file in $dir/*
do
$* $file
done
I just need to exclude directories for the loop, but I don`t know how.
bash for
I need my script to do something to every file in the current directory excluding any sub-directories.
For example, in the current path, there are 5 files, but 1 of them is a folder (a sub-directory).
My script should activate a command given as arguments when running said script. I.e. "bash script wc -w" should give the word count of each file in the current directory, but not any of the folders, so that the output never has any of the "/sub/dir: Is a directory" lines.
My current script:
#!/bin/bash
dir=`pwd`
for file in $dir/*
do
$* $file
done
I just need to exclude directories for the loop, but I don`t know how.
bash for
bash for
edited Mar 13 '17 at 20:25
don_crissti
51.3k15137165
51.3k15137165
asked Mar 13 '17 at 18:28
Tõnis PiipTõnis Piip
5615
5615
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
#!/bin/bash -
for file in "$dir"/*
do
if [ ! -d "$file" ]; then
"$@" "$file"
fi
done
Note that it also excludes files that are of type symlink and where the symlink resolves to a file of type directory (which is probably what you want).
Alternative (from comments), check only for files:
for file in "$dir"/*
do
if [ -f "$file" ]; then
"$@" "$file"
fi
done
@roaima good idea!
– laktak
Mar 13 '17 at 19:16
2
Better to useif [ -f "$file" ]; then ...
. Not a directory would return not only regular files but sockets and symlinks too.
– JRFerguson
Mar 13 '17 at 19:29
@JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with! -d
already.
– roaima
Mar 13 '17 at 20:41
I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.
– Tõnis Piip
Mar 14 '17 at 8:07
add a comment |
Here's an alternative to using a for loop if what you need to do is simple (and doesn't involve setting variables in the main shell &c).
You can use find
with -exec
and use -maxdepth 1
to avoid recursing into the subdirectory.
[ -n "$1" ] && find "$dir" -type f -mindepth 1 -maxdepth 1 -exec "$@" "" ;
The [ -n "$1" ]
is there to avoid executing all the files in the directory when the script isn't passed any arguments.
add a comment |
In zsh, you can use glob qualifiers to restrict wildcard matches by file type. For example, adding (.)
after the pattern restricts it to regular files.
wc -w *(.)
To cope with file names beginning with -
or .
, use wc -c -- *(.N)
or wc -c ./*(.N)
. If you want to include symbolic links to regular files as well, make that *(-.)
.
The other common shells have no such feature, so you need to use some different mechanism for filtering by file such as testing file types in a loop or find
.
add a comment |
Bash for loop excluding sub-directories ('/') and links (@):
for f in `ls|egrep -v '/|@'`; do echo $f; done
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%2f351210%2floop-through-files-excluding-directories%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
#!/bin/bash -
for file in "$dir"/*
do
if [ ! -d "$file" ]; then
"$@" "$file"
fi
done
Note that it also excludes files that are of type symlink and where the symlink resolves to a file of type directory (which is probably what you want).
Alternative (from comments), check only for files:
for file in "$dir"/*
do
if [ -f "$file" ]; then
"$@" "$file"
fi
done
@roaima good idea!
– laktak
Mar 13 '17 at 19:16
2
Better to useif [ -f "$file" ]; then ...
. Not a directory would return not only regular files but sockets and symlinks too.
– JRFerguson
Mar 13 '17 at 19:29
@JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with! -d
already.
– roaima
Mar 13 '17 at 20:41
I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.
– Tõnis Piip
Mar 14 '17 at 8:07
add a comment |
#!/bin/bash -
for file in "$dir"/*
do
if [ ! -d "$file" ]; then
"$@" "$file"
fi
done
Note that it also excludes files that are of type symlink and where the symlink resolves to a file of type directory (which is probably what you want).
Alternative (from comments), check only for files:
for file in "$dir"/*
do
if [ -f "$file" ]; then
"$@" "$file"
fi
done
@roaima good idea!
– laktak
Mar 13 '17 at 19:16
2
Better to useif [ -f "$file" ]; then ...
. Not a directory would return not only regular files but sockets and symlinks too.
– JRFerguson
Mar 13 '17 at 19:29
@JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with! -d
already.
– roaima
Mar 13 '17 at 20:41
I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.
– Tõnis Piip
Mar 14 '17 at 8:07
add a comment |
#!/bin/bash -
for file in "$dir"/*
do
if [ ! -d "$file" ]; then
"$@" "$file"
fi
done
Note that it also excludes files that are of type symlink and where the symlink resolves to a file of type directory (which is probably what you want).
Alternative (from comments), check only for files:
for file in "$dir"/*
do
if [ -f "$file" ]; then
"$@" "$file"
fi
done
#!/bin/bash -
for file in "$dir"/*
do
if [ ! -d "$file" ]; then
"$@" "$file"
fi
done
Note that it also excludes files that are of type symlink and where the symlink resolves to a file of type directory (which is probably what you want).
Alternative (from comments), check only for files:
for file in "$dir"/*
do
if [ -f "$file" ]; then
"$@" "$file"
fi
done
edited Mar 13 '17 at 21:10
answered Mar 13 '17 at 18:31
laktaklaktak
811510
811510
@roaima good idea!
– laktak
Mar 13 '17 at 19:16
2
Better to useif [ -f "$file" ]; then ...
. Not a directory would return not only regular files but sockets and symlinks too.
– JRFerguson
Mar 13 '17 at 19:29
@JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with! -d
already.
– roaima
Mar 13 '17 at 20:41
I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.
– Tõnis Piip
Mar 14 '17 at 8:07
add a comment |
@roaima good idea!
– laktak
Mar 13 '17 at 19:16
2
Better to useif [ -f "$file" ]; then ...
. Not a directory would return not only regular files but sockets and symlinks too.
– JRFerguson
Mar 13 '17 at 19:29
@JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with! -d
already.
– roaima
Mar 13 '17 at 20:41
I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.
– Tõnis Piip
Mar 14 '17 at 8:07
@roaima good idea!
– laktak
Mar 13 '17 at 19:16
@roaima good idea!
– laktak
Mar 13 '17 at 19:16
2
2
Better to use
if [ -f "$file" ]; then ...
. Not a directory would return not only regular files but sockets and symlinks too.– JRFerguson
Mar 13 '17 at 19:29
Better to use
if [ -f "$file" ]; then ...
. Not a directory would return not only regular files but sockets and symlinks too.– JRFerguson
Mar 13 '17 at 19:29
@JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with
! -d
already.– roaima
Mar 13 '17 at 20:41
@JRFerguson based on the OP's question I would say that including sockets and pipes would be a really bad idea. As for symlinks, those are implicitly included with
! -d
already.– roaima
Mar 13 '17 at 20:41
I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.
– Tõnis Piip
Mar 14 '17 at 8:07
I used [ -f $file ] in my final script. Note the lack of quotation marks in the if condition. Almost everywhere I looked had quotation marks around $file, yet I had to delete them in order for my script to work.
– Tõnis Piip
Mar 14 '17 at 8:07
add a comment |
Here's an alternative to using a for loop if what you need to do is simple (and doesn't involve setting variables in the main shell &c).
You can use find
with -exec
and use -maxdepth 1
to avoid recursing into the subdirectory.
[ -n "$1" ] && find "$dir" -type f -mindepth 1 -maxdepth 1 -exec "$@" "" ;
The [ -n "$1" ]
is there to avoid executing all the files in the directory when the script isn't passed any arguments.
add a comment |
Here's an alternative to using a for loop if what you need to do is simple (and doesn't involve setting variables in the main shell &c).
You can use find
with -exec
and use -maxdepth 1
to avoid recursing into the subdirectory.
[ -n "$1" ] && find "$dir" -type f -mindepth 1 -maxdepth 1 -exec "$@" "" ;
The [ -n "$1" ]
is there to avoid executing all the files in the directory when the script isn't passed any arguments.
add a comment |
Here's an alternative to using a for loop if what you need to do is simple (and doesn't involve setting variables in the main shell &c).
You can use find
with -exec
and use -maxdepth 1
to avoid recursing into the subdirectory.
[ -n "$1" ] && find "$dir" -type f -mindepth 1 -maxdepth 1 -exec "$@" "" ;
The [ -n "$1" ]
is there to avoid executing all the files in the directory when the script isn't passed any arguments.
Here's an alternative to using a for loop if what you need to do is simple (and doesn't involve setting variables in the main shell &c).
You can use find
with -exec
and use -maxdepth 1
to avoid recursing into the subdirectory.
[ -n "$1" ] && find "$dir" -type f -mindepth 1 -maxdepth 1 -exec "$@" "" ;
The [ -n "$1" ]
is there to avoid executing all the files in the directory when the script isn't passed any arguments.
answered Mar 13 '17 at 20:40
Gregory NisbetGregory Nisbet
1,4441020
1,4441020
add a comment |
add a comment |
In zsh, you can use glob qualifiers to restrict wildcard matches by file type. For example, adding (.)
after the pattern restricts it to regular files.
wc -w *(.)
To cope with file names beginning with -
or .
, use wc -c -- *(.N)
or wc -c ./*(.N)
. If you want to include symbolic links to regular files as well, make that *(-.)
.
The other common shells have no such feature, so you need to use some different mechanism for filtering by file such as testing file types in a loop or find
.
add a comment |
In zsh, you can use glob qualifiers to restrict wildcard matches by file type. For example, adding (.)
after the pattern restricts it to regular files.
wc -w *(.)
To cope with file names beginning with -
or .
, use wc -c -- *(.N)
or wc -c ./*(.N)
. If you want to include symbolic links to regular files as well, make that *(-.)
.
The other common shells have no such feature, so you need to use some different mechanism for filtering by file such as testing file types in a loop or find
.
add a comment |
In zsh, you can use glob qualifiers to restrict wildcard matches by file type. For example, adding (.)
after the pattern restricts it to regular files.
wc -w *(.)
To cope with file names beginning with -
or .
, use wc -c -- *(.N)
or wc -c ./*(.N)
. If you want to include symbolic links to regular files as well, make that *(-.)
.
The other common shells have no such feature, so you need to use some different mechanism for filtering by file such as testing file types in a loop or find
.
In zsh, you can use glob qualifiers to restrict wildcard matches by file type. For example, adding (.)
after the pattern restricts it to regular files.
wc -w *(.)
To cope with file names beginning with -
or .
, use wc -c -- *(.N)
or wc -c ./*(.N)
. If you want to include symbolic links to regular files as well, make that *(-.)
.
The other common shells have no such feature, so you need to use some different mechanism for filtering by file such as testing file types in a loop or find
.
answered Mar 14 '17 at 1:20
GillesGilles
540k12810941607
540k12810941607
add a comment |
add a comment |
Bash for loop excluding sub-directories ('/') and links (@):
for f in `ls|egrep -v '/|@'`; do echo $f; done
add a comment |
Bash for loop excluding sub-directories ('/') and links (@):
for f in `ls|egrep -v '/|@'`; do echo $f; done
add a comment |
Bash for loop excluding sub-directories ('/') and links (@):
for f in `ls|egrep -v '/|@'`; do echo $f; done
Bash for loop excluding sub-directories ('/') and links (@):
for f in `ls|egrep -v '/|@'`; do echo $f; done
answered Feb 8 at 18:25
James ArmstrongJames Armstrong
1
1
add a comment |
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%2f351210%2floop-through-files-excluding-directories%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