LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-10-2022, 09:51 PM   #1
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
Command to make a script running from every subdir


How is the find command (or else if better) to let us run a script from, i.e. relative to, every subdir under, and including, current working directory ?

illustratio e.g. script is
Code:
grep 'foo' *
 
Old 07-10-2022, 10:20 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Code:
find . -type d -exec sh -c 'cd {}; grep foo *' \;
Note that you have to execute a shell in order to expand the "*" in the target directories. Without that, the find command will just pass a literal "*" to the grep (or whatever) command, which will just complain that it can't find any file named "*".

I originally had a much more complex example using "-execdir", but found things were much simpler without it.

Last edited by rknichols; 07-10-2022 at 10:35 PM.
 
1 members found this post helpful.
Old 07-10-2022, 10:22 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Not sure I follow but you can use -R option in grep to recurse subdirs
 
1 members found this post helpful.
Old 07-10-2022, 10:39 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
I believe grep was just an example, not the actual intended command.
 
Old 07-11-2022, 03:42 AM   #5
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,506

Rep: Reputation: Disabled
Run the command 'recursively'.
 
Old 07-11-2022, 05:59 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,816

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Within the shell script filenames should be in quotes, and exit the cd status should matter.
Code:
find . -type d -exec sh -c 'cd "{}" && grep foo *' \
Code:
find . -type d -exec sh -c 'cd "{}" && pwd' \;
Portable (all find versions) is
Code:
find . -type d -exec sh -c 'cd "$0" && pwd' {} \;
or
Code:
find . -type d -exec sh -c 'cd "$1" && pwd' chdir.sh {} \;

Last edited by MadeInGermany; 07-11-2022 at 06:00 AM.
 
1 members found this post helpful.
Old 07-11-2022, 08:41 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by BudiKusasi View Post
How is the find command (or else if better) to let us run a script from, i.e. relative to, every subdir under, and including, current working directory ?
Trying to modify this to be a coherent question:
Quote:
How can the find command run a script against all of the found results?
All I have here is about the same with rknichols said in post #2, in fact that's my daily use of find.
As cited, you can run a script using the -exec argument.

Does that solve what you're trying to do? If not, maybe a more clear question?
 
Old 07-11-2022, 09:10 AM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Code:
grep -R foo
Your example should probably have used a different command.
 
  


Reply



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
How to archive (tar) every subdir rmcban Linux - Newbie 7 07-27-2010 06:19 AM
how many files on a subdir ? graziano1968 Linux - General 2 08-09-2006 11:18 PM
Why does sudo mkdir -m766 0/subdir deny? suguru Debian 1 11-08-2004 05:07 AM
deleting all *.m3u files in a directory+subdir? minm Linux - Newbie 5 10-12-2004 03:31 PM
How to change owner and group in a directory to include subdir and all files Lakota Linux - General 2 07-15-2004 09:35 AM

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

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

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