linux - How can I find a path by providing only part of the path?
2014-07
Let's say I need to find this path /<some_where_in_root>/find/this/path
and the only information I have is /find/this/path
. What would be the best means of locating the full directory?
I basically have a program that creates a directory and after the directory is created I'd like to see if a path now exist within the directory that was created.
So far I've tried find . -type d -name "/find/this/path"
but this obviously interprets /find/this/path
as a string. Is there any way to use find in this situation? Would it be best to just parse the path I have and take the path
portion and do a search on this string?
You need to use -path
or -ipath
with wildcards
find . -type d -ipath "*/find/this/path"
The -path
predicate (and it's case-insensitive variant, -ipath
) will allow you to search the entire tree for that exact text.
execute these two commands, method1:
root@developer~:# updatedb
root@developer~:# locate /find/this/path
method2:
root@developer~:# tree |grep /find/this/path
the results will consists full path of /find/this/path
also try this run these from /
root@developer~:# cd /
root@developer~:/#updatedb
root@developer~:/#locate /find/this/path
root@developer~:/#tree |grep /find/this/path
works in all flavors of nix:
applemcg.$ find / -type d -name this | grep find/this/path
and a little advice, when asking for help. preface your first reference to a path of this sort with the word "directory", as in "directory path". it took a minute to realize you were not referring to the (csh) path or PATH variable.
I am attempting to create a script that can compress files with a certain extension in a number of directories into a single tar-ball. Currently what I have in the script file is:
find "$rootDir" -name '*doc' -exec tar rvf docs.tar {} \;
Where $rootDir
is the base path to search.
This is fine except the paths are absolute in the tar file. I would prefer the paths to be relative to $rootDir
. How would I go about doing this?
Example of current tar -tf docs.tar
where $rootDir
is /home/username/test
output:
home/username/test/subdir/test.doc
home/username/test/second.doc
What I desire the output to be:
./subdir/test.doc
./second.doc
If you run find
from the desired root directory and don't specify an absolute starting point in find
's options, it will output relative paths to the tar
command invocations it constructs. Like so:
cd $rootDir
find . -name '*doc' -exec tar rvf docs.tar {} \;
If you do not want to change the current working directory permanently and are using bash
or similar as your shell you could do
pushd $rootDir
find . -name '*doc' -exec tar rvf docs.tar {} \;
popd
instead.
Note that pushd/popd are not present in all shells, so check the man page as appropriate. They are present in bash but not in the base sh implementation so while explicitly using /bin/bash
you can rely on them you can't if a script asks for /bin/sh
instead (as this may map to a smaller shell that doesn't have bash's enhancements)