Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a file in all sub-directories if it does not exist 1

Status
Not open for further replies.

himalay

Programmer
Mar 7, 2010
2
US
Hello everyone,

I need to make a task automated using scripting.
I need to search for a file named ".archlock" in all the subdirectories of a directory up to infinite level and then create a file if it does not exist.

until now, I did the following.

1. ls -l|grep ^d | wc -l --> returns number of sub directories in that directory.
2. ls -ltr /*.arch* | wc -l --> verifies how number of sub-dir have that file
3. if the count of step 1 and 2 are not same, I used to go to the individual directory and see if the file exist.

Clearly there must a much better way using scripting, please help.

Thank you,
Himalay
 
One way:
find /path/to/a/directory -type d | while read d; do cat /dev/null >>"$d/.archlock"; done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
find /mydir -type d -exec ksh -c 'test -f $1/.archlock || touch $1/.archlock' create_archlock {} \;
 
And another way:
Code:
for d in `find d1 -type d`
do
touch $d/.archlock
done
 
Thank you for the timely reply. Can anyone of you please direct me to an appropriate tutorial to learn it myself.

I was also trying this
ls -ltr /*.arch* | wc -l --> returns number of sub-dir have file starting with ".arch"

how do I find the number of sub-dir that do not have the file. I tried ! operator but it dint work for me.

Thank you
-Himalay

 
himalay said:
I was also trying this
ls -ltr /*.arch* | wc -l --> returns number of sub-dir have file starting with ".arch"

I don't think it does actually. It will list the contents of directories under root containing the string ".arch", and then count those contents.

I would use something like:

Code:
# count the number of directories under /d1
DIRS=$(find /d1 -type d | wc -l)
# count the number of lock files under /d1
LOCKFILES=$(find /d1 -type f -name .archlock | wc -l)
# subtract the two and print the result
echo "$(( $DIRS-$LOCKFILES )) of $DIRS directories do not contain lock files"

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top