What can I add to this command to append the current date and time to the file name?
find . -maxdepth 1 -type f ! -name "*.*" -exec cp -p -t /new/location {} \+
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityfind . -maxdepth 1 -type f ! -name "*.*" -exec cp -p {} /new/location/{}-$(date +%Y-%m-%d) \;
I have removed the target (-t
) parameter from the cp
command and described the path and filename.
{}
is used as a placeholder for the file name, to which we append a date
in the desired format.
The example formatting +%Y-%m-%d
should be self-explanatory.
_$(date +%Y-%m-%d_%H-%M-%S)
for the date and time, (or date +%F_%T
for short in most date
implementations).
With zsh
:
autoload zmv # best in ~/.zshrc
now=${(%):-%D{%FT%T%z}} # using prompt expansion with the %
# parameter expansion flag
# or (using the strftime builtin)
zmodload zsh/datetime
strftime -s now %FT%T%z $EPOCHSECONDS
# or (using the date external utility)
now=$(date +%FT%T%z)
# optional:
zmodload zsh/files # to enable a cp builtin which would speed
# things up considerably if you have
# zillions of files to copy.
zmv -C '^*.*(#q.)' '/new/localtion/$f-$now'
(#q.)
is a glob qualifier that selects regular files only (like find
's -type f
).