New answers tagged

2

In "Bash Get Time", they describe the arguments to date command. screen uses a similar format, but the arguments are different. Check out the "String Escapes" section of the manual (also available in the man page) for details. As far as I can see, there's no way to achieve what you want. Perhaps seconds (%s) will suffice?


2

Your format string to date is wrong. %I gives the hour in 12 hour format (01..12), you probably want %M, and then your command seems to work (on this linux box - I don't have a synology to try on).


1

25 seconds was the difference between the POSIX-compliant tz zones and the "right" tz zones during the interval 2012-07-01 until 2015-07-01. If the tzdata is that old and if default time zone for the shell running this command is a POSIX CET and the "-u" time zone is a "right" version of UTC then the "right" code will assume that the system clock violated ...


3

If you have the "suid" version of busybox, you could try to make the date command execute as root like this: File /etc/busybox.conf: ... [SUID] date = ssx root.root ...


0

You can rename file with time stamp appended to it mv TheFile.log TheFile.log.`date +"%d-%m-%Y`


0

You copy file to backup file whose name is created with appended time stamp cp foo.txt backup_`date +"%d-%m-%Y"`.txt OR: You can display content of file using "cat" and redirect output to backup file whose name is created with appended time stamp cat foo.txt > backup_`date +"%d-%m-%Y"`.txt You can also use more FORMAT controls options as per your ...


3

Not sure what you are doing wrong but the following works: for i in *; do cp "$i" $(date '+%Y%m%d')"$i"; done (you should only run this once in a directory)


2

By following the strace of the first command (date): open("/etc/localtime", O_RDONLY) It access the timezone file pointed by /etc/localtime which is /usr/share/zoneinfo/europe/Zurich in my case. So everything fine so far. The strace of the second command (date -u) gave me hints why it wasn't working properly: open("/usr/share/zoneinfo/UTC0", ...


1

It appears at now is setting the time as far in the future as possible when parsing of the time string now fails. The command batch is intended to run now, or at least as close to now as load permits. You may be able to reschedule by using atq to cat the job to an at request with the desired time. The man page for at, batch and related commands should ...


0

Take a snapshot of the log directory, one way or another. Your scenario sounds like it calls for running the tests in a virtual machine. Take a snapshot of the VM, run the tests, and once you have the test results, discard the VM state and restore the snapshot. If you want to run the tests without making a VM snapshot, you could take a backup of the log ...


1

As long as you get the script right and you don't run it in a weird environment, everything should be fine. But if something breaks the expected output of date, all bets are off. For example, if you run this snippet with IFS=-, then you'll be running something like rm /media/… 04 26* i.e. delete files beginning with 26 in the current directory. Of course ...


1

With regards to now the man at states that: You can also give times like now + count time-units, where the time-units can be minutes, hours, days, or weeks Nowhere does it say it is allowed to use at now without such an additional count of time-units, so I am not surprised you get undefined/unexpected behaviour.


1

Since there is always a space in your filenames, I'd include that in your command: rm "$(date +%F --date "Yesterday") "* # Removes old clips That should be an easy way to prevent deleting all files in the directory, as even if date doesn't return anything, it would only delete files starting with a space character (which hopefully do not exist). However ...


2

Two things jump out: You have no checking for failure of the substitution There is a race condition if the date changes between uses of the date command. You could solve them both like this: #/bin/bash # Exit if any command fails set -e dir='/media/jmartin/Cams/video' day=$(date +%F --date Yesterday) # Conbine files from the past 24 hours into a ...


5

The $(date +%F --date "Yesterday") technically isn't a variable, it's a command substitution, but that is tangential to your question. This construct could prove to be problematic if for some reason the date command wasn't in your $PATH, and thusreturned nothing - at which point it would delete everything in /video/. If you instead take that command ...


3

If you want to use the current datetime as a filename, you can use date and command substitution. $ md5sum /etc/mtab > $(date +"%Y_%m_%d_%I_%M_%p").log 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.


2

You can use logrorate -f if your system uses logrorate. This should rotate (i.e. empty) all the logs immediatelly. See logrotate(8). Edit: In fact your system does not need to use logrotate permanently, you can have prepared configuration file just for this one rotation and run it only after pushing the time backwards. Logrorate has also benefit of not ...


0

Why? Because POSIX requires it. If preceded by a '-', the timezone shall be east of the Prime Meridian; otherwise, it shall be west (which may be indicated by an optional preceding '+' ). So, this will give time near[1] Los Angeles (with any 3 letter label for time zone text): $ TZ=ANY8 date "+%Y-%m-%d %H:%M:%S %Z%z" 2016-04-23 10:47:12 ANY-0800 $ ...


1

date -s @127846398127 should do it... (But just with seconds, so I'd expect a 10-digit value.)


0

If you allow extra tooling (after all you tagged mysql) you could do it with one command from the dateutils package: $ dateconv -i %m-%d-%Y "9-2-1832" 1832-09-02 The difference to GNU date is that you can specify the input format using format specifiers (the -i parameter). That way any kind of date can be parsed. As you wanted standard ISO 8601 ...


1

GNU date isn't particularly suited for arithmetic as you intend. That's one of the reasons I wrote dateutils. Your example would boil down to: $ dateadd today -1mo -f '%Y%m' 201603 And does the right thing on the 31st of March: $ dateadd 2016-03-31 -1mo -f '%Y%m' 201602 Or what you imply by your comment (getting the last day of the previous month) ...


0

What have you tried so far ? Setting /etc/sysconfig/clock and doing the localtime symlink should address this, e.g. https://thornelabs.net/2013/04/25/rhel-6-manually-change-time-zone.html


0

Assuming your original date is "month-day-year" (September 2, 1832), something like this might work: $ date -d $(sed "s/-/\//g" <<< '9-2-1832') +%Y-%m-%d 1832-09-02 Explanation The date command, even at the current 8.25 version, won't accept a date of the format you list ('9-2-1832', a month-day-year date string with hyphens as separators). $ ...


0

Maybe you need a script that you can input the two specific dates and times. Code: #!/bin/bash if [ $# -ne 4 ];then exit 0 fi AAA=$1" "$2 BBB=$3" "$4 awk -v begintime="${AAA}" -v endtime="${BBB}" '$2" "$3>=begintime && $2" "$3<=endtime' exa > newfile.txt


2

The only real problem is that you assign to $AAA and $BBB instead of AAA and BBB. So if you do (nearly the same as your code): AAA="2015-12-11 20:00:00" BBB="2015-12-12 01:00:00" awk '$2" "$3>="'"$AAA"'" && $2" "$3<="'"$BBB"'"' file.txt > newfile.txt it should already work. But I recommend the following further changes in order to reduce ...


-1

awk is not the best tool. sed -e "/^$AAA/,/^$BBB/,p" file.txt and man sed.


2

If you define an alias such as alias ls='ls --time-style=long-iso' then ls invocations which end up displaying dates will use that.


2

The simplest way to present weekends in human readable format will be: cal -A6 Which will print the next calendar: 2016 April May June Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 1 2 3 4 5 6 7 1 2 3 4 3 4 ...


0

#!/bin/bash WEEK=0 END_MONTH=$((`date -d "6 months" +%m` + 1)) |sed 's/^0//' while true; do CURRENT_MONTH=`date -d "$WEEK weeks" +%m`|sed 's/^0//' if [[ $CURRENT_MONTH -ne $END_MONTH ]]; then date -d "$WEEK weeks saturday" +%Y-%m-%d date -d "$WEEK weeks sunday" +%Y-%m-%d else #$END_MONTH reached... exiting ...


3

Once you have an initial starting date for a Saturday and/or Sunday, you could use relative dates to do this in a bash script using a loop. #!/bin/bash SAT=$(date -dsaturday +%Y-%m-%d) SUN=$(date -dsunday +%Y-%m-%d) # 365 days/year / 2 ~= 182 days ENDSAT=$(date -d "$SAT + 182 days" +"%Y-%m-%d") ENDSUN=$(date -d "$SUN + 182 days" +"%Y-%m-%d") echo $SAT ...



Top 50 recent answers are included