Visit Our UNIX and Linux User Community


Shell script for a writing the directory structure to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script for a writing the directory structure to a file
# 1  
Old 03-09-2009
Shell script for a writing the directory structure to a file

Hi All,
I am new user of shell scripting has come up with a problem. that I have a directory structure like :

Home
|
|--------A
| |----trunk
| |-------A_0_1/
| |
| |-------A_0_2/
|
|--------B
| |-----trunk
| |-------B_0_1/
| |
| |-------B_0_2/
|
|--------C
| |-----trunk
| |---------C_0_1/


My problem is to write a script that will generate a file reporting this file hierarchy i.e (The script can be run be from Home)
(Directiry structure states that under Home/ there are 3 subdirectories called A, B, C . Each has another subdirectory called trunk/, all trunk contains the versions of original directory like A_0_1, A_0_2, B_0_1...so on)

$ cat file
--------------
A/trunk/A_0_1
A/trunk/A_0_2
B/trunk/B_0_1
B/trunk/B_0_2
C/trunk/C_0_1
--------------

Please help me regarding this. Thanks in advance.

Last edited by bhaskar_m; 03-09-2009 at 02:02 AM.. Reason: to make the proper understanding of directory structure
# 2  
Old 03-09-2009
If you are working on Linux, Use the following command to list out all the sub directories in a Directory
ls -R|grep :| awk -F':' '{ print $1}'
# 3  
Old 03-09-2009
Thanks for the help..but in my sub directories there are many number of other subdirectories...so the falling command lists out all the directories/subdirectories inside it...I want to restrict it in 3 levels only...i.e Ditrectory/trunk/Directory_version_subversion ....it should not go to the next depth..any specific solution for it?? thanks in advance
# 4  
Old 03-09-2009
ls -R| grep /| awk -F'/' '{ printf("%s/%s/%s\n",$2,$3,$4)}'
# 5  
Old 03-09-2009
thanks it is working now
# 6  
Old 03-09-2009
Quote:
ls -R| grep /| awk -F'/' '{ printf("%s/%s/%s\n",$2,$3,$4)}'
There is no need to use the Recursive option if you only need 3 levels.

Suppose this is the structure (which has more than 3 levels):
Code:
$ find . -print
.
./A
./A/trunk
./A/trunk/A_0_1
./A/trunk/A_0_1/tmp
./A/trunk/A_0_2
./A/trunk/A_0_2/testfile.txt
./B
./B/trunk
./B/trunk/B_0_1
./B/trunk/B_0_1/junk
./B/trunk/B_0_2
./C
./C/trunk
./C/trunk/C_0_1

Only show three levels:

Code:
$ ls -1d */*/*
A/trunk/A_0_1
A/trunk/A_0_2
B/trunk/B_0_1
B/trunk/B_0_2
C/trunk/C_0_1

# 7  
Old 03-09-2009
Excellent now the work is more easy thanks..it seems shell script is a really vast thing full of tricks and tweaks Smilie..thanks once again

Previous Thread | Next Thread
Test Your Knowledge in Computers #389
Difficulty: Medium
Rocky Mountain BASIC (RMB) is a dialect of the BASIC programming language created by AIX.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

2. Shell Programming and Scripting

Sqlplus inside shell script writing to tmp file

Hi, facing an issue while calling sqlplus inside shell script. It for some reason goes to tmp file to write something and i get error as permission denied as i dont have access there. ANy idea why sqlplus writes in /tmp and how to change or stop this ? (2 Replies)
Discussion started by: rushikeshs
2 Replies

3. Shell Programming and Scripting

Script that creates directory structure

Hi All, I have a script that does daily checks on my storage environment and is run from an AIX host. The script currently works great but I have been changing and updating bits of it to make it easier for my lesser colleagues to understand :p However now with the updates I have made I... (1 Reply)
Discussion started by: colinwilson1303
1 Replies

4. Shell Programming and Scripting

How we can create the master file through shell to show the tree structure of the directory?

Can we create the master file that show the whole tree structure of the directory till a particular folder? Database that contains four sub repository Sybase,sql,oracle,mysql and sql and oracle contains two subrepostories Siebel and plsql and each repositories contains three folders... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

5. Shell Programming and Scripting

Problem with writing into a file through shell script

Hi all, I have a shell script which I use to login to the server from the client and then from the server I run a bunch of other scripts to complete my task. I am having problems with the script below- #!/bin/bash while read line do connections=`echo $line | cut -d " " -f 1` period=`echo... (3 Replies)
Discussion started by: joydeep4u
3 Replies

6. Shell Programming and Scripting

Help with writing shell script file

I am trying to prompt the user using tput command to read the information ( 5 last names, first names and grades) from the keyboard. Save the data in a file called student.txt. Sort the file by last name and display it on the screen My pseudocode is as follow: Pseudocode: Initialize... (1 Reply)
Discussion started by: jestaton
1 Replies

7. Shell Programming and Scripting

Shell Script to remove spaces while writing to the file

Hello Folks, I want to get the results from a SQL query which needs to be exported to a .txt file. My Script is something like #!/bin/ksh db2 connect to DATABASE user user_name using pwd; touch test.txt isResult=0; isResult= `db2 -x select 'ABC',COL_B from TABLE_A WHERE COL_B=CONDITION`... (6 Replies)
Discussion started by: dinesh1985
6 Replies

8. UNIX for Dummies Questions & Answers

HELP! writing shell script with c++ file

how would i write a shell script to count number of one-line comments in a c++ file. please help with coding thank you. (1 Reply)
Discussion started by: deadleg
1 Replies

9. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

10. Shell Programming and Scripting

Need script to make big directory structure

Hi, I'm a novice and I'd like to make a directory structure with a hundred or so folders. I've tried mkdir /foo/foo1/etc... mkdir /foo/foo2/etc mkdir /foo/foo3/etc mkdir /foo/foo4/etc ...but it appends '@' to each folder name and then fails on the subdirectories. Is it better to use a... (2 Replies)
Discussion started by: kamur
2 Replies

Featured Tech Videos