What is the modified date in linux

date command in Linux with examples

date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.

Syntax:

Options with Examples
1: date (no option) : With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time zone name, and the year.

Note : Here unix system is configured in pacific daylight time.

2:-u Option: Displays the time in GMT(Greenwich Mean Time)/UTC(Coordinated Universal Time )time zone.

3: –date or -d Option: Displays the given date string in the format of date. But this will not affect the system’s actual date and time value.Rather it uses the date and time given in the form of string.
Syntax:

4:Using –date option for displaying past dates:

  • Date and time of 2 years ago.
  • Date and time of 5 seconds ago.
  • Date and time of previous day.
  • Date and time of 2 months ago.
  • Date and time of 10 days ago.

5:Using –date option for displaying future date:

  • Date and time of upcoming particular week day.
  • Date and time after two days.
  • Date and time of next day.
  • Date and time after 1 year on the current day.

6:-s or –set Option: To set the system date and time -s or –set option is used.
Syntax:

7:–file or -f Option: This is used to display the date string present at each line of file in the date and time format.This option is similar to –date option but the only difference is that in –date we can only give one date string but in a file we can give multiple date strings at each line.
Syntax:

8:-r Option: This is used to display the last modified timestamp of a datefile .
Syntax:

We can modify the timestamp of a datefile by using touch command.

9: List of Format specifiers used with date command:

Syntax:

Examples:

Источник

What’s the difference between modification date and inode’s modification date?

I’ve just found some interesting information for me in man stat :

But what’s the difference between file last modified time and the inode’s modified time? I’m writing a bakup-bash script which allows to copy only last modificated files from two almost equally directories so I need to know which value I will prefer to use 🙂

Читайте также:  Удалить дату создания файла mac os

1 Answer 1

Access: last time the contents of the file were examined.
Modify: Last time the contents of the file were changed.
Change: Last time the file’s inode was changed.

The change time includes things like modifying the permissions and ownership, while the modify time refers specifically to the files contents.

Or more precisely (from man 2 stat ):

The field st_atime is changed by file accesses, for example, by execve(2), mknod(2), pipe(2), utime(2) and read(2) (of more than zero bytes). Other routines, like mmap(2), may or may not update st_atime.

The field st_mtime is changed by file modifications, for example, by mknod(2), truncate(2), utime(2) and write(2) (of more than zero bytes). More‐ over, st_mtime of a directory is changed by the creation or deletion of files in that directory. The st_mtime field is not changed for changes in owner, group, hard link count, or mode.

The field st_ctime is changed by writing or by setting inode information (i.e., owner, group, link count, mode, etc.).

Interestingly, direct manipulation of the file times counts as modification of the inode, which will bump the ctime to the current clock time. So you can set the ctime to the current time, but you can’t set it to any other time, as you can the other two. This makes the ctime a useful canary to spot when the file’s mtime might have been moved back.

Also, while you can change the inode without changing the file contents (that is, the ctime can change without the mtime changing), the reverse is not true. Every time you modify the contents of the file you will necessarily also end up bumping the ctime.

Источник

How to display modification time of a file?

I’d like a method to find and print the modified time of a file, for use within a bash script.

I have come up with:

Though I’d like to avoid parsing ls , also it’d be useful to have the year in there.

Ideally I’d like to see an output similar to the default output of the date command.

What other useful methods are available?

2 Answers 2

Don’t use ls , this is a job for stat :

-c lets us to get specific output, here %y will get us the last modified time of the file in human readable format. To get time in seconds since Epoch use %Y :

If you want the file name too, use %n :

Set the format specifiers to suit your need. Check man stat .

Example:

If you want the output like Tue Jul 26 15:20:59 BST 2016 , use the Epoch time as input to date :

Check date ‘s format specifiers to meet your need. See man date too.

but if the date is less than 10 it misses the time. This because of the extra space before the date if less than 10. Try this:

Читайте также:  Среда разработки ассемблер для windows

The awk command prints the fields separated by all spaces (-F ‘ ‘). Hope it works. I know this doesn’t answer the original question but just a clarification on the ls command for just date and time. When you Google «ubuntu get date and time of file» it lists this question at the top, which is what I was looking for, since I don’t need the year also. For year, date and time, you could try one of the commands below. %m prints month number. %b prints month abbreviation: Drop the %H:%M if you don’t need the hour and minute. %-d doesn’t print leading zero for the day of the month.

Источник

Date Command in Linux: How to Set, Change, Format and Display Date

Home » SysAdmin » Date Command in Linux: How to Set, Change, Format and Display Date

Linux date command displays and sets the system date and time. This command also allows users to print the time in different formats and calculate future and past dates.

Read on to learn how to use the date command in Linux.

  • A system running Linux
  • A user account with root privileges
  • Access to a terminal window/command line

Linux date Command Syntax

The syntax for the date command is:

How to Use date Command in Linux

To show the current system time and date, type in the date command:

The output displays the day of the week, day of the month, month, year, current time, and time zone. By default, the date command is set to the time zone of the operating system.

The -d option allows users to operate on a specific date. For example, we can type in the following command:

You can use the —date command to display the given date string in the format of a date. This command does not affect the system’s actual date and time values, and it only prints the requested date. For example:

Linux date Command Format Options

To format the date command’s output, you can use control characters preceded by a + sign. Format controls begin with the % symbol and are substituted by their current values.

Here, the %Y character is replaced with the current year, %m with month, and %d with the day of the month:

Here are another two formatting examples:

These are the most common formatting characters for the date command:

    • %D – Display date as mm/dd/yy
    • %Y – Year (e.g., 2020)
    • %m – Month (01-12)
    • %B – Long month name (e.g., November)
    • %b – Short month name (e.g., Nov)
    • %d – Day of month (e.g., 01)
    • %j – Day of year (001-366)
    • %u – Day of week (1-7)
    • %A – Full weekday name (e.g., Friday)
    • %a – Short weekday name (e.g., Fri)
    • %H – Hour (00-23)
    • %I – Hour (01-12)
    • %M – Minute (00-59)
    • %S – Second (00-60)

To see all formatting options, run date —help or the man command man date in your terminal.

Читайте также:  Microsoft windows наклейки coa

Set or Change Date in Linux

To change the system clock manually, use the —set command. For example, to set the date and time to 5:30 PM, May 13, 2010, type:

Most Linux distributions have the system clock synchronized using the ntp or the systemd-timesyncd services, so be careful when the setting the clock manually.

Display Past Dates

Use the —date option to display past dates in Linux. The date command accepts values such as «tomorrow» , «Friday» , «last Friday» , «next Friday» , «next week» , and similar. So, use the following strings to print past dates::

Display Future Dates

The —date option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:

Display the Date String at Line of File

The —file option prints the date string present at each line of the file. Unlike the —date option, —file can present multiple date strings at each line.

This is the syntax for the —file command:

Here we use the cat command to add dates to a file and then print them with the date command:

Display Last Modified Timestamp of a Date File

When you use the -r option, the date command prints the last modification time of a file. For example, the following command prints the last time the hosts file was changed:

Override a Time Zone

By default, the date command uses the time zone defined in /etc/localtime . To use a different time zone in the environment, set the TZ variable to the desired time zone.

For example, to switch to New York time, enter:

Type in the date command to return the system to its default time zone. To see all available time zones, use the timedatectl list-timezones command.

The date command can also show the local time for a different time zone. For example, to display the local time for 4:30 PM next Monday on the Australian east coast, type:

Use date with Other Commands

You can use the date command to create file names that contain the current time and date. The input below creates a backup MySQL file in the format of the current date:

Another common use of the date command is in shell scripts. Below we assign the output of date to the date_now variable:

Use Unix Epoch Time (Epoch Converter)

You can use the date command as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.

To show the number of seconds from the epoch to the current day, use the %s format control:

To see how many seconds passed from epoch to a specific date, enter:

You now have a good understanding of how to use the date command in Linux. If you are interested in more date/time configuration options for Linux, read How to Set or Change Timezone/Date/Time on Ubuntu.

Источник

Оцените статью