- How do you put date and time in a file name?
- 2 Answers 2
- How to redirect the output of the time command to a file in Linux?
- 12 Answers 12
- Append date to filename in linux
- 6 Answers 6
- How can I change the date modified/created of a file?
- 7 Answers 7
- Store the output of date and watch command to a file
- 8 Answers 8
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:
Источник
How to redirect the output of the time command to a file in Linux?
Just a little question about timing programs on Linux: the time command allows to measure the execution time of a program:
Which works fine. But if I try to redirect the output to a file, it fails.
I know there are other implementations of time with the option -o to write a file but my question is about the command without those options.
12 Answers 12
which combines the STDERR of «time» and your command into time.txt
which puts STDERR from «sleep» into the file «sleep.stderr» and only STDERR from «time» goes into «time.txt»
Simple. The GNU time utility has an option for that.
But you have to ensure that you are not using your shell’s builtin time command, at least the bash builtin does not provide that option! That’s why you need to give the full path of the time utility:
Wrap time and the command you are timing in a set of brackets.
For example, the following times ls and writes the result of ls and the results of the timing into outfile :
Or, if you’d like to separate the output of the command from the captured output from time :
If you care about the command’s error output you can separate them like this while still using the built-in time command.
As you see the command’s errors go to a file (since stderr is used for time ).
Unfortunately you can’t send it to another handle (like 3>&2 ) since that will not exist anymore outside the
That said, if you can use GNU time, just do what @Tim Ludwinski said.
Источник
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 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 .
Источник
Store the output of date and watch command to a file
I am a newbie to linux and I am trying to watch a command and try to log it into a file. I tried
watch -t -n 10 «(date ‘+TIME:%H:%M:%S’ ; ps aux | grep «pattern» | wc -l)» >> logfile
and am expecting a result like
to be stored in logfile. However, when the logfile has unprintable characters in in. How do I get this kind of output from the command li
8 Answers 8
In order to do what you are looking for, a simple script (as @Ignacio pointed out) should do the trick:
I use tee instead of >> so that you can see the output on your terminal as well as capture it in your log.
This can easily be done using watch too without using any scripts.
watch -t -n 10 «(date ‘+TIME:%H:%M:%S’ ; ps aux | grep «pattern» | wc -l) | tee -a logfile»
watch is meant for output to a display. If you simply want to run a command every X seconds then you should just use a delay loop for that.
watch is an ncurses program, and is designed to be run in a console window (not redirected), which is why it’s creating a bunch of unprintable characters (those are the control characters that manage and move the cursor around for redrawing the screen).
You might try moving the date / grep commands into a script, and then call that script from a cronjob.
Ok, so I put it in a script and have the following code:
One easy alternative would be putting the command in a cmd.sh file with the following content:
And then just run:
I came across this question when I was trying to get better/logged output from du -sh $data_path . I used the «while command, do sleep» pattern found here, but used some complex AWK to give the output I wanted.
I actually did this as a oneliner, which is why there are semicolons. But to make it readable, I broke it out. The output looks like:
Here is an example I just needed for a watch on a ps axf with a timestamp at the bottom of the entire output. I am watching for when Apache fails. I had to pipe to tee for each command, the ps and the date .
Источник