LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-17-2009, 06:30 AM   #1
samengr
Member
 
Registered: Jan 2008
Posts: 59

Rep: Reputation: 15
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
 
Old 02-17-2009, 06:36 AM   #2
datopdog
Member
 
Registered: Feb 2008
Location: JHB South Africa
Distribution: Centos, Kubuntu, Cross LFS, OpenSolaris
Posts: 806

Rep: Reputation: 41
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
 
Old 02-17-2009, 07:00 AM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,961

Rep: Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948
No need for a script or a loop...

Code:
find . -maxdepth 5 -type d -exec touch {}/testfile \;
 
Old 02-17-2009, 01:32 PM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

no need for a bunch of processes ;-)

Code:
find . -maxdepth 5 -type d -printf ">'%p/testfile'\n" | sh
Jan
 
Old 02-17-2009, 03:47 PM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,961

Rep: Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948Reputation: 3948
Quote:
Originally Posted by jan61 View Post
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\;\#
 
Old 02-17-2009, 06:21 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040
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
 
Old 02-18-2009, 01:45 PM   #7
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
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
 
Old 02-18-2009, 02:07 PM   #8
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by GazL View Post
...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
 
Old 02-18-2009, 05:00 PM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040
Quote:
Originally Posted by jan61 View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to create sequential, numbered directories ericcarlson Linux - Software 4 01-08-2010 09:43 AM
Bash script creates blank file name snowman81 Programming 9 03-02-2008 10:37 PM
Create a script to display file name, Inode, and size of any file. Has to be a script JaxsunApex Linux - Newbie 7 01-29-2007 08:15 PM
Script to create multiple directories under users homes shawnbishop Linux - Software 4 03-30-2006 08:11 AM
Create an archive file which includes files in sub-directories? amaze Linux - General 1 09-26-2003 08:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration