list all directories containing *.html files and also list the files in the directories
在请求帮助或建议时,用'in the event of...'而非'要是...就好了' #生活技巧# #职场沟通技巧# #商务英语#
Using zsh:
setopt extendedglob nullglob for pathname in /**/*(/e{'[[ -n $REPLY/(#i)*.htm(l#)(#q.) ]]'}); do printf '%s:\n' $pathname ls -l $pathname done
This prints the pathname of each directory containing any regular file whose name ends with either .htm or .html (regardless of case), followed by the ls -l output for that directory.
The loop loops over every directory in or beneath / that contains a HTML file. It does this using the /**/* glob which, by itself matches everything in the whole / directory hierarchy. This list is filtered to only contain directory pathnames by the / glob qualifier (the initial / in the first parenthesis), and the list is further filtered to only contain those entries for which [[ -n $REPLY/(#i)*.htm(l#)(#q.) ]] is true. This expression, where $REPLY is one of the directory pathnames being examined, will be true if a directory contains at least one regular file with either a .htm or .html filename suffix (case insensitively).
The e{...} part of the globbing pattern could probably be written more succinctly.
Using bash:
shopt -s globstar nullglob extglob nocaseglob for pathname in /**/*/; do set -- "$pathname"/*.htm?(l) if [[ -f $1 ]]; then printf '%s:\n' "${pathname%/}" ls -l "$pathname" fi done
This uses the globstar shell option to enable the use of the ** globbing pattern (enabled by default in the zsh shell). It iterates over all directory pathnames in the whole directory hierarchy from / down and tries to expand the *.htm?(l) glob in each directory (this matches the HTML files we're interested in). If the first match of this glob is a regular file, or a symbolic link to one, then the directory pathname and the ls -l listing is outputted.
If you may have directories with the .htm on .html filename suffix, you would have to test the matches of the expansion inside the loop in a separate loop, just to make sure you catch any regular files (or symbolic links to regular files) with the HTML suffixes:
shopt -s globstar extglob nocaseglob for pathname in /**/*/; do for match in "$pathname"/*.htm?(l); do if [[ -f $match ]]; then printf '%s:\n' "${pathname%/}" ls -l "$pathname" break fi done done
I've deleted the nullglob shell option in this variation since we do no longer depend on it.
In the POSIX sh shell you don't have access to the ** glob, so you would have to use find to generate the directory pathnames for the loop:
find / -type d -exec sh -c ' for pathname do for match in "$pathname"/*.[hH][tT][mM] "$pathname"/*.[hH][tT][mM][lL] ; do if [ -f "$match" ]; then printf "%s:\n" "${pathname%/}" ls -l "$pathname" break fi done done' sh {} +
Here, find acts like a sort of pathname generator for the embedded sh -c script and feeds it with pathnames of directories.
The sh -c script does pretty much what the 2nd bash variation of the answer does, i.e. it iterates over the expansion of the glob that should match the wanted names, testing each name to see whether it's a regular file (or a symbolic link to one). Once it finds a file it prints the directory pathname followed by the ls -l output.
网址:list all directories containing *.html files and also list the files in the directories https://klqsh.com/news/view/344081
相关内容
Find Command in Linux (Find Files and Directories)search in subdirectories for all html files containing the tag
6 Examples to Find Files in Linux with Find Command
How to Use the Linux find Command to Locate Files Like a Pro
How to find file in Linux
Find Command Cheat Sheet & Quick Reference
How to use file explorer in Windows 11
Find your files in Windows
Zip and unzip files
Full List of Colleges with the largest endowment in 2024

