1

Is it possible to do something like this:

inputNum="$1"

files=($(find /dir/to/check -mtime $inputNum))

Basically the idea is that I can use an input parameter to set the number of days to find files and set it to a variable array. I am not sure on the syntax to make this readable in bash.

2

The output of find is not post-processable reliably unless you use -print0 instead of -print (-print implied when no action is specified).

To post-process the output of find -print0 and store the file paths in an array:

With bash4.4+:

readarray -td '' files < <(find /dir/to/check -mtime "$inputNum" -print0)

With older versions:

files=()
while IFS= read -rd '' file; do
  files=("${files[@]}" "$file")
done < <(find /dir/to/check -mtime "$inputNum" -print0)

More generally, you'd want to read the recommendations at: Why is looping over find's output bad practice?

| improve this answer | |
0

If you can use zsh instead of bash, then it's a lot easier. Zsh can match files based on their attributes through glob qualifiers.

files=(/dir/to/check/**/*(Dm$inputNum))

**/* searches subdirectories recursively. The parentheses after * contain glob qualifiers: D to include dot files, and m to match files based on their modification time in days like find -mtime (you can also choose a different unit and make the time a limit, e.g. (Dmm-$min) acts like find -mmin -$min).

Zsh is preinstalled on macOS. On most other unix systems (in particular most Linux distributions), it's available as a package but not installed by default.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.