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

PowerShell: Install and Uninstall App on Remote Machines

# Trigger uninstall of an MSI file$computername="SERVER007"$keywordSearch="Polycom*"$installFile="\\FILESHERVER\Software\Polycom\Polycom_BToE_Connector_4.0.0.0 (new for 2019-07-19)\Polycom_BToE_Connector_4.0.0.0.msi"# Trigger silent install of an…

PowerShell: Downloading File Error ‘Internet Explorer engine is not available’

Error Message: PS C:\temp> wget http://download.windowsupdate.com/d/msdownload/update/software/secu/2022/01/windows10.0-kb5009546-x64_d3ab97e9f811d7bf19c268e5e6b5e00e92e110ed.msu wget : The response content cannot be parsed because…

Manual Sync for Office 365 Azure-AD Integration

1. Perform Prerequisites:* a. Install Dot Net Framework 4.5 )* b. Install Windows Management Framework…