I am working on a task for which AWK is the designated tool.
The task is to list files that are:
- modified today (same day the script is run)
- of size 1 MB or less (size <= 1048576 bytes)
- User's input is to instruct where to start search.
- Search for files recursively.
Script:
#!/bin/bash
#User's input target for search of files.
target="$1"
#Absolute path of target.
ap="$(realpath $target)"
echo "Start search in: $ap/*"
#Today's date (yyyy-mm-dd).
today="$(date '+%x')"
#File(s) modified today.
filemod="$(find $target -newermt $today)"
#Loop through files modified today.
for fm in $filemod
do
#Print name and size of file if no larger than 1 MiB.
ls -l $fm | awk '{if($5<=1048576) print $5"\t"$9}'
done
My problem is that the for-loop does not mind the size of files!
Every variable gets its intended value. AWK does what it should outside a for-loop. I've experimented with quotation marks to no avail.
Can anyone tell what's wrong?
I appreciate any feedback, thanks.
Update: I've solved it by searching explicitly for files:
filemod="$(find $target -type f -newermt $today)"
How come that matters?