Saturday, April 27, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 6817  / 2 Years ago, fri, march 4, 2022, 12:09:46

I am trying to use shell to find all sub-directories in any directory. What I would want is to have a .sh file (shell script file) that can receive as a parameter the name of the directory I'm interested in and the list of files I want to find (NOTE: I want only sub-directories that have all these files).



I know I can use this:



find $D -perm -u=rx -type f


Where D is the directory, -u is the user, r is the users right to read and x is the right to modify I believe, but uhm I have no idea how to make the file accept parameters and I have no idea how to use -u=rx



EDIT: I now understand how to use parameters for a shell script file, so that's ok. I still don't get most of the rest.



I would love it if someone could either explain the code I mentioned or ... give an alternative ?



I'm also ok with a partial answer, I just need some help.


More From » command-line

 Answers
2

I would interpret your requirements as "find all subdirectories which contain all the specific files"



#!/bin/bash
parent_dir="$1"
shift
find "$parent_dir" -type d |
while IFS= read -r subdir; do
all_present=true
for file in "$@"; do
if [[ ! -f "$subdir/$file" ]]; then
all_present=false
break
fi
done
$all_present && echo "$subdir"
done


the "IFS=" and "read -r" parts ensure the value of "dir" contains the actual directory name even if it includes spaces or special characters.


[#38593] Friday, March 4, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
olouredping

Total Points: 259
Total Questions: 100
Total Answers: 121

Location: New Caledonia
Member since Wed, Sep 15, 2021
3 Years ago
;