Find exclude path containing specific files
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I am trying to find all directories in a folder recursively while exclude all git submodules by excluding all path containing .git
file. How could I do it?
Explanation:
.git
file exists at the root of every submodules folder. This submodule folder could be included anywhere.
Test Case
$ mkdir Test
$ cd Test
$ mkdir a
$ mkdir b
$ mkdir c
$ cd a
$ mkdir .git
$ cd ..
$ cd b
$ touch .git
$ cd ..
$ cd c
$ mkdir c1
$ mkdir c2
$ cd..
$ find . -type d ( ( ! -name . -exec [ -e /.git ] ; -prune ) -o ( (
-name .git
-o -name .vscode
-o -name node_modules
-o -name Image
-o -name Rendered
-o -name iNotebook
-o -name GeneratedTest
-o -name GeneratedOutput
) -prune ) -o -print ) | sort
Expected Results
.
./a
./c
./c/c1
./c/c2
find directory path git
add a comment |Â
up vote
3
down vote
favorite
I am trying to find all directories in a folder recursively while exclude all git submodules by excluding all path containing .git
file. How could I do it?
Explanation:
.git
file exists at the root of every submodules folder. This submodule folder could be included anywhere.
Test Case
$ mkdir Test
$ cd Test
$ mkdir a
$ mkdir b
$ mkdir c
$ cd a
$ mkdir .git
$ cd ..
$ cd b
$ touch .git
$ cd ..
$ cd c
$ mkdir c1
$ mkdir c2
$ cd..
$ find . -type d ( ( ! -name . -exec [ -e /.git ] ; -prune ) -o ( (
-name .git
-o -name .vscode
-o -name node_modules
-o -name Image
-o -name Rendered
-o -name iNotebook
-o -name GeneratedTest
-o -name GeneratedOutput
) -prune ) -o -print ) | sort
Expected Results
.
./a
./c
./c/c1
./c/c2
find directory path git
No I meant .git files as that is the case for submodules
â Nikhil
Sep 13 at 8:44
excluding .git folder is not a problem as it can be easily done like thisfind "$(pwd)" -not ( -path "*/.git"` ) -type d
â Nikhil
Sep 13 at 8:46
.git
file exist at the root of every submodules folder. This submodule folder could be included anywhere
â Nikhil
Sep 13 at 8:47
But I thought you want to exclude folders containing .git... So your find doesn't work for that even if its a folder.
â RoVo
Sep 13 at 8:48
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to find all directories in a folder recursively while exclude all git submodules by excluding all path containing .git
file. How could I do it?
Explanation:
.git
file exists at the root of every submodules folder. This submodule folder could be included anywhere.
Test Case
$ mkdir Test
$ cd Test
$ mkdir a
$ mkdir b
$ mkdir c
$ cd a
$ mkdir .git
$ cd ..
$ cd b
$ touch .git
$ cd ..
$ cd c
$ mkdir c1
$ mkdir c2
$ cd..
$ find . -type d ( ( ! -name . -exec [ -e /.git ] ; -prune ) -o ( (
-name .git
-o -name .vscode
-o -name node_modules
-o -name Image
-o -name Rendered
-o -name iNotebook
-o -name GeneratedTest
-o -name GeneratedOutput
) -prune ) -o -print ) | sort
Expected Results
.
./a
./c
./c/c1
./c/c2
find directory path git
I am trying to find all directories in a folder recursively while exclude all git submodules by excluding all path containing .git
file. How could I do it?
Explanation:
.git
file exists at the root of every submodules folder. This submodule folder could be included anywhere.
Test Case
$ mkdir Test
$ cd Test
$ mkdir a
$ mkdir b
$ mkdir c
$ cd a
$ mkdir .git
$ cd ..
$ cd b
$ touch .git
$ cd ..
$ cd c
$ mkdir c1
$ mkdir c2
$ cd..
$ find . -type d ( ( ! -name . -exec [ -e /.git ] ; -prune ) -o ( (
-name .git
-o -name .vscode
-o -name node_modules
-o -name Image
-o -name Rendered
-o -name iNotebook
-o -name GeneratedTest
-o -name GeneratedOutput
) -prune ) -o -print ) | sort
Expected Results
.
./a
./c
./c/c1
./c/c2
find directory path git
find directory path git
edited Sep 15 at 19:37
asked Sep 13 at 8:01
Nikhil
1799
1799
No I meant .git files as that is the case for submodules
â Nikhil
Sep 13 at 8:44
excluding .git folder is not a problem as it can be easily done like thisfind "$(pwd)" -not ( -path "*/.git"` ) -type d
â Nikhil
Sep 13 at 8:46
.git
file exist at the root of every submodules folder. This submodule folder could be included anywhere
â Nikhil
Sep 13 at 8:47
But I thought you want to exclude folders containing .git... So your find doesn't work for that even if its a folder.
â RoVo
Sep 13 at 8:48
add a comment |Â
No I meant .git files as that is the case for submodules
â Nikhil
Sep 13 at 8:44
excluding .git folder is not a problem as it can be easily done like thisfind "$(pwd)" -not ( -path "*/.git"` ) -type d
â Nikhil
Sep 13 at 8:46
.git
file exist at the root of every submodules folder. This submodule folder could be included anywhere
â Nikhil
Sep 13 at 8:47
But I thought you want to exclude folders containing .git... So your find doesn't work for that even if its a folder.
â RoVo
Sep 13 at 8:48
No I meant .git files as that is the case for submodules
â Nikhil
Sep 13 at 8:44
No I meant .git files as that is the case for submodules
â Nikhil
Sep 13 at 8:44
excluding .git folder is not a problem as it can be easily done like this
find "$(pwd)" -not ( -path "*/.git"` ) -type d
â Nikhil
Sep 13 at 8:46
excluding .git folder is not a problem as it can be easily done like this
find "$(pwd)" -not ( -path "*/.git"` ) -type d
â Nikhil
Sep 13 at 8:46
.git
file exist at the root of every submodules folder. This submodule folder could be included anywhereâ Nikhil
Sep 13 at 8:47
.git
file exist at the root of every submodules folder. This submodule folder could be included anywhereâ Nikhil
Sep 13 at 8:47
But I thought you want to exclude folders containing .git... So your find doesn't work for that even if its a folder.
â RoVo
Sep 13 at 8:48
But I thought you want to exclude folders containing .git... So your find doesn't work for that even if its a folder.
â RoVo
Sep 13 at 8:48
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
accepted
find
actions are also tests, so you can add tests using -exec
:
find . ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print
This applies three sets of actions:
-exec [ -f /.git ] ; -prune
prunes directories containing a file named.git
-name .git -prune
prunes directories named.git
(so the command doesnâÂÂt search inside the main.git
directory of a repository)-print
prints anything which isnâÂÂt caught by the above.
To only match directories, add -type d
, either just before -print
, or (to save time processing files):
find . -type d ( ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print )
This also works when run this on a directory other than .
, by changing the find
start path:
find /some/other/path -type d ( ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print )
1
This is really cool. And I thought my solution is clever ;-)
â RoVo
Sep 13 at 9:13
@Stephen Kitt You mentioned-name .git -prune
to exclude directories. Is it better to use-not -path "*/.git/*"
â Nikhil
Sep 13 at 9:39
1
@Nikhil-not -path "*/.git/*"
checks all the files and directories inside.git
, which can take a little while, whereas-name .git -prune
avoids descending into the directory at all; the latter is more efficient.
â Stephen Kitt
Sep 13 at 9:49
1
@Nikhil no:-print
is the default action, used when no other expression is given. Here thefind
command includes other expressions (-exec
,-prune
, and-name
) so we need to specify-print
explicitly. Try removing-print
: thefind
command wonâÂÂt output anything.find .
andfind . -print
are equivalent as complete commands, not as portions of commands.
â Stephen Kitt
Sep 16 at 13:14
1
@Nikhil your very first test in that command limits everything else to files, so youâÂÂll never prune any directory. You need to split the tests up to prune directories and then look forindex.md
:find . ( -type d ( -exec [ -f /.git ] ; -o -name "Rendered" ) -prune ) -o ( -type f -name index.md -print )
.
â Stephen Kitt
Sep 22 at 23:01
 |Â
show 5 more comments
up vote
4
down vote
We can create a recursive find
:
Add the following lines to a script file:
#!/bin/bash
if [ ! -f "$1"/.git ]; then
echo "$1"
find "$1" -mindepth 1 -type d -prune -exec "$0" ;
fi
I named the file findifnotgit
but it doesn't matter.
Then make it executable
chmod u+x findifnotgit
Then run it with the path you want to run as argument:
./findifnotgit .
--> .
for current dir
or
./findifnotgit /path/to/search/
Explanation:
if [ ! -f "$1"/.git ]; then ... fi
Only run the following when there is not.git
file inside the current folder ($1
)- We need
-mindepth 1
option to let find not find the folder we started with which would create an indefinite loop. - We need
-prune
so that find will not descend into directories. We will do this ourselves inside-exec
. -exec "$0"
will call the same script$0
with the finds.
Is it possible to store the results of find in a file liketemp
â Nikhil
Sep 13 at 9:03
sure, just run./findifnotgit . > temp
â RoVo
Sep 13 at 9:05
Yes, I tried that but all the output lines are prefixed with[3J[H[2J.
and some special characters.
â Nikhil
Sep 13 at 9:09
this seems to be another issue not related to my answer... askubuntu.com/questions/638952/â¦
â RoVo
Sep 13 at 9:12
add a comment |Â
up vote
1
down vote
Here is how to tell find not to look inside the .git or .hg repositories.
find . ( -iname '.git' -o -iname '.hg' ) -prune -false -o -iname '*thing-i-am-looking for*'
1
do you know something I don't know ? what isbookmarker
, why.hg
? Also I thought OP wants to find folders and not anything called*thing-i-am-looking for*
?
â RoVo
Sep 13 at 8:54
Sorry left in file name from test, and I included two things to ignore, so one can see how to extend it.
â ctrl-alt-delor
Sep 13 at 8:57
add a comment |Â
up vote
1
down vote
Bit of a dirty script, but this will find all directories that don't contain a .git
file in them:
#!/bin/bash
# find dirs that contain *.git files and store in array
exclude_dirs=($(find . -type f -iname ".git" | xargs -i dirname ))
# set up the base command
command="find . -type d"
# loop over each element in array by index
for i in $(seq 0 $(expr $#exclude_dirs[@] - 1)); do
command="$command -not -path $exclude_dirs[$i]"
done
# run the find
eval $command
edit:
fixed syntax errors and updated *.git
to .git
edit2:
yep that was wrong, my apologies. Edited so it actually works now.
Indeed very dirty ... and did you try it yourself? It doesn't work as you have syntax errors. Also OP wants to exclude.git
not*.git
.
â RoVo
Sep 13 at 8:42
It doesn't work
â Nikhil
Sep 13 at 8:44
You're right, I'm sorry - I was testing two different things and copied the wrong bit.. I've updated it now and double-tested
â RobotJohnny
Sep 13 at 9:11
Still doesn't work. Please check once more.
â Nikhil
Sep 13 at 9:12
What doesn't work about it for you? are you missing results you expect?
â RobotJohnny
Sep 13 at 9:16
 |Â
show 2 more comments
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
find
actions are also tests, so you can add tests using -exec
:
find . ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print
This applies three sets of actions:
-exec [ -f /.git ] ; -prune
prunes directories containing a file named.git
-name .git -prune
prunes directories named.git
(so the command doesnâÂÂt search inside the main.git
directory of a repository)-print
prints anything which isnâÂÂt caught by the above.
To only match directories, add -type d
, either just before -print
, or (to save time processing files):
find . -type d ( ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print )
This also works when run this on a directory other than .
, by changing the find
start path:
find /some/other/path -type d ( ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print )
1
This is really cool. And I thought my solution is clever ;-)
â RoVo
Sep 13 at 9:13
@Stephen Kitt You mentioned-name .git -prune
to exclude directories. Is it better to use-not -path "*/.git/*"
â Nikhil
Sep 13 at 9:39
1
@Nikhil-not -path "*/.git/*"
checks all the files and directories inside.git
, which can take a little while, whereas-name .git -prune
avoids descending into the directory at all; the latter is more efficient.
â Stephen Kitt
Sep 13 at 9:49
1
@Nikhil no:-print
is the default action, used when no other expression is given. Here thefind
command includes other expressions (-exec
,-prune
, and-name
) so we need to specify-print
explicitly. Try removing-print
: thefind
command wonâÂÂt output anything.find .
andfind . -print
are equivalent as complete commands, not as portions of commands.
â Stephen Kitt
Sep 16 at 13:14
1
@Nikhil your very first test in that command limits everything else to files, so youâÂÂll never prune any directory. You need to split the tests up to prune directories and then look forindex.md
:find . ( -type d ( -exec [ -f /.git ] ; -o -name "Rendered" ) -prune ) -o ( -type f -name index.md -print )
.
â Stephen Kitt
Sep 22 at 23:01
 |Â
show 5 more comments
up vote
6
down vote
accepted
find
actions are also tests, so you can add tests using -exec
:
find . ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print
This applies three sets of actions:
-exec [ -f /.git ] ; -prune
prunes directories containing a file named.git
-name .git -prune
prunes directories named.git
(so the command doesnâÂÂt search inside the main.git
directory of a repository)-print
prints anything which isnâÂÂt caught by the above.
To only match directories, add -type d
, either just before -print
, or (to save time processing files):
find . -type d ( ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print )
This also works when run this on a directory other than .
, by changing the find
start path:
find /some/other/path -type d ( ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print )
1
This is really cool. And I thought my solution is clever ;-)
â RoVo
Sep 13 at 9:13
@Stephen Kitt You mentioned-name .git -prune
to exclude directories. Is it better to use-not -path "*/.git/*"
â Nikhil
Sep 13 at 9:39
1
@Nikhil-not -path "*/.git/*"
checks all the files and directories inside.git
, which can take a little while, whereas-name .git -prune
avoids descending into the directory at all; the latter is more efficient.
â Stephen Kitt
Sep 13 at 9:49
1
@Nikhil no:-print
is the default action, used when no other expression is given. Here thefind
command includes other expressions (-exec
,-prune
, and-name
) so we need to specify-print
explicitly. Try removing-print
: thefind
command wonâÂÂt output anything.find .
andfind . -print
are equivalent as complete commands, not as portions of commands.
â Stephen Kitt
Sep 16 at 13:14
1
@Nikhil your very first test in that command limits everything else to files, so youâÂÂll never prune any directory. You need to split the tests up to prune directories and then look forindex.md
:find . ( -type d ( -exec [ -f /.git ] ; -o -name "Rendered" ) -prune ) -o ( -type f -name index.md -print )
.
â Stephen Kitt
Sep 22 at 23:01
 |Â
show 5 more comments
up vote
6
down vote
accepted
up vote
6
down vote
accepted
find
actions are also tests, so you can add tests using -exec
:
find . ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print
This applies three sets of actions:
-exec [ -f /.git ] ; -prune
prunes directories containing a file named.git
-name .git -prune
prunes directories named.git
(so the command doesnâÂÂt search inside the main.git
directory of a repository)-print
prints anything which isnâÂÂt caught by the above.
To only match directories, add -type d
, either just before -print
, or (to save time processing files):
find . -type d ( ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print )
This also works when run this on a directory other than .
, by changing the find
start path:
find /some/other/path -type d ( ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print )
find
actions are also tests, so you can add tests using -exec
:
find . ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print
This applies three sets of actions:
-exec [ -f /.git ] ; -prune
prunes directories containing a file named.git
-name .git -prune
prunes directories named.git
(so the command doesnâÂÂt search inside the main.git
directory of a repository)-print
prints anything which isnâÂÂt caught by the above.
To only match directories, add -type d
, either just before -print
, or (to save time processing files):
find . -type d ( ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print )
This also works when run this on a directory other than .
, by changing the find
start path:
find /some/other/path -type d ( ( -exec [ -f /.git ] ; -prune ) -o ( -name .git -prune ) -o -print )
edited Sep 15 at 20:57
answered Sep 13 at 9:08
Stephen Kitt
148k22324393
148k22324393
1
This is really cool. And I thought my solution is clever ;-)
â RoVo
Sep 13 at 9:13
@Stephen Kitt You mentioned-name .git -prune
to exclude directories. Is it better to use-not -path "*/.git/*"
â Nikhil
Sep 13 at 9:39
1
@Nikhil-not -path "*/.git/*"
checks all the files and directories inside.git
, which can take a little while, whereas-name .git -prune
avoids descending into the directory at all; the latter is more efficient.
â Stephen Kitt
Sep 13 at 9:49
1
@Nikhil no:-print
is the default action, used when no other expression is given. Here thefind
command includes other expressions (-exec
,-prune
, and-name
) so we need to specify-print
explicitly. Try removing-print
: thefind
command wonâÂÂt output anything.find .
andfind . -print
are equivalent as complete commands, not as portions of commands.
â Stephen Kitt
Sep 16 at 13:14
1
@Nikhil your very first test in that command limits everything else to files, so youâÂÂll never prune any directory. You need to split the tests up to prune directories and then look forindex.md
:find . ( -type d ( -exec [ -f /.git ] ; -o -name "Rendered" ) -prune ) -o ( -type f -name index.md -print )
.
â Stephen Kitt
Sep 22 at 23:01
 |Â
show 5 more comments
1
This is really cool. And I thought my solution is clever ;-)
â RoVo
Sep 13 at 9:13
@Stephen Kitt You mentioned-name .git -prune
to exclude directories. Is it better to use-not -path "*/.git/*"
â Nikhil
Sep 13 at 9:39
1
@Nikhil-not -path "*/.git/*"
checks all the files and directories inside.git
, which can take a little while, whereas-name .git -prune
avoids descending into the directory at all; the latter is more efficient.
â Stephen Kitt
Sep 13 at 9:49
1
@Nikhil no:-print
is the default action, used when no other expression is given. Here thefind
command includes other expressions (-exec
,-prune
, and-name
) so we need to specify-print
explicitly. Try removing-print
: thefind
command wonâÂÂt output anything.find .
andfind . -print
are equivalent as complete commands, not as portions of commands.
â Stephen Kitt
Sep 16 at 13:14
1
@Nikhil your very first test in that command limits everything else to files, so youâÂÂll never prune any directory. You need to split the tests up to prune directories and then look forindex.md
:find . ( -type d ( -exec [ -f /.git ] ; -o -name "Rendered" ) -prune ) -o ( -type f -name index.md -print )
.
â Stephen Kitt
Sep 22 at 23:01
1
1
This is really cool. And I thought my solution is clever ;-)
â RoVo
Sep 13 at 9:13
This is really cool. And I thought my solution is clever ;-)
â RoVo
Sep 13 at 9:13
@Stephen Kitt You mentioned
-name .git -prune
to exclude directories. Is it better to use -not -path "*/.git/*"
â Nikhil
Sep 13 at 9:39
@Stephen Kitt You mentioned
-name .git -prune
to exclude directories. Is it better to use -not -path "*/.git/*"
â Nikhil
Sep 13 at 9:39
1
1
@Nikhil
-not -path "*/.git/*"
checks all the files and directories inside .git
, which can take a little while, whereas -name .git -prune
avoids descending into the directory at all; the latter is more efficient.â Stephen Kitt
Sep 13 at 9:49
@Nikhil
-not -path "*/.git/*"
checks all the files and directories inside .git
, which can take a little while, whereas -name .git -prune
avoids descending into the directory at all; the latter is more efficient.â Stephen Kitt
Sep 13 at 9:49
1
1
@Nikhil no:
-print
is the default action, used when no other expression is given. Here the find
command includes other expressions (-exec
, -prune
, and -name
) so we need to specify -print
explicitly. Try removing -print
: the find
command wonâÂÂt output anything. find .
and find . -print
are equivalent as complete commands, not as portions of commands.â Stephen Kitt
Sep 16 at 13:14
@Nikhil no:
-print
is the default action, used when no other expression is given. Here the find
command includes other expressions (-exec
, -prune
, and -name
) so we need to specify -print
explicitly. Try removing -print
: the find
command wonâÂÂt output anything. find .
and find . -print
are equivalent as complete commands, not as portions of commands.â Stephen Kitt
Sep 16 at 13:14
1
1
@Nikhil your very first test in that command limits everything else to files, so youâÂÂll never prune any directory. You need to split the tests up to prune directories and then look for
index.md
: find . ( -type d ( -exec [ -f /.git ] ; -o -name "Rendered" ) -prune ) -o ( -type f -name index.md -print )
.â Stephen Kitt
Sep 22 at 23:01
@Nikhil your very first test in that command limits everything else to files, so youâÂÂll never prune any directory. You need to split the tests up to prune directories and then look for
index.md
: find . ( -type d ( -exec [ -f /.git ] ; -o -name "Rendered" ) -prune ) -o ( -type f -name index.md -print )
.â Stephen Kitt
Sep 22 at 23:01
 |Â
show 5 more comments
up vote
4
down vote
We can create a recursive find
:
Add the following lines to a script file:
#!/bin/bash
if [ ! -f "$1"/.git ]; then
echo "$1"
find "$1" -mindepth 1 -type d -prune -exec "$0" ;
fi
I named the file findifnotgit
but it doesn't matter.
Then make it executable
chmod u+x findifnotgit
Then run it with the path you want to run as argument:
./findifnotgit .
--> .
for current dir
or
./findifnotgit /path/to/search/
Explanation:
if [ ! -f "$1"/.git ]; then ... fi
Only run the following when there is not.git
file inside the current folder ($1
)- We need
-mindepth 1
option to let find not find the folder we started with which would create an indefinite loop. - We need
-prune
so that find will not descend into directories. We will do this ourselves inside-exec
. -exec "$0"
will call the same script$0
with the finds.
Is it possible to store the results of find in a file liketemp
â Nikhil
Sep 13 at 9:03
sure, just run./findifnotgit . > temp
â RoVo
Sep 13 at 9:05
Yes, I tried that but all the output lines are prefixed with[3J[H[2J.
and some special characters.
â Nikhil
Sep 13 at 9:09
this seems to be another issue not related to my answer... askubuntu.com/questions/638952/â¦
â RoVo
Sep 13 at 9:12
add a comment |Â
up vote
4
down vote
We can create a recursive find
:
Add the following lines to a script file:
#!/bin/bash
if [ ! -f "$1"/.git ]; then
echo "$1"
find "$1" -mindepth 1 -type d -prune -exec "$0" ;
fi
I named the file findifnotgit
but it doesn't matter.
Then make it executable
chmod u+x findifnotgit
Then run it with the path you want to run as argument:
./findifnotgit .
--> .
for current dir
or
./findifnotgit /path/to/search/
Explanation:
if [ ! -f "$1"/.git ]; then ... fi
Only run the following when there is not.git
file inside the current folder ($1
)- We need
-mindepth 1
option to let find not find the folder we started with which would create an indefinite loop. - We need
-prune
so that find will not descend into directories. We will do this ourselves inside-exec
. -exec "$0"
will call the same script$0
with the finds.
Is it possible to store the results of find in a file liketemp
â Nikhil
Sep 13 at 9:03
sure, just run./findifnotgit . > temp
â RoVo
Sep 13 at 9:05
Yes, I tried that but all the output lines are prefixed with[3J[H[2J.
and some special characters.
â Nikhil
Sep 13 at 9:09
this seems to be another issue not related to my answer... askubuntu.com/questions/638952/â¦
â RoVo
Sep 13 at 9:12
add a comment |Â
up vote
4
down vote
up vote
4
down vote
We can create a recursive find
:
Add the following lines to a script file:
#!/bin/bash
if [ ! -f "$1"/.git ]; then
echo "$1"
find "$1" -mindepth 1 -type d -prune -exec "$0" ;
fi
I named the file findifnotgit
but it doesn't matter.
Then make it executable
chmod u+x findifnotgit
Then run it with the path you want to run as argument:
./findifnotgit .
--> .
for current dir
or
./findifnotgit /path/to/search/
Explanation:
if [ ! -f "$1"/.git ]; then ... fi
Only run the following when there is not.git
file inside the current folder ($1
)- We need
-mindepth 1
option to let find not find the folder we started with which would create an indefinite loop. - We need
-prune
so that find will not descend into directories. We will do this ourselves inside-exec
. -exec "$0"
will call the same script$0
with the finds.
We can create a recursive find
:
Add the following lines to a script file:
#!/bin/bash
if [ ! -f "$1"/.git ]; then
echo "$1"
find "$1" -mindepth 1 -type d -prune -exec "$0" ;
fi
I named the file findifnotgit
but it doesn't matter.
Then make it executable
chmod u+x findifnotgit
Then run it with the path you want to run as argument:
./findifnotgit .
--> .
for current dir
or
./findifnotgit /path/to/search/
Explanation:
if [ ! -f "$1"/.git ]; then ... fi
Only run the following when there is not.git
file inside the current folder ($1
)- We need
-mindepth 1
option to let find not find the folder we started with which would create an indefinite loop. - We need
-prune
so that find will not descend into directories. We will do this ourselves inside-exec
. -exec "$0"
will call the same script$0
with the finds.
edited Sep 13 at 9:04
answered Sep 13 at 8:31
RoVo
1,800213
1,800213
Is it possible to store the results of find in a file liketemp
â Nikhil
Sep 13 at 9:03
sure, just run./findifnotgit . > temp
â RoVo
Sep 13 at 9:05
Yes, I tried that but all the output lines are prefixed with[3J[H[2J.
and some special characters.
â Nikhil
Sep 13 at 9:09
this seems to be another issue not related to my answer... askubuntu.com/questions/638952/â¦
â RoVo
Sep 13 at 9:12
add a comment |Â
Is it possible to store the results of find in a file liketemp
â Nikhil
Sep 13 at 9:03
sure, just run./findifnotgit . > temp
â RoVo
Sep 13 at 9:05
Yes, I tried that but all the output lines are prefixed with[3J[H[2J.
and some special characters.
â Nikhil
Sep 13 at 9:09
this seems to be another issue not related to my answer... askubuntu.com/questions/638952/â¦
â RoVo
Sep 13 at 9:12
Is it possible to store the results of find in a file like
temp
â Nikhil
Sep 13 at 9:03
Is it possible to store the results of find in a file like
temp
â Nikhil
Sep 13 at 9:03
sure, just run
./findifnotgit . > temp
â RoVo
Sep 13 at 9:05
sure, just run
./findifnotgit . > temp
â RoVo
Sep 13 at 9:05
Yes, I tried that but all the output lines are prefixed with
[3J[H[2J.
and some special characters.â Nikhil
Sep 13 at 9:09
Yes, I tried that but all the output lines are prefixed with
[3J[H[2J.
and some special characters.â Nikhil
Sep 13 at 9:09
this seems to be another issue not related to my answer... askubuntu.com/questions/638952/â¦
â RoVo
Sep 13 at 9:12
this seems to be another issue not related to my answer... askubuntu.com/questions/638952/â¦
â RoVo
Sep 13 at 9:12
add a comment |Â
up vote
1
down vote
Here is how to tell find not to look inside the .git or .hg repositories.
find . ( -iname '.git' -o -iname '.hg' ) -prune -false -o -iname '*thing-i-am-looking for*'
1
do you know something I don't know ? what isbookmarker
, why.hg
? Also I thought OP wants to find folders and not anything called*thing-i-am-looking for*
?
â RoVo
Sep 13 at 8:54
Sorry left in file name from test, and I included two things to ignore, so one can see how to extend it.
â ctrl-alt-delor
Sep 13 at 8:57
add a comment |Â
up vote
1
down vote
Here is how to tell find not to look inside the .git or .hg repositories.
find . ( -iname '.git' -o -iname '.hg' ) -prune -false -o -iname '*thing-i-am-looking for*'
1
do you know something I don't know ? what isbookmarker
, why.hg
? Also I thought OP wants to find folders and not anything called*thing-i-am-looking for*
?
â RoVo
Sep 13 at 8:54
Sorry left in file name from test, and I included two things to ignore, so one can see how to extend it.
â ctrl-alt-delor
Sep 13 at 8:57
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Here is how to tell find not to look inside the .git or .hg repositories.
find . ( -iname '.git' -o -iname '.hg' ) -prune -false -o -iname '*thing-i-am-looking for*'
Here is how to tell find not to look inside the .git or .hg repositories.
find . ( -iname '.git' -o -iname '.hg' ) -prune -false -o -iname '*thing-i-am-looking for*'
edited Sep 13 at 8:56
answered Sep 13 at 8:51
ctrl-alt-delor
9,20431948
9,20431948
1
do you know something I don't know ? what isbookmarker
, why.hg
? Also I thought OP wants to find folders and not anything called*thing-i-am-looking for*
?
â RoVo
Sep 13 at 8:54
Sorry left in file name from test, and I included two things to ignore, so one can see how to extend it.
â ctrl-alt-delor
Sep 13 at 8:57
add a comment |Â
1
do you know something I don't know ? what isbookmarker
, why.hg
? Also I thought OP wants to find folders and not anything called*thing-i-am-looking for*
?
â RoVo
Sep 13 at 8:54
Sorry left in file name from test, and I included two things to ignore, so one can see how to extend it.
â ctrl-alt-delor
Sep 13 at 8:57
1
1
do you know something I don't know ? what is
bookmarker
, why .hg
? Also I thought OP wants to find folders and not anything called *thing-i-am-looking for*
?â RoVo
Sep 13 at 8:54
do you know something I don't know ? what is
bookmarker
, why .hg
? Also I thought OP wants to find folders and not anything called *thing-i-am-looking for*
?â RoVo
Sep 13 at 8:54
Sorry left in file name from test, and I included two things to ignore, so one can see how to extend it.
â ctrl-alt-delor
Sep 13 at 8:57
Sorry left in file name from test, and I included two things to ignore, so one can see how to extend it.
â ctrl-alt-delor
Sep 13 at 8:57
add a comment |Â
up vote
1
down vote
Bit of a dirty script, but this will find all directories that don't contain a .git
file in them:
#!/bin/bash
# find dirs that contain *.git files and store in array
exclude_dirs=($(find . -type f -iname ".git" | xargs -i dirname ))
# set up the base command
command="find . -type d"
# loop over each element in array by index
for i in $(seq 0 $(expr $#exclude_dirs[@] - 1)); do
command="$command -not -path $exclude_dirs[$i]"
done
# run the find
eval $command
edit:
fixed syntax errors and updated *.git
to .git
edit2:
yep that was wrong, my apologies. Edited so it actually works now.
Indeed very dirty ... and did you try it yourself? It doesn't work as you have syntax errors. Also OP wants to exclude.git
not*.git
.
â RoVo
Sep 13 at 8:42
It doesn't work
â Nikhil
Sep 13 at 8:44
You're right, I'm sorry - I was testing two different things and copied the wrong bit.. I've updated it now and double-tested
â RobotJohnny
Sep 13 at 9:11
Still doesn't work. Please check once more.
â Nikhil
Sep 13 at 9:12
What doesn't work about it for you? are you missing results you expect?
â RobotJohnny
Sep 13 at 9:16
 |Â
show 2 more comments
up vote
1
down vote
Bit of a dirty script, but this will find all directories that don't contain a .git
file in them:
#!/bin/bash
# find dirs that contain *.git files and store in array
exclude_dirs=($(find . -type f -iname ".git" | xargs -i dirname ))
# set up the base command
command="find . -type d"
# loop over each element in array by index
for i in $(seq 0 $(expr $#exclude_dirs[@] - 1)); do
command="$command -not -path $exclude_dirs[$i]"
done
# run the find
eval $command
edit:
fixed syntax errors and updated *.git
to .git
edit2:
yep that was wrong, my apologies. Edited so it actually works now.
Indeed very dirty ... and did you try it yourself? It doesn't work as you have syntax errors. Also OP wants to exclude.git
not*.git
.
â RoVo
Sep 13 at 8:42
It doesn't work
â Nikhil
Sep 13 at 8:44
You're right, I'm sorry - I was testing two different things and copied the wrong bit.. I've updated it now and double-tested
â RobotJohnny
Sep 13 at 9:11
Still doesn't work. Please check once more.
â Nikhil
Sep 13 at 9:12
What doesn't work about it for you? are you missing results you expect?
â RobotJohnny
Sep 13 at 9:16
 |Â
show 2 more comments
up vote
1
down vote
up vote
1
down vote
Bit of a dirty script, but this will find all directories that don't contain a .git
file in them:
#!/bin/bash
# find dirs that contain *.git files and store in array
exclude_dirs=($(find . -type f -iname ".git" | xargs -i dirname ))
# set up the base command
command="find . -type d"
# loop over each element in array by index
for i in $(seq 0 $(expr $#exclude_dirs[@] - 1)); do
command="$command -not -path $exclude_dirs[$i]"
done
# run the find
eval $command
edit:
fixed syntax errors and updated *.git
to .git
edit2:
yep that was wrong, my apologies. Edited so it actually works now.
Bit of a dirty script, but this will find all directories that don't contain a .git
file in them:
#!/bin/bash
# find dirs that contain *.git files and store in array
exclude_dirs=($(find . -type f -iname ".git" | xargs -i dirname ))
# set up the base command
command="find . -type d"
# loop over each element in array by index
for i in $(seq 0 $(expr $#exclude_dirs[@] - 1)); do
command="$command -not -path $exclude_dirs[$i]"
done
# run the find
eval $command
edit:
fixed syntax errors and updated *.git
to .git
edit2:
yep that was wrong, my apologies. Edited so it actually works now.
edited Sep 13 at 9:23
answered Sep 13 at 8:33
RobotJohnny
704216
704216
Indeed very dirty ... and did you try it yourself? It doesn't work as you have syntax errors. Also OP wants to exclude.git
not*.git
.
â RoVo
Sep 13 at 8:42
It doesn't work
â Nikhil
Sep 13 at 8:44
You're right, I'm sorry - I was testing two different things and copied the wrong bit.. I've updated it now and double-tested
â RobotJohnny
Sep 13 at 9:11
Still doesn't work. Please check once more.
â Nikhil
Sep 13 at 9:12
What doesn't work about it for you? are you missing results you expect?
â RobotJohnny
Sep 13 at 9:16
 |Â
show 2 more comments
Indeed very dirty ... and did you try it yourself? It doesn't work as you have syntax errors. Also OP wants to exclude.git
not*.git
.
â RoVo
Sep 13 at 8:42
It doesn't work
â Nikhil
Sep 13 at 8:44
You're right, I'm sorry - I was testing two different things and copied the wrong bit.. I've updated it now and double-tested
â RobotJohnny
Sep 13 at 9:11
Still doesn't work. Please check once more.
â Nikhil
Sep 13 at 9:12
What doesn't work about it for you? are you missing results you expect?
â RobotJohnny
Sep 13 at 9:16
Indeed very dirty ... and did you try it yourself? It doesn't work as you have syntax errors. Also OP wants to exclude
.git
not *.git
.â RoVo
Sep 13 at 8:42
Indeed very dirty ... and did you try it yourself? It doesn't work as you have syntax errors. Also OP wants to exclude
.git
not *.git
.â RoVo
Sep 13 at 8:42
It doesn't work
â Nikhil
Sep 13 at 8:44
It doesn't work
â Nikhil
Sep 13 at 8:44
You're right, I'm sorry - I was testing two different things and copied the wrong bit.. I've updated it now and double-tested
â RobotJohnny
Sep 13 at 9:11
You're right, I'm sorry - I was testing two different things and copied the wrong bit.. I've updated it now and double-tested
â RobotJohnny
Sep 13 at 9:11
Still doesn't work. Please check once more.
â Nikhil
Sep 13 at 9:12
Still doesn't work. Please check once more.
â Nikhil
Sep 13 at 9:12
What doesn't work about it for you? are you missing results you expect?
â RobotJohnny
Sep 13 at 9:16
What doesn't work about it for you? are you missing results you expect?
â RobotJohnny
Sep 13 at 9:16
 |Â
show 2 more comments
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468734%2ffind-exclude-path-containing-specific-files%23new-answer', 'question_page');
);
Post as a guest
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
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
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
No I meant .git files as that is the case for submodules
â Nikhil
Sep 13 at 8:44
excluding .git folder is not a problem as it can be easily done like this
find "$(pwd)" -not ( -path "*/.git"` ) -type d
â Nikhil
Sep 13 at 8:46
.git
file exist at the root of every submodules folder. This submodule folder could be included anywhereâ Nikhil
Sep 13 at 8:47
But I thought you want to exclude folders containing .git... So your find doesn't work for that even if its a folder.
â RoVo
Sep 13 at 8:48