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, 2018 at 13:57

3 Answers 3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .