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 dirsdir1/
,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
How to Execute Shell Builtin from Scala
How to Add Export Statement in a Bash_Profile File
Identify Program That Connects to a Unix Domain Socket
How to Do a While Loop with a String Redirected into It
Why Is The Close Function Is Called Release in 'struct File_Operations' in The Linux Kernel
Which Suits Linux ? Gnu Make Vs Cmake Vs Codeblocks Vs Qmake
Compiling a Driver as a Part of a Kernel, Not as a Module
Mongo Daemon Doesn't Run by Service Mongod Start
Getting Following Error After The Command Sudo Apt-Get Update on Ubuntu 16.04
Apache Httpd VS. Tomcat 7: Port 80 VS. Port 8080
How to Block Push to Master Branch on Remote
Linux: Send Mail After a Process Id Finishes or Is Killed
Why The Size of an Empty Directory in Linux Is 4Kb
How Delete File from Fortran Code
How to Print Linux Group Names on Multiple Line Instead of Single Line Output