Chose list of sub directories in a bash script file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Chose list of sub directories in a bash script file
# 1  
Old 03-06-2013
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 04:02 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 03-06-2013
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

# 3  
Old 03-06-2013
Quote:
Originally Posted by bipinajith
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
hope this helps.

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need BASH Script Help to Move Files While Creating Directories

I've got this script to loop through all folders and move files that are more than 2 years old. I'm using the install command because it creates the necessary directories on the destination path and then I remove the source. I'd like to change the script to use the mv command since it is much... (4 Replies)
Discussion started by: consultant
4 Replies

2. Shell Programming and Scripting

Separating list of input files (*.file) with a comma in bash script

Hi all, I'm trying to get a bash script working for a program (bowtie) which takes a list of input files (*.fastq) and assembles them to an output file (outfile.sam). All the .fastq files are in one folder in my home directory (~/infiles). The problem is that the 'bowtie' requires that... (7 Replies)
Discussion started by: TuAd
7 Replies

3. Shell Programming and Scripting

bash script to rename multiple directories

Hello I have a directory structure with year in format 4 digits, e.g 2009, below which is month format 1 or 2 digits, e.g 1 or 12, blow which is day format 1 or 2 digits, e.g 1 or 31. I want to change the names of lots of directories to the be Year - 4 digits , e.g 2009 - No change here... (4 Replies)
Discussion started by: garethsays
4 Replies

4. Shell Programming and Scripting

Bash Script to Read & Write on different directories

Hi, root@server] df -h 121G 14G 101G 12% /home 147G 126G 14G 91% /backup We having our site files and images are storing in /backup/home/user/files/ through symbolic link created in /home directory pointing in /backup directory as following. root@server] cd /home... (1 Reply)
Discussion started by: mirfan
1 Replies

5. Shell Programming and Scripting

Bash script: issue changing directories in script

I am working on a script that checks two arguments at the command line. The first argument is a search pattern, the second can be a file or a directory, if it is a file a second script is called that checks it for the search pattern. If the second argument is a directory, it checks for the search... (5 Replies)
Discussion started by: Breakology
5 Replies

6. Shell Programming and Scripting

a remove script taken in input a file which contain a list of directories

Hi, I'm looking to delete some files from directories. I've just put in a file the location of these files. e.g: in file supprs.txt there is: /usr/host/t1.txt /etc/dev/u1.java /home/new/files/view.c Is it possible to take this file "supprs.txt" as a parameter in a shell command ? (2 Replies)
Discussion started by: yeclota
2 Replies

7. Shell Programming and Scripting

(w)get web server's directories + bash script

Hi everyone. I need to write a script which will download files/folders (a huge collection) to the local file server (centOS 4.4 Server), and check regularly (every 6 hours or so if any new files are present, or if the old ones were modified to update contents). Any insights on how to tackle... (2 Replies)
Discussion started by: reminiscent
2 Replies

8. Shell Programming and Scripting

help with script to list directories

Hi All I have two scripts which i used to try and list all the directories one using 'function', which only lists the first directory and does not show directories within directories. function ListDir () { for arg in $(ls $HOME) do if then echo $arg ... (2 Replies)
Discussion started by: chassis
2 Replies

9. Shell Programming and Scripting

Bash Script to display directories in Path?

Hello there, As a newbie: The directories in PATH can be hard to distinguish when printed out as one line with colon .Please, can i have a sample script to display them,one to a line. Thank you. (1 Reply)
Discussion started by: debut
1 Replies

10. Shell Programming and Scripting

Script to list changes in Directories

Hello guys it's me again, I need some help. What I'm doing is listing all the file and directories Recusively and using it a a master file. Then I need to do the same the nest day to make sure nothing was deleted or modified. What happen is file in one of out major directories was deleted without... (2 Replies)
Discussion started by: aojmoj
2 Replies
Login or Register to Ask a Question