-1

So I have a file that I want to copy if it has been modified. I want to save it as date and time in it's new location.

mv my_file.db $(date +%F)

This command will move it and set the date as the name, but it removes the extension. I have 3 different file extensions I want to move. I also want to have the timestamp as I will be copying multiple times per day.

Input:

sdfgdg.a
abdfgs.b
dfgdfg.c

So by running...

mv * ../Folder

Should produce:

Folder/sdfgdg-2015-01-02-14:50:00.a
Folder/abdfgs-2015-01-02-14:50:00.b
Folder/dfgdfg-2015-01-02-14:50:00.c
User
  • 23,729
  • 38
  • 124
  • 207

1 Answers1

0

Add this function to your ~/.bashrc and source it (source ~/.bashrc) or login with a second session.

function mymv() {
  if [[ $# -lt 2 ]]; then
     echo "Syntax error: missing aruments"
     return 1
  fi

  target="${@: -1}"     # last argument
  d="$(date +%F-%T)"

  while [[ $# -ge 2 ]]; do
    file="$1"
    mv -v "$file" "$target/${file%.*}-$d.${file##*.}"
    shift
  done
}

Usage:

mymv file ... directory

Example:

mymv * ../Folder
Cyrus
  • 84,225
  • 14
  • 89
  • 153