Modify time on linux

Linux Set Date and Time From a Command Prompt

H ow can I set the system date and time from the command prompt (bash shell)? I don’t have GUI installed and I am login over ssh session. How can I set date under Linux operating systems?

Use the date command to display the current date and time or set the system date / time over ssh session. You can also run the date command from X terminal as root user.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements None
Est. reading time 2m

This is useful if the Linux server time and/or date is wrong, and you need to set it to new values from the shell prompt.

You must login as root user to use date command.

Linux Display Current Date and Time

Just type the date command:
$ date
Sample outputs:

Linux Display The Hardware Clock (RTC)

Type the following hwclock command to read the Hardware Clock and display the time on screen:
# hwclock -r
OR
# hwclock —show
$ sudo hwclock —show —verbose

OR show it in Coordinated Universal time (UTC):
# hwclock —show —utc
Sample outputs:

Linux Set Date Command Example

Use the following syntax to set new data and time:
date —set=»STRING»
For example, set new data to 2 Oct 2006 18:00:00, type the following command as root user:
# date -s «2 OCT 2006 18:00:00»
OR
# date —set=»2 OCT 2006 18:00:00″
You can also simplify format using following syntax:
# date +%Y%m%d -s «20081128»

Linux Set Time Examples

To set time use the following syntax:
# date +%T -s «10:13:13»
Where,

Use %p locale’s equivalent of either AM or PM, enter:
# date +%T%p -s «6:10:30AM»
# date +%T%p -s «12:10:30PM»

How do I set the Hardware Clock to the current System Time?

Use the following syntax:
# hwclock —systohc
OR
# hwclock -w

A note about systemd based Linux system

With systemd based system you need to use the timedatectl command to set or view the current date and time. Most modern distro such as RHEL/CentOS v.7.x+, Fedora Linux, Debian, Ubuntu, Arch Linux and other systemd based system need to the timedatectl utility. Please note that the above command should work on modern system too.

timedatectl: Display the current date and time

Type the following command:
$ timedatectl

Fig.01: Systemd Linux timedatecetl command to display the current date and time

How do I change the current date using the timedatectl command?

To change the current date, type the following command as root user:
# timedatectl set-time YYYY-MM-DD
OR
$ sudo timedatectl set-time YYYY-MM-DD
For example set the current date to 2015-12-01 (1st, Dec, 2015):
# timedatectl set-time ‘2015-12-01’
# timedatectl
Sample outputs:

To change both the date and time, use the following syntax:
# timedatectl set-time YYYY-MM-DD HH:MM:SS
Where,

  1. HH : An hour.
  2. MM : A minute.
  3. SS : A second, all typed in two-digit form.
  4. YYYY: A four-digit year.
  5. MM : A two-digit month.
  6. DD: A two-digit day of the month.

For example, set the date ’23rd Nov 2015′ and time to ‘8:10:40 am’, enter:
# timedatectl set-time ‘2015-11-23 08:10:40’
# date

  • 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

How do I set the current time only?

The syntax is:
# timedatectl set-time HH:MM:SS
# timedatectl set-time ’10:42:43′
# date
Sample outputs:

How do I set the time zone using timedatectl command?

To see list all available time zones, enter:
$ timedatectl list-timezones
$ timedatectl list-timezones | more
$ timedatectl list-timezones | grep -i asia
$ timedatectl list-timezones | grep America/New
To set the time zone to ‘Asia/Kolkata’, enter:
# timedatectl set-timezone ‘Asia/Kolkata’
Verify it:
# timedatectl

How do I synchronizing the system clock with a remote server using NTP?

Simply type the following command:
# timedatectl set-ntp yes
Verify it:
$ timedatectl
Sample outputs:

Conclusion

Linux users can use date command to print or set the system date and time. Systemd based Linux users can use timedatectl to control the system time and date.

  • You can also set new timzone using this mini-howto.
  • Man pages – timedatectl(8)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Linux – Fake File Access, Modify and Change TimeStamps

Files in Linux have 3 types of timestamps: atime (access), mtime (modify) and ctime (change).

Someday you may have a situation when you would like to fake a timestamps of some file.

atime and mtime timestamps can be easily changed using touch command, but there is no a standard way to set a different ctime timestamp.

As a possible workaround you can set the system time to the ctime you want to impose, then touch the file and then restore the system time.

Read the below article to learn how to change a file’s timestamps and keep anonymity.

Cool Tip: To cover up the traces – clear the last login history. Read more →

Get a File’s TimeStamps

Use the stat command to see the current file’s timestamps:

Difference Between “atime”, “mtime” and “ctime”

Timestamp When it gets updated?
atime Access time gets updated when you open a file or when a file is used for other operations like grep, cat, head and so on.
mtime Modify time gets updated when you whenever update content of a file or save a file.
ctime Change time gets updated when the file attributes are changed, like changing the owner, changing the permission or moving it to another filesystem, but will also be updated when you modify a file.

Change File “Access” & “Modification” Time

Change a file’s atime (access time):

Change a file’s mtime (modification time):

Change File “Change” Time

Nevertheless, if you are ready to risk, it is possible;)

Firstly you can set the system time to the ctime you want to impose.

Then touch the file and immediately rollback the system time.

Unexpected impact: Modification of a system time may cause an unexpected impact! Use the below commands on your own risk!

Save the current system’s date and time in the variable NOW :

Set the fake date and time (requires root):

Touch the file to fake the all timestamps:

Rollback the date and time (requires root):

Cool Tip: Clear the BASH history effectively! Read more →

To speedup modification and reduce the possible impact, execute the above commands as follows:

Stay Stealthy

To stay stealthy – unset the variable, clear logs and history.

Unset the variable NOW :

Remove the information about changed time from /var/log/messages (requires root):

atime and mtime timestamps can be easily changed using touch command, but there is no a standard way to set a different ctime timestamp.

Cool Tip: Want to stay anonymous? Learn how to use PROXY on the Linux command line. Read more →

Clear the last login history (requires root):

Источник

Linux — modify file modify/access/change time

I read an article about new steganographic method: «Time is on my side: Steganography in filesystem metadata» http://www.sciencedirect.com/science/article/pii/S1742287616300433. It is very interesting and makes me thinking about one thing.

Is it possible to manipulate file modify/access/change time metedata using C or any other language in Linux OS? I found method which is using touch command, but it creates file with specific tag value. I would like to find way to modify this metadata in existing one.

2 Answers 2

I came across this thread and I had to share my findings (as mentioned this site is a repository for knowledge):

«touch» is a command line application and in order to execute it (from c++ application) you need to run system() which is discouraged. more on touch: http://www.linfo.org/touch.html

The programmatic way to set the access and modification timestamps of a file is using one of the following system calls: utime, utimes, futimes, futimens (simple internet search will give you the man page of them) i.e http://www.tin.org/bin/man.cgi?section=2&topic=utimensat

Linux has 3 time stamps associated to a file:

  1. Access — the last time the file was read
  2. Modify — the last time the file was modified (content has been modified)
  3. Change — the last time meta data of the file was changed (e.g. permissions) extra reading here: https://unix.stackexchange.com/questions/2464/timestamp-modification-time-and-created-time-of-a-file/2465#2465?newreg=b4e0ee2ef0734b8792240c205e420c55

It’s relatively easy to modify the ‘access’ and ‘modify’ timestamps using the above commands, but modifying the change timestamp (last time permissions were changed) is harder. Thread on this here: Setting creation or change timestamps

Источник

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 .

Источник

5 Linux Touch Command Examples (How to Change File Timestamp)

Every file in Linux is associated with timestamps, which specifies the last access time, last modification time and last change time.

Whenever we create a new file, or modify an existing file or its attributes, these timestamps will be updated automatically.

Touch command is used to change these timestamps (access time, modification time, and change time of a file).

1. Create an Empty File using touch

You can create an empty file using touch command. The following example will create a zero byte new file named tgs.txt.

You can also use -c option to avoid creating new files. If you use -c option, and if a file doesn’t exists, touch will not create the file.

Commands like ls command and find command uses these timestamp information for listing and finding files.

You can also create more than 1 files from a single touch command. The following example will create 4 files named a, b, c, and d.

2. Change File’s Access Time using -a

We can change the access time of a file using -a option. By default it will take the current system time and update the atime field.

Before touch command is executed:

After the above touch command (Please note that the access time is changed):

3. Change File’s Modification Time using -m

You can change the modification time of a file using -m option.

The above method can be used to change the mtime of all obj files, when using make utility.

NOTE: It is not possible to change the ctime using touch command

4. Explicitly Setting Access and Modification time using -t and -d

Instead of taking the current time-stamp, you can explicitly specify the time using -t and -d options.

The format for specifying -t is [[CC]YY]MMDDhhmm[.SS]

The following explains the above format:

  • CC – Specifies the first two digits of the year
  • YY – Specifies the last two digits of the year. If the value of the YY is between 70 and 99, the value of the CC digits is assumed to be 19. If the value of the YY is between 00 and 37, the value of the CC digits is assumed to be 20. It is not possible to set the date beyond January 18, 2038.
  • MM – Specifies the month
  • DD – Specifies the date
  • hh – Specifies the hour
  • mm – Specifies the minute
  • SS – Specifies the seconds

Verify the above change using stat command:

You can also use a string to change the time

For developers, touch command will be really helpful when you are working with Makefiles

5. Copy the Time-stamp from Another File using -r

You can also take a file as a reference, and update the time for other files, so that both file will hold the same time.

Источник

Читайте также:  Get file list linux
Оцените статью