I'm currently taking a class for Operating Systems and we're learning to do bash scripts as part of the curriculum. I need to append the date to all the files in a directory without touching sub-directories via a script. I've been to cobble up a one-liner script that will append the date to all the files, but it hits the folders in the current directory as well.
for f in *; do mv -- "$f" "$f-$(stat -c %Y "$f" | date +%Y%m%d)"; done
This'll append the date to the end of the filename, but like I said, it hits the directories under it. I'm currently using version 4.1.2 of bash on RedHatLinux.
I'm confused as all get out because of how inexeperienced with Unix I am (I'm primarily a Windows user), so any help would be appreciated.
Best Answer
As slm already indicated you can test for
$f
to be a regular file. While learning I would change the script to not be a one liner, they tend to be harder to read and maintain:(you can always fold this later by inserting
;
and deleting newlines)The
-f
tests the argument to be a regular file (not a device or directory), there are other tests as well (-d
for directory e.g, so you could testif [ ! -d "$f" ]
as well in this case).