Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Chose list of sub directories in a bash script file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-06-2013
hce hce is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 26 November 2014, 4:38 AM EST
Posts: 49
Thanks: 24
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 Unix and Linux 03-06-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 20 April 2015, 6:16 PM EDT
Location: Galactic Empire
Posts: 3,402
Thanks: 240
Thanked 1,216 Times in 1,142 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 Unix and Linux 03-06-2013
hce hce is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 26 November 2014, 4:38 AM EST
Posts: 49
Thanks: 24
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 Unix and Linux 03-07-2013
busyboy busyboy is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 19 March 2014, 3:51 AM EDT
Posts: 191
Thanks: 2
Thanked 9 Times in 9 Posts
hope this helps.


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

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 12:03 AM.