In Linux, How to Find Find Directory with The Most Subdirectories or Files

In Linux, how do I find find directory with the most subdirectories or files?

starting from the current directory, you could try

find . -type d | cut -d/ -f 2 | uniq -c

This will list all directories starting from the current one, split each line by the character "/", select field number "2" (each line starts with "./", so your first field would be ".") and then only outputs unique lines, and a count how often this unique line appears (-c parameter).

You could also add an "sort -g" at the end.

How can I recursively find all files in current and subfolders based on wildcard matching?

Use find:

find . -name "foo*"

find needs a starting point, so the . (dot) points to the current directory.

Which directory has more files and which has more subdirectories?

For files you can do:

filesX=$(find "${X}" -type f | wc -l)
filesY=$(find "${Y}" -type f | wc -l)

if (( filesX < filesY )); then
echo "${Y} has more files"
elif (( filesX > filesY )); then
echo "${X} has more files"
else
echo "${X} and ${Y} have same number of files"
fi

For dirs it's basically the same:

dirsX=$(find "${X}" -type d | wc -l)
dirsY=$(find "${Y}" -type d | wc -l)

if (( dirsX < dirsY )); then
echo "${Y} has more dirs"
elif (( dirsX > dirsY )); then
echo "${X} has more dirs"
else
echo "${X} and ${Y} have same number of dirs"
fi

Print the directory with the most files in unix/linux

Here you go:

for d in */ ; do echo "$d" $(find $d -type f | wc -l); done | sort -n -k 2

Explanation:

for d in * 

Loop through directories only

echo "$d" $(find $d -type f | wc -l) 

Show the directory name and the count of its files (recursively)

sort -n -k 2

Sort numerically the output of the whole thing (for loop) by using the second field (the number of files)

List files, owner, and directory of all files in all subdirectories

Use find:

find /path/to/directory -type f -printf '%u\t%f\t%h\t%s\n'

Columns are separated by tab characters.

How to find all files containing specific text (string) on Linux?

Do the following:

grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
  • -e is the pattern used during the search

Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:

  • This will only search through those files which have .c or .h extensions:

    grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
  • This will exclude searching all the files ending with .o extension:

    grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
  • For directories it's possible to exclude one or more directories using the --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:

    grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"

This works very well for me, to achieve almost the same purpose like yours.

For more options, see man grep.



Related Topics



Leave a reply



Submit