I have some comments on the above script:
1) It is not recommended to parse ls
for data. If you need to extract the metadata of a file to test against, use stat
2) Do not Read Lines With For
! You should always use a while+read
loop when reading lines of text input
3) $(..) is highly recommended over `..`
But actually, you seem to be going to a lot of roundabout trouble just to move a few files. Saving the output of ls -l
to a file, then using awk
to parse out the filenames again, and finally a for
loop to move them one at a time. Seriously?
Is there any reason you can't simply do this?
echo 'Now moving the following files:'
printf '%s\n' $HOME/logfile.*
mv -t /path/to/directory $HOME/logfile.*
You can usually match the filenames you want with the proper globbing
patterns. And the nice thing is you won't get any error messages unless there are no files that match at all (and even that can be controlled with the nullglob
If you need to be more specific and only match files with actual month extensions, you can use bash
's extended globbing:
shopt -s extglob
mv -t /path/to/directory $HOME/logfile.@(0[1-9]|1[0-2])
Finally, if this is some kind of logfile maintenance script, have you looked into using logrotate