Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Solved] recurse directories with bash

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
bitrat
Posts: 80
Joined: 2023-07-20 09:41

[Solved] recurse directories with bash

#1 Post by bitrat »

Hi,

I want to walk a largish directory tree and call the same bash script for each, with parent environment (directory) passed via stdargs, without creating a fork bomb.

I was just wondering if there's an idiom or prior art for this?

My first cut:

Code: Select all

$ cat ./brd.sh
#!/bin/bash

find "$1" -mindepth 1 -maxdepth 1 -type d | while read d ; do

       echo "$d"

       ./brd.sh "$d"

done

Code: Select all

mkdir -p a/b/c/d/1/2/3
mkdir -p a/b/c/4/5/6

$ ./brd.sh .
./a
./a/b
./a/b/c
./a/b/c/d
./a/b/c/d/1
./a/b/c/d/1/2
./a/b/c/d/1/2/3
./a/b/c/4
./a/b/c/4/5
./a/b/c/4/5/6
I'd be interested to hear about any caveats, before I release it to the wild.

Cheers,
bitrat
Last edited by bitrat on 2023-11-17 19:38, edited 1 time in total.

User avatar
LinuxOS
Posts: 52
Joined: 2022-07-31 01:46
Has thanked: 27 times
Been thanked: 3 times

Re: recurse directories with bash

#2 Post by LinuxOS »

find has -exec and -execdir, try something like

Code: Select all

find "$1" -mindepth 1 -maxdepth 1 -type d -exec ./brd.sh '{}' \;

Aki
Global Moderator
Global Moderator
Posts: 2979
Joined: 2014-07-20 18:12
Location: Europe
Has thanked: 75 times
Been thanked: 407 times

Re: recurse directories with bash

#3 Post by Aki »

Moved to “Programming” sub-forum
⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
⠈⠳⣄⠀

steve_v
df -h | grep > 20TiB
df -h | grep > 20TiB
Posts: 1418
Joined: 2012-10-06 05:31
Location: /dev/chair
Has thanked: 80 times
Been thanked: 191 times

Re: recurse directories with bash

#4 Post by steve_v »

One can almost certainly do this entirely in bash, using the '*/' directory glob, a test for sanity, and some string manipulation (or dirname) to get the parent, without forking any find processes.
Personally I'd be inclined to drop the calling another script bit (and by extension the mentioned risk of fork bombs) too, and just put whatever it's supposed to do inside the main loop. Hard to say how well that'll work without knowing your final intent though.
Once is happenstance. Twice is coincidence. Three times is enemy action. Four times is Official GNOME Policy.

bitrat
Posts: 80
Joined: 2023-07-20 09:41

Re: recurse directories with bash

#5 Post by bitrat »

Thanks, all good! I found find -exec find .. a good approach. I also made some intermediate files of directory listings, as I wanted to filter them.

Cheers,
bitrat

Post Reply