How to list specific type of files in recursive directories in shell?
If you are more confortable with "ls" and "grep", you can do what you want using a regular expression in the grep command (the ending '$' character indicates that .doc must be at the end of the line. That will exclude "file.doc.txt"):
ls -R |grep "\.doc$"
More information about using grep with regular expressions in the man.
Bash - What is a good way to recursively find the type of all files in a directory and its subdirectories?
This may help: How to recursively list subdirectories in Bash without using "find" or "ls" commands?
That said, I modified it to accept user input as follows:
#!/bin/bash
recurse() {
for i in "$1"/*;do
if [ -d "$i" ];then
echo "dir: $i"
recurse "$i"
elif [ -f "$i" ]; then
echo "file: $i"
fi
done
}
recurse $1
If you didn't want the files portion (which it appears you don't) then just remove the elif and line below it. I left it in as the original post had it also. Hope this helps.
Recursively looking for a list of file types
The whole idea of using find
int the first place is not needed. The shell globbing support in bash
is sufficient enough for this requirement. The bash
shell provides an extended glob support option using which you can get the file names under recursive paths that don't end with the extensions you want to ignore.
The extended option is extglob
which needs to be set using the shopt
option as below. Additionally you could use couple of options more i.e. nullglob
in which an unmatched glob is swept away entirely, replaced with a set of zero words. And globstar
that allows to recurse through all the directories
shopt -s extglob nullglob globstar
Now all you need to do is form the glob expression to exclude the files of type *.png
, *.jpg
and *.gif
which you can do as below. We use an array to populate the glob results because when quoted properly and expanded, the filenames with special characters would remain intact
fileList=(**/!(*.jpg|*.gif|*.png))
The option **
is to recurse through the sub-folders and !()
is a negate operation to not include any of the file extensions listed inside. Now for printing the actual files, just do
printf '%s\n' "${fileList[@]}"
If your intentions is for example to remove all the files identified, you don't need to store the glob results in the array. One could use the array approach when writing simple shell scripts which need to use the results of the glob. But for a case of deleting the files, you could use the rm
command.
At first you could check if the files returned are as expected and once you confirmed you could the rm
on the expression. Use ls
to see if the files are listed as expected
ls -1 -- **/!(*.jpg|*.gif|*.png)
and now after confirming the files to delete, do rm
at your own risk.
rm -- **/!(*.jpg|*.gif|*.png)
How to ls all the files in the subdirectories using wildcard?
3 solutions :
Simple glob
ls */*.pdb
Recursive using bash
shopt -s globstar
ls **/*.pdb
Recursive using find
find . -type f -name '*.pdb'
Recursively list files from a given directory in Bash
Your algorithm is entering endless loop since lsRec
function implicitly expects its argument to end in "/". First level works, as you pass path ending with "/" as input, but second level doesn't, as the path you're making recursive call with doesn't end in "/". What you could do is either add the slash when you make a recursive call, so it'll look like lsRec $x/
, or (better yet) add the slash in the loop arguments as in for x in $1/*; do
(as system generally ignores multiple adjacent path separators).
Moving forward, I'd advise you to quote the values (e.g. for x in "$1/"*
, lsRec "$x"
, lsRec "$arg"
) to avoid issues when path contains whitespace characters. You'll get there when you create a directory with space in its name under directory hierarchy you're scanning.
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.
How to find all file extensions recursively from a directory?
How about this:
find . -type f -name '*.*' | sed 's|.*\.||' | sort -u
grep recursively for a specific file type on Linux
Consider checking this answer and that one.
Also this might help you: grep certain file types recursively | commandlinefu.com.
The command is:
grep -r --include="*.[ch]" pattern .
And in your case it is:
grep -r --include="*.html" "onblur" .
Related Topics
How to Delete Multiple Files at Once in Bash on Linux
Colored Shell Script Output Library
Any Way to Exit Bash Script, But Not Quitting the Terminal
Excluding Directory When Creating a .Tar.Gz File
Custom Linux Gui: Where to Begin
Internals of a Linux System Call
How to Perform a 'Git Pull' Without Re-Entering My Ssh Password
JSON Command Line Formatter Tool for Linux
How to Clean Caches Used by the Linux Kernel
I Get "Dquote>" as a Result of Executing a Program in Linux Shell
How to Create a Directory and Give Permission in Single Command
How to Enable Scrolling in Tmux Panels with Mouse Wheel
Suppress Echo of Command Invocation in Makefile
Centos/Linux Setting Logrotate to Maximum File Size for All Logs
Linux Script to Kill Java Process
Why Can't You Use Cat to Read a File Line by Line Where Each Line Has Delimiters