Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-06-2013
hce hce is offline
Registered User
 
Join Date: Jun 2012
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Chose list of sub directories in a bash script file

Hi,

I have a command in my bash script, searchDirectoryName.sh:

Code:
DIR_NAME=$(find . -type d)
echo "${DIR_NAME}"


Code:
.
./Dir1
./Dir1/1.0.2.1
./Dir2
./Dir2/1.1
./Dir3
./Dir3/2.2.1

How can I select only following directory names with second subdirectoies and without first ./ in the DIR_NAME?

Code:
Dir1/1.0.2.1
Dir2/1.1
Dir3/2.2.11

Finally substitute "/" to "_" for each name:

Code:
Dir1_1.0.2.1 
Dir2_1.1
Dir3_2.2.1

Thanks.

Kind regards.

Last edited by Franklin52; 03-07-2013 at 03:02 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old 03-06-2013
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,310
Thanks: 154
Thanked 738 Times in 710 Posts

Code:
for DIR_NAME in $(find . -type d)
do
        if [[ "$DIR_NAME" =~ \/[0-9]\. ]]
        then
                DIR_NAME="${DIR_NAME#\.\/}"
                DIR_NAME="${DIR_NAME/\//_}"
                echo "$DIR_NAME"
        fi
done

Sponsored Links
    #3  
Old 03-06-2013
hce hce is offline
Registered User
 
Join Date: Jun 2012
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by bipinajith View Post
Code:
for DIR_NAME in $(find . -type d)
do
        if [[ "$DIR_NAME" =~ \/[0-9]\. ]]
        then
                DIR_NAME="${DIR_NAME#\.\/}"
                DIR_NAME="${DIR_NAME/\//_}"
                echo "$DIR_NAME"
        fi
done

Thanks bipinajith, terrific.

Cheers.
    #4  
Old 03-07-2013
Registered User
 
Join Date: Jan 2010
Posts: 182
Thanks: 2
Thanked 9 Times in 9 Posts
hope this helps.


Code:
find . -type d |awk '{ gsub(/\.\//,"",$0); gsub(/\//,"_",$0);print }'

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Separating list of input files (*.file) with a comma in bash script TuAd Shell Programming and Scripting 7 05-31-2011 01:35 PM
Bash script: issue changing directories in script Breakology Shell Programming and Scripting 5 05-26-2009 08:47 AM
a remove script taken in input a file which contain a list of directories yeclota Shell Programming and Scripting 2 09-09-2008 10:56 AM
help with script to list directories chassis Shell Programming and Scripting 2 09-13-2006 01:17 PM
Script to list changes in Directories aojmoj Shell Programming and Scripting 2 11-22-2003 09:51 PM



All times are GMT -4. The time now is 09:55 AM.