- How To Append Current Date To Filename in Bash Shell
- Append current date to a filename
- date command FORMAT_STRING
- Bash shell append date to filename in Linux or Unix
- How can I change the date modified/created of a file?
- 7 Answers 7
- Appending a current date from a variable to a filename
- 5 Answers 5
- Bash script to inject a date into a filename:
- Append date to filename in linux
- 6 Answers 6
- How do you put date and time in a file name?
- 2 Answers 2
How To Append Current Date To Filename in Bash Shell
H ow do I append current date (mm_dd_yyyy format) to a filename (e.g., backup_mm_dd_yyyy.sql) under Linux and UNIX like operating systems? How can I append a current date from a variable to a filename under Linux or Unix bash shell? How do I append date to filename?
We use the date command to show or set the system date and time. Further we can show the current date and time in the given FORMAT. This page explains how to append current date to a filename using various command line options.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Bash running on Linux, macOS or Unix |
Est. reading time | 4 minutes |
Append current date to a filename
To get the current date in mm_dd_yyyy format use the following date format syntax:
You can store this to a variable name:
Alternat syntax for command substitution is as follows:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Now we can append the current date stored in $now to a filename as follows:
echo «Coping data to /tmp/filename-$
date command FORMAT_STRING
FORMAT_STRING (FORMAT) controls the output. Interpreted sequences are as follows (taken from GNU/date man page)
- %a – locale’s abbreviated weekday name (e.g., Sun)
- %A – locale’s full weekday name (e.g., Sunday)
- %b – locale’s abbreviated month name (e.g., Jan)
- %B – locale’s full month name (e.g., January)
- %C – century; like %Y, except omit last two digits (e.g., 20)
- %d – day of month (e.g., 01)
- %D – date; same as %m/%d/%y
- %F – full date; same as %Y-%m-%d
- %g – last two digits of year of ISO week number (see %G)
- %G – year of ISO week number (see %V); normally useful only with %V
- %H – hour (00..23)
- %I – hour (01..12)
- %j – day of year (001..366)
- %m – month (01..12)
- %M – minute (00..59)
- %n – a newline
- %N – nanoseconds (000000000..999999999)
- %p – locale’s equivalent of either AM or PM; blank if not known
- %P – like %p, but lower case
- %q – quarter of year (1..4)
- %r – locale’s 12-hour clock time (e.g., 11:11:04 PM)
- %R – 24-hour hour and minute; same as %H:%M
- %s – seconds since 1970-01-01 00:00:00 UTC
- %S – second (00..60)
- %T – time; same as %H:%M:%S
- %u – day of week (1..7); 1 is Monday
- %U – week number of year, with Sunday as first day of week (00..53)
- %V – ISO week number, with Monday as first day of week (01..53)
- %w – day of week (0..6); 0 is Sunday
- %W – week number of year, with Monday as first day of week (00..53)
- %x – locale’s date representation (e.g., 12/31/99)
- %X – locale’s time representation (e.g., 23:13:48)
- %y – last two digits of year (00..99)
- %Y – year
- %z – +hhmm numeric time zone (e.g., -0400)
- %:z – +hh:mm numeric time zone (e.g., -04:00)
- %::z – +hh:mm:ss numeric time zone (e.g., -04:00:00)
- %. z – numeric time zone with : to necessary precision (e.g., -04, +05:30)
- %Z – alphabetic time zone abbreviation (e.g., EDT)
Bash shell append date to filename in Linux or Unix
Finally, you can create a filename as follows:
Источник
How can I change the date modified/created of a file?
Is there a way to change the date when a file was modified/created (which is shown in Nautilus or with the ls -l command)? Ideally I am looking for a command which can change the date/time stamps of a whole bunch of files to a certain amount of time earlier or later (e.g. +8 hours or -4 days etc.).
7 Answers 7
As long as you are the owner of the file (or root), you can change the modification time of a file using the touch command:
By default this will set the file’s modification time to the current time, but there are a number of flags, such as the -d flag to pick a particular date. So for example, to set a file as being modified two hours before the present, you could use the following:
If you want to modify the file relative to its existing modification time instead, the following should do the trick:
If you want to modify a large number of files, you could use the following:
You can change the arguments to find to select only the files you are interested in. If you only want to update the file modification times relative to the present time, you can simplify this to:
This form isn’t possible with the file time relative version because it uses the shell to form the arguments to touch .
As far as the creation time goes, most Linux file systems do not keep track of this value. There is a ctime associated with files, but it tracks when the file metadata was last changed. If the file never has its permissions changed, it might happen to hold the creation time, but this is a coincidence. Explicitly changing the file modification time counts as a metadata change, so will also have the side effect of updating the ctime .
Источник
Appending a current date from a variable to a filename
I’m trying to append the current date to the end of a file name like this:
Here is what I have so far:
However all I get is the name of the file, and it appends nothing. How do I append a current date to a filename?
5 Answers 5
More than likely it is your use of set . That will assign ‘today’, ‘=’ and the output of the date program to positional parameters (aka command-line arguments). You want to just use C shell (which you are tagging this as «bash», so likely not), you will want to use:
Notice the lack of spaces around the equal sign.
You also do not want to use & at the end of your statements; which causes the shell to not wait for the command to finish. Especially when one relies on the next. The find command could fail because it is started before the mkdir .
Bash script to inject a date into a filename:
This bash code in file called a.sh
When run, prints:
Explanation of the code:
Interpret the script with the /bin/bash interpreter. Make a new variable called today. Execute the date command, passing in the Y, m, d, H, M, S flags to configure the output. Place the result into the date variable.
Create a new variable called filename, surround the $today variable with the rest of the static filename text. then echo the filename to screen.
Cram it into a one-liner to increase lulz:
You seem to have mixed up several things.
set today = ‘date +%Y’ looks like tcsh syntax, but even in tcsh it assigns the string date +%Y to the variable today , it doesn’t run the date command. As you’re probably using bash or some other POSIX shell, the syntax of an assignment is today=some_value (with no spaces around the equal sign). To run the command and assign its output to the variable, use command substitution:
(I’ve also completed the date specification). You can use backquotes instead of dollar-parentheses, but it’s prone to being visually confused with forward quotes, and the rules for when you need quotes inside a backquoted command are pretty complex and implementation-dependent, so it’s better not to stick to $(…) (which has the same effect with a saner syntax).
You used & at the end of several commands. That makes the command execute in the background, which is not wanted here. I suspect you meant && , which means to execute the next command only if the first command succeeded.
An alternative to using && after each command is to start your script with set -e . This tells the shell to stop executing the script as soon as any command returns a nonzero status (except for commands in if conditions and a few other cases).
Your find command is fine but probably doesn’t do what you intend to do (though I don’t know for sure what that is).
You’re creating a directory with mkdir and then immediately traversing it with find . That won’t be useful unless the directory already exists. Did you mean to create a directory for today’s logs and move recent files from The_Logs to a directory called e.g. The_Logs.2012-02-11 ?
Or did you mean to rename today’s log files to add the suffix $today ? That requires calculating the different file name for each file to move.
Note that I used -mtime , to move files based on their modification time, and not -atime , which is the time the file was last read (if your system keeps track of that — if it doesn’t, the atime may be as far back as the mtime).
Источник
Append date to filename in linux
I want add the date next to a filename («somefile.txt»). For example: somefile_25-11-2009.txt or somefile_25Nov2009.txt or anything to that effect
Maybe a script will do or some command in the terminal window. I’m using Linux(Ubuntu).
Thanks in advance.
oh i almost forgot to add that the script or command should update the filename to a new date everytime you want to save the file into a specific folder but still keeping the previous files. So there would be files like this in the folder eventually: filename_18Oct2009.txt , filename_9Nov2009.txt , filename_23Nov2009.txt
6 Answers 6
You can use backticks .
There’s two problems here.
1. Get the date as a string
This is pretty easy. Just use the date command with the + option. We can use backticks to capture the value in a variable.
You can change the date format by using different % options as detailed on the date man page.
2. Split a file into name and extension.
This is a bit trickier. If we think they’ll be only one . in the filename we can use cut with . as the delimiter.
However, this won’t work with multiple . in the file name. If we’re using bash — which you probably are — we can use some bash magic that allows us to match patterns when we do variable expansion:
Putting them together we get:
And if we’re less worried about readability we do all the work on one line (with a different date format):
Источник
How do you put date and time in a file name?
I’m trying to execute a command and would like to put the date and time in the output file name.
Here is a sample command I’d like to run.
md5sum /etc/mtab > 2016_4_25_10_30_AM.log
The date time format can be anything sensible with underscores. Even UTC if the AM and PM can’t be used.
2 Answers 2
If you want to use the current datetime as a filename, you can use date and command substitution.
This results in the file 2016_04_25_10_30_AM.log (although, with the current datetime) being created with the md5 hash of /etc/mtab as its contents.
Please note that filenames containing 12-hour format timestamps will probably not sort by name the way you want them to sort. You can avoid this issue by using 24-hour format timestamps instead.
If you don’t have a requirement to use that specific date format, you might consider using an ISO 8601 compliant datetime format. Some examples of how to generate valid ISO 8601 datetime representations include:
If you want «safer» filenames (e.g., for compatibility with Windows), you can omit the colons from the time portion.
Please keep in mind that the above examples all assume local system time. If you need a time representation that is consistent across time zones, you should specify a time zone offset or UTC. You can get an ISO 8601 compliant time zone offset by using «%z» in the format portion of your date call like this:
You can get UTC time in your date call by specifying the -u flag and adding «Z» to the end of the datetime string to indicate that the time is UTC like this:
Источник