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: 1-Liner to Change Computer Name and Join Active Directory

# The 1-liner Add-Computer -DomainName 'somedomain.com' -credential kimconnect.com\domainadmin1 -ComputerName $env:computernname -newname 'NewName' -restart # Quick…

PowerShell: Grant Current User Full Access to File or Folder

# Grant Current User Full Access to Object $object='C:\temp' $acl=Get-Acl $object $currentUser=[System.Security.Principal.WindowsIdentity]::GetCurrent().Name $grantAccess=New-Object System.Security.AccessControl.FileSystemAccessRule("$currentUser","FullControl","Allow") $acl.AddAccessRule($grantAccess)…

Basic CSS: Set the id of an Element

<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css"><style> .red-text { color: red; } h2 { font-family: Lobster, monospace; }…