Reputation: 2938
I have this script for easy moving into folders.
My problem is, that if I search for tests
but this folder is within my current folder and a subfolder, if the subfolder is searched first, it moves into the tests in the subfolder. But it should always move into the "lowest" match from the subfolder structure. Any ideas?
function f {
if [[ -d $(find . -name $1 -type d) ]]; then
cd $(find . -name $1 -type d)
else
cd $(find ~ -name $1 -type d)
fi
}
Upvotes: 0
Views: 263
Reputation: 11593
Your question is a bit unclear to me. Are just looking for something like
cd "$(find . -depth -type d -name "$1" -print -quit)"
Which will traverse the directory in dfs order and cd
into the first matching $1
it finds.
Upvotes: 0
Reputation: 2337
Try this script:
cd $(for each in `find . -name $1 -type d`
do
cnt=`echo $each | sed 's:[^/]::g' | awk '{print length}'`
echo "$cnt $each"
done | sort -g | awk '{print $2}' | head -1)
find
inside for loop finds all directories with name $1 cnt
variable counts the no. of "/" in the paths.sort -g
sorts the output based on cnt
variable.head -1
returns the first item in the sorted list which would be the "lowest" match.Upvotes: 1