2

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 {} \+
1
  • filename only. Not content
    – JT-66
    Mar 29 '18 at 13:57
2

find . -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.

2
_$(date +%Y-%m-%d_%H-%M-%S)

for the date and time, (or date +%F_%T for short in most date implementations).

0
1

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).

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.