-1

I need to append the current date to the end of a filename and currently use the following:

mv "$file" "${file}_$dateNow"

So if I have the file Testfile1.xml it will get changed to Testfile1.xml230124

What I want to do is place the date after the filename, but before the file extension, ie Testfile1120324.xml

How may I do this? The file extension will not always be .xml, could be anything of any length.

Many thanks for any help

0stone0
  • 34,288
  • 4
  • 39
  • 64
Entropy1024
  • 7,847
  • 9
  • 30
  • 32

3 Answers3

1

This solution works using bash's parmeter expansion / pattern matching and and will place the date before multiple file extensions such as tar.gz:

mv "${file}" "${file%%.*}${dateNow}.${file#*.}"
# myfile.tar.gz -> myfile2023_01_24.tar.gz

The first part of this solution uses ${file%%.*} to greedily match against the .* at the end of the variable and cuts out that match returning the difference.

The end part, ${file#*.}, works in the opposite way by lazily matching against the *. pattern at the beggining of the variable and returning the difference.

If for example you want to place the date before just the first extension you can do this:

mv "${file}" "${file%.*}${dateNow}.${file##*.}"
# google.com.txt -> google.com2023_01_24.txt

I am assuming you have properly set the $dateNow var, however if you haven't then you can use the date command to insert the date:

mv "${file}" "${file%%.*}$(date +'%Y_%m_%d').${file#*.}"
Maximilian Ballard
  • 815
  • 1
  • 11
  • 19
1
filename="example.txt"
extension="${filename##*.}"
filename="${filename%.*}"
new_filename="$filename-$(date +%Y%m%d).$extension"
mv "$filename.$extension" "$new_filename"

This command first saves the file extension to a variable called "extension" by using the parameter substitution pattern ${filename##.}. Then it saves the filename without the extension to a variable called "filename" by using the parameter substitution pattern ${filename%.}. Then it creates a new variable called "new_filename" that appends the current date to the filename before the extension by using the command date +%Y%m%d and concatenating it to the filename variable. Finally, it uses the mv command to rename the original file to the new filename.

This command will append the current date formatted as YYYYMMDD to the file name before the extension, you can change the format of the date by changing the parameter on `date

Wiimm
  • 2,971
  • 1
  • 15
  • 25
-1

I would first put $file apart into everything before and after the extension:

if [[ $file =~ ^(.*)[.]([^./]*)$ ]]
then
  newname=${BASH_REMATCH[1]}_$dateNow.${BASH_REMATCH[2]}
else
  newname=$file_$dateNow
fi
mv -n "$file" "$newname"

Note that this also handles cases such as when file contains a.b/c.d (extension is just d, not b/c.d) and when file contains ' a.b/c` (no extension provided).

The -n catches the case that we accidentally already have a file named $newname in our directory.

user1934428
  • 19,864
  • 7
  • 42
  • 87