LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 02-25-2023, 05:58 AM   #1
james.farrow
Member
 
Registered: Mar 2003
Location: UK Darlington
Distribution: Fedora Freebsd Centos
Posts: 296

Rep: Reputation: 31
bash scripting question


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.
 
Old 02-25-2023, 06:00 AM   #2
james.farrow
Member
 
Registered: Mar 2003
Location: UK Darlington
Distribution: Fedora Freebsd Centos
Posts: 296

Original Poster
Rep: Reputation: 31
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.
 
Old 02-25-2023, 06:06 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,182

Rep: Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394
would be nice to show exactly what did you try and we can help you to extend it.
 
Old 02-25-2023, 06:52 AM   #4
james.farrow
Member
 
Registered: Mar 2003
Location: UK Darlington
Distribution: Fedora Freebsd Centos
Posts: 296

Original Poster
Rep: Reputation: 31
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
 
Old 02-25-2023, 06:56 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,843

Rep: Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977
Put the output of the find command in a variable first.
Code:
#!/bin/bash
dir=$(find / -type d -name "some_directory" 2>/dev/null)
if [ ! -z "$dir" ]; then
   echo "$dir found"
else 
   echo "not found"
fi

Last edited by michaelk; 02-25-2023 at 07:04 AM.
 
Old 02-25-2023, 07:04 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,182

Rep: Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394
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.
 
Old 02-25-2023, 07:54 AM   #7
james.farrow
Member
 
Registered: Mar 2003
Location: UK Darlington
Distribution: Fedora Freebsd Centos
Posts: 296

Original Poster
Rep: Reputation: 31
Thanks everyone for your help I now have this

#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?

Thanks again
 
Old 02-25-2023, 08:16 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,843

Rep: Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977
Here is one way.
Code:
  if (( $(echo "$insheetsdir" | wc -l) > 1 )); then
      echo "multiple dirs found"
  fi

Last edited by michaelk; 02-25-2023 at 08:19 AM.
 
Old 02-26-2023, 09:45 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,861

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
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".
 
Old 02-26-2023, 11:57 PM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,182

Rep: Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394
you can also do something like this
Code:
while read -r insheetsdir;
do
   whatever you want
done < <(find ....)
(if you have more than one dir to find)
 
Old 02-27-2023, 07:30 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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)

Last edited by BW-userx; 02-27-2023 at 08:00 AM.
 
Old 02-27-2023, 11:30 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,861

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
I wonder how the
Code:
[[ "${d,,}" =~ "${seekdir,,}" ]]
can ever be false if the input is from
Code:
find / -type d -iname "$seekdir"
 
Old 02-27-2023, 04:56 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by MadeInGermany View Post
I wonder how the
Code:
[[ "${d,,}" =~ "${seekdir,,}" ]]
can ever be false if the input is from
Code:
find / -type d -iname "$seekdir"
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

Last edited by BW-userx; 02-27-2023 at 04:59 PM.
 
Old 02-28-2023, 12:31 AM   #14
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,393

Rep: Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765
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

Last edited by allend; 02-28-2023 at 01:09 AM.
 
Old 02-28-2023, 08:11 AM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by allend View Post
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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Shell Scripting Part I: Getting started with bash scripting LXer Syndicated Linux News 0 04-29-2015 08:03 AM
Bash Scripting – Code Structure - Defining Multiple Points Of Entry In Bash Script carlr Programming 10 08-25-2014 02:38 AM
[To share Bash knowledge]Notes for Advanced Bash-Scripting Version 10 (Latest) jcky Programming 4 07-31-2014 09:24 AM
LXer: Bash If statements, Exit Status and Comparison Operators, A beginners guide to bash scripting LXer Syndicated Linux News 0 06-29-2014 07:35 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration