Posted On January 1, 2021

Linux: Using Bash to Search for Files Matching Certain Extensions

kimconnect 0 comments
blog.KimConnect.com >> Codes , Linux >> 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 *

Related Post

How To Install Microsoft Edge on Ubuntu / Debian / Linux Mint

Step 1: Add Microsoft Repository curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpgsudo install -o root…

Ubuntu/Lubuntu: Setting Up Development Environment

Linux is a great platform for development. One advantage of a this OS is that…

How to Remove Apps in CentOS/Redhat

Check the repo directory: [root@localhost ~]# ls /etc/yum.repos.d CentOS-Base.repo CentOS-Sources.repo CentOS-CR.repo CentOS-Vault.repo CentOS-Debuginfo.repo docker-ce.repo CentOS-fasttrack.repo…