Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to be able to move $oldfile to my backup folder and add the date to the file name. So I tried this...

mv $oldfile /home/u0146121/backupfiles/$oldfile_$(date +%F-%T)

This just gives me this output filename. (no orignal filename)


Then I realized the potential problem for the above method. I have a while read loop that reads through a text file of path names for other files I wanted to compare. So for example, $oldfile is actually =

 $ cat oldfiles.txt

So... I want to be able to move the file2.txt and just keep the file2.txt name and add the date to it as well.

share|improve this question
up vote 7 down vote accepted

The shell sees the variable $oldfile_ which is undefined. You can fix that by using ${oldfile}_ instead.

But, do you really want to keep the paths to the old file? If not, use

mv $oldfile /home/u0146121/backupfiles/${oldfile##*/}_$(date +%F-%T)
share|improve this answer

This is my solution to adding a time stamp when moving files in bash

#!/usr/bin/env bash


for file in *.FILE_EXTENSION; do
        NEWAPPEND=$(date +%s)
        cp $file "$COPYDIR"/"$NEWAPPEND"$file
        rm $file
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.