create dir in main &subdirs,perform action


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting create dir in main &subdirs,perform action
# 1  
Old 05-23-2010
MySQL create dir in main &subdirs,perform action

Hi,

I have one dir which has N subdirs.For ex:

/home/user/Project_Src
/home/user/Project_Src/Dir_A
/home/user/Project_Src/Dir_A/subdir/sub_dir2
/home/user/Project_Src/Dir_A/subdir/sub_dir3
/home/user/Project_Src/Dir_B
/home/user/Project_Src/Dir_B/Build

i want to create a folder with the name "NEW" in all the dirs & subdirs under /home/user/Project_Src and perform action (rm -rf .svn) in above mentioned directories.(ie under Project_Src all dirs & subdirs) while performing action i want the directory name in which the action is performed ie echo $dirname.

Result should be with the removal of .svn in all dirs,subdirs recursively:
/home/user/Project_Src/NEW
/home/user/Project_Src/Dir_A/NEW
/home/user/Project_Src/Dir_A/subdir/sub_dir2/NEW
/home/user/Project_Src/Dir_A/subdir/sub_dir3/NEW
/home/user/Project_Src/Dir_B/NEW
/home/user/Project_Src/Dir_B/Build/NEW

i tried to use for loop similar to below mentioned thread:
https://www.unix.com/shell-programmin...ch-folder.html

I got folder only under /Project_Src. I am using bash.

Thanks for all your help.
# 2  
Old 05-23-2010
Quote:
Originally Posted by dragon.1431
Hi,

I have one dir which has N subdirs.For ex:

/home/user/Project_Src
/home/user/Project_Src/Dir_A
/home/user/Project_Src/Dir_A/subdir/sub_dir2
/home/user/Project_Src/Dir_A/subdir/sub_dir3
/home/user/Project_Src/Dir_B
/home/user/Project_Src/Dir_B/Build

i want to create a folder with the name "NEW" in all the dirs & subdirs under /home/user/Project_Src and perform action (rm -rf .svn) in above mentioned directories.(ie under Project_Src all dirs & subdirs) while performing action i want the directory name in which the action is performed ie echo $dirname.

Result should be with the removal of .svn in all dirs,subdirs recursively:
/home/user/Project_Src/NEW
/home/user/Project_Src/Dir_A/NEW
/home/user/Project_Src/Dir_A/subdir/sub_dir2/NEW
/home/user/Project_Src/Dir_A/subdir/sub_dir3/NEW
/home/user/Project_Src/Dir_B/NEW
/home/user/Project_Src/Dir_B/Build/NEW

...
Something like this ?

Code:
$ 
$ 
$ # show the entire file hierarchy from the root directory "Project_Src"
$ find ./Project_Src -name "*"
./Project_Src
./Project_Src/x1.svn
./Project_Src/Dir_B
./Project_Src/Dir_B/x1.svn
./Project_Src/Dir_B/Build
./Project_Src/Dir_B/Build/x1.svn
./Project_Src/Dir_B/Build/y1.txt
./Project_Src/Dir_B/y1.txt
./Project_Src/Dir_A
./Project_Src/Dir_A/x1.svn
./Project_Src/Dir_A/y1.txt
./Project_Src/Dir_A/subdir
./Project_Src/Dir_A/subdir/sub_dir2
./Project_Src/Dir_A/subdir/sub_dir2/x1.svn
./Project_Src/Dir_A/subdir/sub_dir2/y1.txt
./Project_Src/Dir_A/subdir/sub_dir3
./Project_Src/Dir_A/subdir/sub_dir3/x1.svn
./Project_Src/Dir_A/subdir/sub_dir3/y1.txt
./Project_Src/Dir_A/subdir/x1.svn
./Project_Src/Dir_A/subdir/y1.txt
./Project_Src/y1.txt
$ 
$ # show the content of the Bash shell script that processes
$ # all directories in "Project_Src" recursively
$ cat -n processdirs.sh
     1    #!/bin/bash
     2    for i in `find ./Project_Src -type d -name "*"` ; do
     3      echo "Now inspecting directory : $i"
     4      echo "Now removing svn files..."
     5      rm $i/*.svn
     6      echo "Now creating directory NEW..."
     7      mkdir -p $i/NEW
     8      echo "=========================================================================="
     9    done
$ 
$ 
$ # now execute the script "processdirs.sh"
$ ./processdirs.sh
Now inspecting directory : ./Project_Src
Now removing svn files...
Now creating directory NEW...
==========================================================================
Now inspecting directory : ./Project_Src/Dir_B
Now removing svn files...
Now creating directory NEW...
==========================================================================
Now inspecting directory : ./Project_Src/Dir_B/Build
Now removing svn files...
Now creating directory NEW...
==========================================================================
Now inspecting directory : ./Project_Src/Dir_A
Now removing svn files...
Now creating directory NEW...
==========================================================================
Now inspecting directory : ./Project_Src/Dir_A/subdir
Now removing svn files...
Now creating directory NEW...
==========================================================================
Now inspecting directory : ./Project_Src/Dir_A/subdir/sub_dir2
Now removing svn files...
Now creating directory NEW...
==========================================================================
Now inspecting directory : ./Project_Src/Dir_A/subdir/sub_dir3
Now removing svn files...
Now creating directory NEW...
==========================================================================
$ 
$ 
$ # check the entire hierarchy tree again, to ensure that the script worked
$ find ./Project_Src -name "*"
./Project_Src
./Project_Src/Dir_B
./Project_Src/Dir_B/Build
./Project_Src/Dir_B/Build/NEW
./Project_Src/Dir_B/Build/y1.txt
./Project_Src/Dir_B/NEW
./Project_Src/Dir_B/y1.txt
./Project_Src/NEW
./Project_Src/Dir_A
./Project_Src/Dir_A/NEW
./Project_Src/Dir_A/y1.txt
./Project_Src/Dir_A/subdir
./Project_Src/Dir_A/subdir/sub_dir2
./Project_Src/Dir_A/subdir/sub_dir2/NEW
./Project_Src/Dir_A/subdir/sub_dir2/y1.txt
./Project_Src/Dir_A/subdir/sub_dir3
./Project_Src/Dir_A/subdir/sub_dir3/NEW
./Project_Src/Dir_A/subdir/sub_dir3/y1.txt
./Project_Src/Dir_A/subdir/NEW
./Project_Src/Dir_A/subdir/y1.txt
./Project_Src/y1.txt
$ 
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 05-23-2010
I believe .svn are hidden directories used by subversion, not files. If so, rm without -rf won't remove them. Also, if they are directories, since they're going to be nuked, it's best to exclude them from the find traversal to avoid noisy error messages and/or pointless mkdir operations.

Perhaps this will do:
Code:
find /home/user/Project_Src -type d ! \( -name .svn -o -name NEW \) -exec sh -c '
    for d; do
        echo "$d"
        mkdir "$d"/NEW
        rm -fr "$d"/.svn
    done' sh {} +

Regards,
Alister

Last edited by alister; 05-23-2010 at 08:28 PM..
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perform an action if certain text exist in output (PERL)

Hello, I'm attempting to write a tool that checks an IP address for existing PTR records then if there are no PTR records does a ping to see if it response. Then if there is no response, it should print a message saying This is what I have so far. #!/usr/bin/perl $nxdomain =... (4 Replies)
Discussion started by: spartan22
4 Replies

2. UNIX for Dummies Questions & Answers

List files older that 7 days in a dir, excluding all subdirs

Hi, I would like to list all files, older than 7 days, in a directory, but exclude all subdirectories in the find command. If I use find . -type f -mtime +7 all files in the subdirs are also included. How can I exclude them? Regards, JW (6 Replies)
Discussion started by: jwbijl
6 Replies

3. Emergency UNIX and Linux Support

Command to calculate space for all subdirs under a dir

du -hs command calculates the space for all the subdirs under a dir ...but it is very slow if the dir is huge....is there any quick way ...I am using Sun OS. Thanks, Ajay (19 Replies)
Discussion started by: ajaypatil_am
19 Replies

4. Shell Programming and Scripting

Copy files and subdirs from dir to a new dir

Hello Comunity I am trying to make a bash shell script that it copies files and subdirs(with files) to a new dir. I would like the dest_dir to contain only subdirectories with files not other subdirs inside. it called : cpflatdir src_dir dest_dir Pleaze help me! Thank you in... (2 Replies)
Discussion started by: BTKBaaMMM
2 Replies

5. Shell Programming and Scripting

shell script - search a file and perform some action

hi, i have a service on unix platform, it will generate traces in a particular folder i want to check using shell script if traces exist, then perform some action else continue to be in loop. filename is service.tra can you please help? thanks (4 Replies)
Discussion started by: gauravah
4 Replies

6. Shell Programming and Scripting

Perform action in dir if dir has .git subdir

Hi, I want to run git status for the dir which has subdir ".git" in it with dir path mentioned in output log? If a dir does not have .git subdir then skip that dir. Dir will have 100 main dirs & 500 + subdirs and so on. I appreciate all your help :b: (4 Replies)
Discussion started by: dragon.1431
4 Replies

7. Shell Programming and Scripting

How to perform action on newest line in log using tail?

I don't quite know what I'm doing, so this simple script is proving a challenge. Here is some pseudo code that doesn't work yet: if tail -1 "WORKING.txt" >/dev/null | egrep "^NMBR=*" > /dev/null then curl -k 'http://www.myserver.com/log.cgi?input=$?' echo "hi there" fi Purpose:... (3 Replies)
Discussion started by: dihewidd
3 Replies

8. Shell Programming and Scripting

Need help in searching 2 files for strings then perform an action

I have 2 files. I basically want to search both of them to see if the 1st column ($1) matches and if it matches then check to see if the 2nd column ($2) matches, then execute some code showing the results of the matches. File 1: AAA 123 misc blah BBB 456 CCC 789 File 2: ... (2 Replies)
Discussion started by: streetfighter2
2 Replies

9. Shell Programming and Scripting

substrings from all files incl subdirs into csv with dir names

Greetings! I have multiple files, one per subdirectory, all with the same file name. All subdirectories are one level deep from the main directory. The data in the files is tab delimited between fields and record delimited with a newline. The subdirectory names have the date in the... (5 Replies)
Discussion started by: vtischuk@yahoo.
5 Replies

10. Shell Programming and Scripting

Perform action file name written to the pipe

Hello, I have a script that monitors files uploaded via ftp. After a successful upload, the file name is written to the pipe. There is another program that reads this pipe and allows automatically run any program or script ( say test.sh ) to process the newly uploaded file. cat test.sh... (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies
Login or Register to Ask a Question