Looping through all files, skipping some dynamically Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionLooping through files with spaces in the names?Bash script for looping through filesLooping through a file using a columnLooping through unix datesLooping through lines in several files (bash)Bash looping through two sets of fileslooping through JSON array in shell scriptNeed to iterate through subdirectories, concatenating files, with an iterative numberFlatpak - Safely Relocate Sandboxed Home DirectoryWhile loops not looping through all values
Hangman Game with C++
How does the secondary effect of the Heat Metal spell interact with a creature resistant/immune to fire damage?
How could we fake a moon landing now?
Quadrilaterals with equal sides
Should there be a hyphen in the construction "IT affin"?
How often does castling occur in grandmaster games?
Sentence order: Where to put もう
Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?
Do wooden building fires get hotter than 600°C?
How were pictures turned from film to a big picture in a picture frame before digital scanning?
How do I decide if I need to go for Normalization and not Standardization or vice-versa?
Who can remove European Commissioners?
What initially awakened the Balrog?
How do living politicians protect their readily obtainable signatures from misuse?
Amount of permutations on an NxNxN Rubik's Cube
What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?
Why weren't discrete x86 CPUs ever used in game hardware?
How fail-safe is nr as stop bytes?
If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?
QGIS virtual layer functionality does not seem to support memory layers
How to improve on this Stylesheet Manipulation for Message Styling
Trademark violation for app?
An adverb for when you're not exaggerating
"Lost his faith in humanity in the trenches of Verdun" — last line of an SF story
Looping through all files, skipping some dynamically
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionLooping through files with spaces in the names?Bash script for looping through filesLooping through a file using a columnLooping through unix datesLooping through lines in several files (bash)Bash looping through two sets of fileslooping through JSON array in shell scriptNeed to iterate through subdirectories, concatenating files, with an iterative numberFlatpak - Safely Relocate Sandboxed Home DirectoryWhile loops not looping through all values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
rather new to unix. Trying to create a script to loop through all files and subdirectories given a directory and printing them. I would like to be able to skip certain directories dynamically though.
My idea was to create a for loop, for i in $(find ./ -printf '%fn')
as this will iterate through everything from the given directory, but I was curious if there was a way to skip over a directory if I came across one I didn't want to go down.
As in
if [ "$i" == something I don't want to go down ]
then
skip this directory
fi
Wasn't sure if it was possible. Found ways to skip directories using prune function but this requires you to know the types you want to skip beforehand, at least that was my understanding. Thanks.
linux bash shell scripting find
New contributor
add a comment |
rather new to unix. Trying to create a script to loop through all files and subdirectories given a directory and printing them. I would like to be able to skip certain directories dynamically though.
My idea was to create a for loop, for i in $(find ./ -printf '%fn')
as this will iterate through everything from the given directory, but I was curious if there was a way to skip over a directory if I came across one I didn't want to go down.
As in
if [ "$i" == something I don't want to go down ]
then
skip this directory
fi
Wasn't sure if it was possible. Found ways to skip directories using prune function but this requires you to know the types you want to skip beforehand, at least that was my understanding. Thanks.
linux bash shell scripting find
New contributor
add a comment |
rather new to unix. Trying to create a script to loop through all files and subdirectories given a directory and printing them. I would like to be able to skip certain directories dynamically though.
My idea was to create a for loop, for i in $(find ./ -printf '%fn')
as this will iterate through everything from the given directory, but I was curious if there was a way to skip over a directory if I came across one I didn't want to go down.
As in
if [ "$i" == something I don't want to go down ]
then
skip this directory
fi
Wasn't sure if it was possible. Found ways to skip directories using prune function but this requires you to know the types you want to skip beforehand, at least that was my understanding. Thanks.
linux bash shell scripting find
New contributor
rather new to unix. Trying to create a script to loop through all files and subdirectories given a directory and printing them. I would like to be able to skip certain directories dynamically though.
My idea was to create a for loop, for i in $(find ./ -printf '%fn')
as this will iterate through everything from the given directory, but I was curious if there was a way to skip over a directory if I came across one I didn't want to go down.
As in
if [ "$i" == something I don't want to go down ]
then
skip this directory
fi
Wasn't sure if it was possible. Found ways to skip directories using prune function but this requires you to know the types you want to skip beforehand, at least that was my understanding. Thanks.
linux bash shell scripting find
linux bash shell scripting find
New contributor
New contributor
edited Apr 15 at 2:41
mosvy
10.4k11238
10.4k11238
New contributor
asked Apr 15 at 1:00
m.smith7878m.smith7878
1
1
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Yes, -exec
is a predicate just like -name
, and find
will consider the exit status of the command and treat it as false if it's non-zero.
But notice that a) this only applies to the -exec ... ;
form, not to the -exec .. +
form (which is always true) and b) forking a separate process for each file or directory is slow.
For example this will skip all directories that contain a file named foo.txt
:
find . -type d -exec test -f /foo.txt ; -prune -or -print
$ mkdir -p a/1,2,3/x,y,z; touch a/1,2,3/x,y,z/a.txt
$ find . -type d -exec test -f /foo.txt ; -prune -or -print
... lots of files ...
$ touch a/1,2/foo.txt a/3/x,y/foo.txt
$ find . -type d -exec test -f /foo.txt ; -prune -or -print
.
./a
./a/3
./a/3/z
./a/3/z/a.txt
Of course, instead of the test -f ...
static command you can use eg. path/to/some/script ...
. With GNU find (the default in Linux) you can use -execdir
instead of -exec
.
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
);
);
m.smith7878 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f512466%2flooping-through-all-files-skipping-some-dynamically%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
Yes, -exec
is a predicate just like -name
, and find
will consider the exit status of the command and treat it as false if it's non-zero.
But notice that a) this only applies to the -exec ... ;
form, not to the -exec .. +
form (which is always true) and b) forking a separate process for each file or directory is slow.
For example this will skip all directories that contain a file named foo.txt
:
find . -type d -exec test -f /foo.txt ; -prune -or -print
$ mkdir -p a/1,2,3/x,y,z; touch a/1,2,3/x,y,z/a.txt
$ find . -type d -exec test -f /foo.txt ; -prune -or -print
... lots of files ...
$ touch a/1,2/foo.txt a/3/x,y/foo.txt
$ find . -type d -exec test -f /foo.txt ; -prune -or -print
.
./a
./a/3
./a/3/z
./a/3/z/a.txt
Of course, instead of the test -f ...
static command you can use eg. path/to/some/script ...
. With GNU find (the default in Linux) you can use -execdir
instead of -exec
.
add a comment |
Yes, -exec
is a predicate just like -name
, and find
will consider the exit status of the command and treat it as false if it's non-zero.
But notice that a) this only applies to the -exec ... ;
form, not to the -exec .. +
form (which is always true) and b) forking a separate process for each file or directory is slow.
For example this will skip all directories that contain a file named foo.txt
:
find . -type d -exec test -f /foo.txt ; -prune -or -print
$ mkdir -p a/1,2,3/x,y,z; touch a/1,2,3/x,y,z/a.txt
$ find . -type d -exec test -f /foo.txt ; -prune -or -print
... lots of files ...
$ touch a/1,2/foo.txt a/3/x,y/foo.txt
$ find . -type d -exec test -f /foo.txt ; -prune -or -print
.
./a
./a/3
./a/3/z
./a/3/z/a.txt
Of course, instead of the test -f ...
static command you can use eg. path/to/some/script ...
. With GNU find (the default in Linux) you can use -execdir
instead of -exec
.
add a comment |
Yes, -exec
is a predicate just like -name
, and find
will consider the exit status of the command and treat it as false if it's non-zero.
But notice that a) this only applies to the -exec ... ;
form, not to the -exec .. +
form (which is always true) and b) forking a separate process for each file or directory is slow.
For example this will skip all directories that contain a file named foo.txt
:
find . -type d -exec test -f /foo.txt ; -prune -or -print
$ mkdir -p a/1,2,3/x,y,z; touch a/1,2,3/x,y,z/a.txt
$ find . -type d -exec test -f /foo.txt ; -prune -or -print
... lots of files ...
$ touch a/1,2/foo.txt a/3/x,y/foo.txt
$ find . -type d -exec test -f /foo.txt ; -prune -or -print
.
./a
./a/3
./a/3/z
./a/3/z/a.txt
Of course, instead of the test -f ...
static command you can use eg. path/to/some/script ...
. With GNU find (the default in Linux) you can use -execdir
instead of -exec
.
Yes, -exec
is a predicate just like -name
, and find
will consider the exit status of the command and treat it as false if it's non-zero.
But notice that a) this only applies to the -exec ... ;
form, not to the -exec .. +
form (which is always true) and b) forking a separate process for each file or directory is slow.
For example this will skip all directories that contain a file named foo.txt
:
find . -type d -exec test -f /foo.txt ; -prune -or -print
$ mkdir -p a/1,2,3/x,y,z; touch a/1,2,3/x,y,z/a.txt
$ find . -type d -exec test -f /foo.txt ; -prune -or -print
... lots of files ...
$ touch a/1,2/foo.txt a/3/x,y/foo.txt
$ find . -type d -exec test -f /foo.txt ; -prune -or -print
.
./a
./a/3
./a/3/z
./a/3/z/a.txt
Of course, instead of the test -f ...
static command you can use eg. path/to/some/script ...
. With GNU find (the default in Linux) you can use -execdir
instead of -exec
.
edited Apr 15 at 2:45
answered Apr 15 at 2:12
mosvymosvy
10.4k11238
10.4k11238
add a comment |
add a comment |
m.smith7878 is a new contributor. Be nice, and check out our Code of Conduct.
m.smith7878 is a new contributor. Be nice, and check out our Code of Conduct.
m.smith7878 is a new contributor. Be nice, and check out our Code of Conduct.
m.smith7878 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f512466%2flooping-through-all-files-skipping-some-dynamically%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