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: Maintain Chocolatey Applications

This current version has some bugs... Review these other codes for some ideas on fixing...…

Basic CSS: Add Borders Around Your Elements

<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css"><style>.red-text {color: red;}h2 {font-family: Lobster, monospace;}p {font-size: 16px;font-family: monospace;}.smaller-image {width: 100px;}</style><h2 class="red-text">CatPhotoApp</h2><main><p…

Quick Script to Mount Remote UNC Paths as Local Drive Letters

Simple Mount: # Mapping a drive $username='domain\testAdmin' $password='PASSWORD' $remotePath='\\UNCSERVER\SHAREPATH\Backup' $unavailableDriveLetters=(Get-Volume).DriveLetter|sort $availableDriveLetters=.{(65..90|%{[char]$_})|?{$_ -notin $unavailableDriveLetters}} [char]$firstAvailableDriveLetter=$availableDriveLetters[0] $command="net…