Bash- how to select a directory based on the number of files it contains

Bash- how to select a directory based on the number of files it contains

I want to select all the sub-directories in a given folder that have a certain number (in this case, 75) files within them. Then I want to take the name of each of these sub-directories and assign it a new variable name to use throughout the rest of my script.

答案1

Here an idea.

filecount is the number of files you want to find. For each folder with this number of files, a variable will be created. The variable-name is the folder-name but the "/" were substituted by "_"

filecount=1
for i in `find folder -type d`
do
count=`ls $i|wc -l` if [ $count -eq $filecount ] then export `echo $i | sed -e "s/\//_/g"`=`ls -d $PWD/$i` fi done env | grep folder

相关内容