Chose list of sub directories in a bash script file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Chose list of sub directories in a bash script file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-06-2013
hce hce is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 10 September 2014, 7:45 AM EDT
Posts: 46
Thanks: 22
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
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 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
Last Activity: 10 September 2014, 7:45 AM EDT
Posts: 46
Thanks: 22
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
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 Thread

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 02:36 AM.