Linux: Using Bash to Search for Files Matching Certain Extensions

# using tree: list any files in a directory
parentDirectory=/home
tree --prune $parentDirectory

# using tree: list any files in a directory
# -f : print full file path
# -I : exclude files with certain pattern
# -P : include files with certain extensions
excludePatterns=".bashrc|.profile|bash_logout|FTPDevOps|ibadmin"
includePatterns="*.xml|*.txt"
parentDirectory=/home
tree -f -I $excludePatterns -P $includePatterns --prune $parentDirectory

# List any files that is over 5 minutes old
parentDirectory=/home
find $parentDirectory -type f -mtime -300

# List XML or TXT files over 5 minutes old, excluding one sub-directory
parentDirectory=/home
find $parentDirectory -path $parentDirectory/test -prune -false -o -type f \( -name "*.xml" -or -name "*.txt" \) -mtime -300

# List files over 5 minutes old, excluding multiple directories
parentDirectory=/home
minutes=5
# seconds=$(expr $minutes \* 60)
let seconds=$minutes*60 # more natural math expression
files=$(find $parentDirectory -type d \( -path $parentDirectory/test -o -path $parentDirectory/test1 -o -path $parentDirectory/test2 -o -path /home/kim \) -prune -false -o -name '*.xml' -mtime -$seconds)

# This part doesn't work yet due to line feeds and blank spaces in the output
if [ -z "$files" ]
then
	# Get the time of last modification, human-readable
	stat -c %y "$files"
fi

Leave a Reply

Your email address will not be published. Required fields are marked *