Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello experts!
I would like to find a directory, then once found cd into the found directory - I would really appreciate any help!
Thank you in advance.
Sorry forgot to add - finding the directory(or not) is ok. I've tried putting the find command in an if statement but then how do I get the found directory into a variable for later use.
I'm looking for a directory called inputsheets, then when found cd to it and copy the contents to my destination but I'm struggling with it...
if [[ find $PMPATH -type d name 'inputsheet/' ]]
then cd inputsheet
cp -r /inputshhet *.xml $destination
echo "inputsheets/ copied"
else echo "inputsheets not found"
fi
if [[ find $PMPATH -type d name 'inputsheet/' ]]
here you need not use [[ and ]]
if find $PMPATH -type d name 'inputsheet/';
but anyway you need to handle the case when more than one dir was found.
#copy inputsheets to destination, find directory in case inputsheets not default location
insheetsdir=$(find $PMPATH -type d -name 'inputsheets' 2>/dev/null)
if [[ ! -z '$insheetsdir' ]]
then
cd $insheetsdir
cp *.xml $inputsheetsdest
echo "copying inputsheets"
else echo "inputsheets not found"
fi
It works, but as pan 64 said - how to handle case if more than 1 found ( there should not be more than 1) but what to do?
Note that a $var should be put in "" to avoid unwanted expansions.
The following simply checks the exit status of the cd command.
Code:
insheetsdir=$(find "$PMPATH" -type d -name 'inputsheets' 2>/dev/null)
if [[ -n "$insheetsdir" ]] && cd "$insheetsdir"
then
cp *.xml "$inputsheetsdest"
echo "copying inputsheets"
else
echo "inputsheets not found"
fi
If there are two or more directories then the cd to the combined string (in "") fails, nothing will be copied, and it will say "not found".
simple as this, from the CLI to check it to see if that works first before using it, which it does
Code:
if [[ -d /home ]] ; then echo "here" ; cd /home ; echo $PWD ; else echo "not here" ; fi
here
/home
which translates to
Code:
dir=/he/foo/bar/directory
if [[ -d "$dir" ]] ; then
echo "here"
cd "$dir"
echo $PWD
fi
if you have no idea where the directory is
Code:
#!/bin/env bash
#using a subdir to seek for
seekdir="efi"
while read d
do
# 'd' gives the absolute path to the directory
#using bash subscript '=~' to check for a string within a string
# if mixed case just lower case everything for checking
if [[ "${d,,}" =~ "${seekdir,,}" ]] ; then
cd "$d"
echo "$PWD"
break
fi
done < <(find / -type d -iname "$seekdir" 2>/dev/null)
that was just a little might not apply now but it is useful whenever needed bonus .. no charge .. didn't mean to cause confusion because I did not change the search pattern to show its effects. it too ensures uniformity if you happen to be writing a big script and keep vars on the top then that is covered if you change it and add a cap within in .. etc...
if $d had somewhere within it had a CAP letter well then
A bash alternative using the globstar shell option.
Code:
#!/bin/bash
topdir="$HOME"
inputdir="/**/inputsheets/"
shopt -s globstar
i=0
for d in $topdir$inputdir; do
[ -d "$d" ] && indir[i++]="$d"
done
echo "Found $i directories at:"
for ((j=0;j<i;j++)); do
echo "${indir[j]}"
done
A bash alternative using the globstar shell option.
Code:
#!/bin/bash
topdir="$HOME"
inputdir="/**/inputsheets/"
shopt -s globstar
i=0
for d in $topdir$inputdir; do
[ -d "$d" ] && indir[i++]="$d"
done
echo "Found $i directories at:"
for ((j=0;j<i;j++)); do
echo "${indir[j]}"
done
ah, the globstar kid finally shows up showing his oooouuu la la look at this code ... nice...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.