Drilling down to the last subdirectory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Drilling down to the last subdirectory
# 1  
Old 10-08-2013
Drilling down to the last subdirectory

Within a BASH environment, I need to search through a filesystem looking for the last subdirectory. Once the last subdirectory of the filesystem is found, I need to create another directory within it:

Basic example:
Code:
/u01/data1/project_1/proj_data1/score
/u01/data1/project_1/proj_data1/time/chart
/U01/data1/project_1/proj_data2/score
/U01/data1/project_1/proj_data3/depth/probe
/U01/data1/project_1/proj_data3/all
 
Once I am to the last directory I need to create:
- adminuser
 
So the outcome would be 
 
/u01/data1/project_1/proj_data1/score/adminuser
/u01/data1/project_1/proj_data1/time/chart/adminuser
/U01/data1/project_1/proj_data2/score/adminuser
/U01/data1/project_1/proj_data3/depth/probe/adminuser
/U01/data1/project_1/proj_data3/all/adminuser
 
HOME=”/u01/data1/project_1”

I attempted to perform searches for this scenario within this section, but couldn't find a resolution. Your help would be greatly appreciated.

Many Thanks!
# 2  
Old 10-08-2013
Does it have to be bash? If you can live with Perl solution, then put this into "script.pl":
Code:
#!/usr/ bin/perl
use File::Basename;
use strict;
chomp(my @dirs=`find $ARGV[0] -type d`);
@dirs=sort {length($b) <=> length($a)} @dirs;
my @out;
for my $dir (@dirs) {
  push @out, dirname($dir);
}
for my $dir (@dirs) {
  mkdir "$dir/adminuser" if !grep {$dir eq $_} @out;
}

Then run:
Code:
perl script.pl /u01/data1/project_1

# 3  
Old 10-08-2013
Thanks, unfortunately, I don't know perl. However, I'll definitely play with what you posted.
# 4  
Old 10-08-2013
This prints the deepest directories
Code:
 find . -depth -type d | awk -F / '(NF>=prev) {print "mkdir "$0"/adminuser"} {prev=NF}'

If this is what you want, pipe it to sh

Last edited by MadeInGermany; 10-08-2013 at 07:31 PM.. Reason: bug fix: only the amount of / matters - adminuser not useradmin
# 5  
Old 10-08-2013
That's exactly what I need MadeInGermany. How would I change the print command to an execute command within awk? Thanks!
# 6  
Old 10-08-2013
Hi,

Could you please try following script and let me know if this helps.

Code:
DIRNAME=LAST_dir_test
dir_values=`ls -ltR | grep '^d' | awk '{print$9}'`
FILE_NAME=values_for_files
 
set -A dir_values_array ${dir_values}
 
for i in ${dir_values_array[@]}
do
 
find `pwd` -name $i | grep -v '^$' >> ${FILE_NAME}
 
done
 
while read line
do
cd $line
mkdir test_test
echo "done"
 
done < ${FILE_NAME}


kindly let me know if this helps.


Thanks,
R. Singh
# 7  
Old 10-08-2013
Quote:
Originally Posted by leepet
That's exactly what I need MadeInGermany. How would I change the print command to an execute command within awk? Thanks!
Pipe the printed shell commands to sh:
Code:
find ... | awk ... | sh

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to move to the last subdirectory one by one?

Hi, How can i traverse to the last subfolder in all the directories. eg: i have the below folders structure f1/sf1/r1 f2/sf2/r2 f3/sf3/r3/r4 i need to move to the last directory in each directory. Can anyone tell me a solution for this? I saw an example that does that. find . -type... (2 Replies)
Discussion started by: Little
2 Replies

2. UNIX for Advanced & Expert Users

Selective rsync to a subdirectory

I have a directory which has a collection of directories. I want to transfer all directories starting with `a` through `e` to the directory `ae`. What would be the command to achieve this? (0 Replies)
Discussion started by: kristinu
0 Replies

3. Shell Programming and Scripting

Should be a simple subdirectory check

Ok. Just getting back into PERL and probably (or most definitely) making a mountain out of a mole hill. I'm trying to see if a subdirectory exists, and if not, print the slightly modified path of the missing sub to a file. Sounds simple enough. Well here is my elaborate code. Save the... (2 Replies)
Discussion started by: luvdairish
2 Replies

4. UNIX for Dummies Questions & Answers

How to Find files in subdirectory?

I am trying to find all DAT files in a subdirectory named IN. I do not know the entire path. For example: /stage/<?>/<?>/IN/file.DAT I am using the find command without success: find /stage -name IN -a -name '*.DAT' -print What is the correct logic and syntax? Thank you for the help. (5 Replies)
Discussion started by: TwinGT
5 Replies

5. Shell Programming and Scripting

Pick one file from each subdirectory

Hi, I have a problem I am trying to solve with bash. I need to search in a file system (data base) with hundreds of directories and thousands of subdirectories and millions of files. The files have a specific format with a header that gives the properties. Directories are organized so... (1 Reply)
Discussion started by: majest
1 Replies

6. Shell Programming and Scripting

How to list directory and subdirectory?

Hi, I want to list all the directory and subdirectories under any directory. For eg. i am in a directory called A and want to check all directories under A. Output should be as below. /A /A/a1 /A/a1/a2 /A/b1 /A/c1/c2 A,a1,a2,b1,c1 and c2 all are directories.Just for Eg. Please... (7 Replies)
Discussion started by: anshu ranjan
7 Replies

7. Shell Programming and Scripting

Regex to remove subdirectory

I need to remove subdirectories that are empty and I've not done this before. First I am going through the files to remove old records. Then if the directory is empty I want to delete it. There are files in /direcotry/images/fs* - 0-9 and a-z The fs* directories need to stay, but any directories... (4 Replies)
Discussion started by: janel10
4 Replies

8. Shell Programming and Scripting

Comparing subdirectory names

I am trying to reformat data from one private directory and reformat it and move it to a public one, but i only want to get directories that have not already been moved to the public directory. Here's what i'm working with Dir1 contains folders for each named with timestamp 20090320081302... (2 Replies)
Discussion started by: dabombace
2 Replies

9. UNIX for Dummies Questions & Answers

How can I copy files and subdirectory?

I am trying to copy some files from one location to another and I need to write a script to move all the files and the subdirectories to the new location (both unix), but excluding the temp directory. i.e., I want to avoid copying my temp subdirectories during the process of copying other files... (3 Replies)
Discussion started by: odogbolu98
3 Replies

10. UNIX for Dummies Questions & Answers

subdirectory password

Hi! I am a user of a workstation. I have got my own previlages on my system. Still I would like to have a security for my data. Can I set password or write a script that the entry to a particular subdirectiry is restricted?:confused: (1 Reply)
Discussion started by: sskb
1 Replies
Login or Register to Ask a Question