Append the current date and time to the file name

5,886

Solution 1

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.

Solution 2

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

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

Solution 3

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

Share:
5,886

Related videos on Youtube

JT-66
Author by

JT-66

Updated on September 18, 2022

Comments

  • JT-66
    JT-66 almost 2 years

    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 {} \+
    
    • JT-66
      JT-66 over 6 years
      filename only. Not content