Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-17-2009, 06:30 AM
|
#1
|
Member
Registered: Jan 2008
Posts: 59
Rep:
|
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
|
|
|
02-17-2009, 06:36 AM
|
#2
|
Member
Registered: Feb 2008
Location: JHB South Africa
Distribution: Centos, Kubuntu, Cross LFS, OpenSolaris
Posts: 806
Rep:
|
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
|
|
|
02-17-2009, 07:00 AM
|
#3
|
LQ Veteran
Registered: May 2008
Posts: 7,190
|
No need for a script or a loop...
Code:
find . -maxdepth 5 -type d -exec touch {}/testfile \;
|
|
|
02-17-2009, 01:32 PM
|
#4
|
Member
Registered: Jun 2008
Posts: 235
Rep:
|
Moin,
no need for a bunch of processes ;-)
Code:
find . -maxdepth 5 -type d -printf ">'%p/testfile'\n" | sh
Jan
|
|
|
02-17-2009, 03:47 PM
|
#5
|
LQ Veteran
Registered: May 2008
Posts: 7,190
|
Quote:
Originally Posted by jan61
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\;\#

|
|
|
02-17-2009, 06:21 PM
|
#6
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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
|
|
|
02-18-2009, 01:45 PM
|
#7
|
Member
Registered: Jun 2008
Posts: 235
Rep:
|
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
|
|
|
02-18-2009, 02:07 PM
|
#8
|
Member
Registered: Jun 2008
Posts: 235
Rep:
|
Moin,
Quote:
Originally Posted by GazL
...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
|
|
|
02-18-2009, 05:00 PM
|
#9
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
Quote:
Originally Posted by jan61
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 07:24 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|