Append Date to Filename in Linux

Append date to a filename in linux

Something like can do the work:

a=somefile.txt
bname="${a%.*}"
ename="${a##*.}"
date=$(date +%d%m%Y%H%M%S)
echo ${bname}_${date}.${ename}

How to create a file with todays date in the filename

You should use double quotes and need to evaluate date +"%F" using command substitution.

  $ touch "test_$(date +%F)"

This will create an empty file test_2018-01-15

Double quote helps you create a single file where some options of date command would include a space.

For example, touch test_$(date) will create multiple files, where as touch "test_$(date)" won't.

As pointed out by OP, one would need additional quotes " around the format options, when multiple of them are used:

touch "test_$(date +"%F %T")" 

Creating filename_$(date %Y-%m-%d) from systemd bash inline script

You'll need to escape the $ and % signs, by doubling them both in order to make this work.

As described here:

  • https://github.com/systemd/systemd/issues/2146
  • https://www.freedesktop.org/software/systemd/man/systemd.service.html#Command%20lines

To pass a literal dollar sign, use "$$"

ExecStart=/bin/bash -c 'echo $$(date +%%Y-%%m-%%d) >> /home/username/test_output_$$(date +%%Y-%%m-%%d).log'

Appending creation date on filename

You could use the following bash command:

find . -type f -exec bash -c 'mv "$1" "$(dirname "$1")/$(stat -c %w "$1" | sed "s/\([^.]*\).*/\1/;s/[-: ]/_/g")_$(basename "$1")"' _ {} \;

find looks for all regular file and rename using mv each file found.

The filename is built such that it gets the creation time appended at the beginning of the file.

In order to get the file creation time, I use stat with option -c %w instead of date -r that gives the modification time.

The sed command replaces the output from stat to the wanted format (with _).

Append current date to the filename via Cron?


* * * * * echo "hello" > /tmp/helloFile_$(date +\%Y\%m\%d\%H\%M\%S).txt

You just need to escape the percent signs.

Other date formats:
http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

Linux Script to redirect output to log file with date filename

You can use date to choose the format of the log file. Assuming YYYY-MM-DD, you can use the following. Note using '>>' to append/create the log file.

java abc.java >> "logfile.$(date +'%Y-%m-%d').log"
# Test
echo abc.java >> "logfile.$(date +'%Y-%m-%d').log"

Also note that 'java abc.java' need to be reviewed. The java command is usually invoked with class name (java abc), and not the name of a file.

Append date/timestamp to existing files

I would use a "for" loop over the wildcard list of matches and then use parameter expansion and command substitution to splice out the rest:

for file in *.log
do
echo mv -- "$file" "${file%.log}_$(date +%Y%m%d).log"
done

The pieces in the middle break down as:

  • mv -- -- invoke "mv" and explicitly tell it that there are no more options; this insulates us from filenames that might start with -i, for example
  • "${file%.log} -- expands the "$file" variable and removes the ".log" from the end
  • _ -- just adds the underscore where we want it
  • $(date +%Y%m%d) -- calls out to the date command and inserts the resulting output
  • .log -- just adds the ".log" part back at the end

Remove the "echo" if you like the resulting commands.
If you want a static timestamp, then just use that text in place of the whole $(date ...) command substitution.

On your sample input, but with today's date, the output is:

mv -- foo1.log foo1_20210610.log
mv -- foo2.log foo2_20210610.log

Adding timestamp to a filename with mv in BASH

The few lines you posted from your script look okay to me. It's probably something a bit deeper.

You need to find which line is giving you this error. Add set -xv to the top of your script. This will print out the line number and the command that's being executed to STDERR. This will help you identify where in your script you're getting this particular error.

BTW, do you have a shebang at the top of your script? When I see something like this, I normally expect its an issue with the Shebang. For example, if you had #! /bin/bash on top, but your bash interpreter is located in /usr/bin/bash, you'll see this error.

EDIT

New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?

Two ways:

  1. Select the Edit->EOL Conversion->Unix Format menu item when you edit a file. Once it has the correct line endings, Notepad++ will keep them.
  2. To make sure all new files have the correct line endings, go to the Settings->Preferences menu item, and pull up the Preferences dialog box. Select the New Document/Default Directory tab. Under New Document and Format, select the Unix radio button. Click the Close button.


Related Topics



Leave a reply



Submit