LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script that create a blank file in all sub-directories. (https://www.linuxquestions.org/questions/programming-9/script-that-create-a-blank-file-in-all-sub-directories-705327/)

samengr 02-17-2009 06:30 AM

script that create a blank file in all sub-directories.
 
Hi All,

Can some body write me a script (shell script) which can create a blank test file in all sub-directories upto 5-6 levels?

thanks

datopdog 02-17-2009 06:36 AM

Code:

for name in $(find path_to_base_dir -type d); do
touch $name/file_name
done

That should do it, substitute path_to_base_dir for the actual path and file_name for the name of the file you want created

GazL 02-17-2009 07:00 AM

No need for a script or a loop...

Code:

find . -maxdepth 5 -type d -exec touch {}/testfile \;

jan61 02-17-2009 01:32 PM

Moin,

no need for a bunch of processes ;-)

Code:

find . -maxdepth 5 -type d -printf ">'%p/testfile'\n" | sh
Jan

GazL 02-17-2009 03:47 PM

Quote:

Originally Posted by jan61 (Post 3447283)
Moin,

no need for a bunch of processes ;-)

Code:

find . -maxdepth 5 -type d -printf ">'%p/testfile'\n" | sh
Jan

Show-off! :)

I'm impressed Jan. That's some excellent efficiency going on there. However...

...sooner or later, some nasty git would come along and do something evil like...
Code:

mkdir evildir\'\;rm\ something_important\;\#
;)

jlinkels 02-17-2009 06:21 PM

Jan,

That is impressive, never thought about it. But how comes you say you don't create additional processes? I think for each line 'find' produces, one shell process is started and killed again.

jlinkels

jan61 02-18-2009 01:45 PM

Moin,

no, the -exec creates a new process for each line, not the -printf. The shell behind the pipe is one process, it gets a list of output redirects, no external programs. It's the same as you would write the lines on a shell prompt.

Jan

jan61 02-18-2009 02:07 PM

Moin,

Quote:

Originally Posted by GazL (Post 3447451)
...sooner or later, some nasty git would come along and do something evil like...
Code:

mkdir evildir\'\;rm\ something_important\;\#
;)

Such bad guys exist, really ;-) But I wonder, if the -exec touch is safe against similar attacks - the evil dirname additionally has to include the "\;", but it's the same way.

IMHO there is no 100% protection - may be, the -print0 find option together with xargs -0 is safe - at least it's more difficult to trick, but also more difficult to use in this case :-)

Jan

jlinkels 02-18-2009 05:00 PM

Quote:

Originally Posted by jan61 (Post 3448546)
Moin,

no, the -exec creates a new process for each line, not the -printf. The shell behind the pipe is one process, it gets a list of output redirects, no external programs. It's the same as you would write the lines on a shell prompt.

Jan

I see. Your are right, but I didn't see that yesterday night. Nice touch (pun intended)

Hans


All times are GMT -5. The time now is 09:19 PM.