Linux set modification date

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):

Источник

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.

Читайте также:  С чем синхронизировать время windows

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.

Источник

How to Get Last Modified Date of File in Linux

Sometimes, you may be required to check detailed information about a file (timestamp) such as its last modified date. This can come in handy when you want to check when the file was last edited. Additionally, it ensures that you have the latest version of the file.

In this article, you will learn 4 ways to get the last modified date of file in Linux.

1. Using stat command

The ls -l command is just okay in giving you basic information about a file such as file ownership and permissions, file size, and creation date. The stat command returns detailed information file attributes such as the last time the file was accessed and modified.

The syntax is quite simple. stat is followed by the file name or the full path to the file.

From the above output, we can clearly see when the file was last accessed ( Access date ), Modify date, Change date among other parameters.

If you wish to view the modified date only and leave out all the other information, run the following command:

The -c option is used to return the date in a custom format, while the ‘%y’ flag displays the last modification time. For directories, the syntax remains the same. Simply replace the file name with that of the directory.

2. Using date command

The date command in its basic syntax displays the current date. However, when used with the -r option, you can display the last modification date of a file as shown.

Читайте также:  Bash под windows 10

3. Using ls -l command

The ls -l command is usually used for long listing — display additional information about a file such as file ownership and permissions, size and creation date. To list and display the last modified times, use the lt option as shown.

4. Using httpie

Another way you can check the last modified date is by using the httpie HTTP command-line client tool. The tool is usually used for interacting with HTTP servers and APIs and can also check when a file residing on a web server was last modified.

But first, you need to install it using the command:

On Ubuntu / Debian / Mint, run the command:

To check when a file on a web server was last modified, use the syntax:

Output

Conclusion

This wraps up this article. In this guide, we have featured various ways that you can use to list the last modified date of a file on a Linux system, and even a file hosted on a web server using the httpie tool. Hopefully, you won’t have an issue viewing when files were last modified.

1 thought on “How to Get Last Modified Date of File in Linux”. add one

I prefer using ‘ls’ over all the others because ls allows you to control precisely how the date and time are displayed. I believe stat only gives the choice between seconds-since-epoch and human-readable, with no control over the human-readable format.

For ls, the relevant option is ‘—time-style’ and its format specifiers are fairly straightforward, using the same specifiers used by /bin/date. See ‘man date’ for all the available specifiers. My personal favorite is —time-style=»+%Y-%m-%d %H:%M:%S». I use this alias for my day-to-day ls needs.

alias l=»/bin/ls —time-style=\»+%Y-%m-%d %H:%M:%S\» —group-directories-first -lLFAGv»

Источник

How to Find and Sort Files Based on Modification Date and Time in Linux

Usually, we are in habit of saving a lot of information in form of files on our system. Some, hidden files, some kept in a separate folder created for our ease of understanding, while some as it is. But, this whole stuff fills our directories; usually desktop, making it look like a mess. But, the problem arises when we need to search for a particular file modified on particular date and time in this huge collection.

Find and Sort Files by Date and Time in Linux

People comfortable with GUI’s can find it using File Manager, which lists files in long listing format, making it easy to figure out what we wanted, but those users having habit of black screens, or even anyone working on servers which are devoid of GUI’s would want a simple command or set of commands that could ease out their search.

Real beauty of Linux shows here, as Linux has a collection of commands which if used separately or together can help to search for a file, or sort a collection of files according to their name, date of modification, time of creation, or even any filter you could think of applying to get your result.

Here, we will unveil the real strength of Linux by examining a set of commands which can help sorting a file or even a list of files by Date and Time.

Linux Utilities to Sort Files in Linux

Some basic Linux command line utilities that are just sufficient for sorting a directory based on Date and Time are:

ls command

ls – Listing contents of directory, this utility can list the files and directories and can even list all the status information about them including: date and time of modification or access, permissions, size, owner, group etc.

We’ve already covered many articles on Linux ls command and sort command, you can find them below:

sort command

sort – This command can be used to sort the output of any search just by any field or any particular column of the field.

We’ve already covered two articles on Linux sort command, you can find them below:

These commands are in themselves very powerful commands to master if you work on black screens and have to deal with lots of files, just to get the one you want.

Читайте также:  Линукс не для десктопа

Some Ways to Sort Files using Date and Time

Below are the list of commands to sort based on Date and Time.

1. List Files Based on Modification Time

The below command lists files in long listing format, and sorts files based on modification time, newest first. To sort in reverse order, use ‘-r’ switch with this command.

2. List Files Based on Last Access Time

Listing of files in directory based on last access time, i.e. based on time the file was last accessed, not modified.

3. List Files Based on Last Modification Time

Listing of files in directory based on last modification time of file’s status information, or the ‘ctime’ . This command would list that file first whose any status information like: owner, group, permissions, size etc has been recently changed.

If ‘-a’ switch is used with above commands, they can list and sort even the hidden files in current directory, and ‘-r’ switch lists the output in reverse order.

For more in-depth sorting, like sorting on Output of find command, however ls can also be used, but there ‘sort’ proves more helpful as the output may not have only file name but any fields desired by user.

Below commands show usage of sort with find command to sort the list of files based on Date and Time.

To learn more about find command, follow this link: 35 Practical Examples of ‘find’ Command in Linux

4. Sorting Files based on Month

Here, we use find command to find all files in root (‘/’) directory and then print the result as: Month in which file was accessed and then filename. Of that complete result, here we list out top 11 entries.

The below command sorts the output using key as first field, specified by ‘-k1’ and then it sorts on Month as specified by ‘M’ ahead of it.

5. Sort Files Based on Date

Here, again we use find command to find all the files in root directory, but now we will print the result as: last date the file was accessed, last time the file was accessed and then filename. Of that we take out top 11 entries.

The below sort command first sorts on basis of last digit of the year, then sorts on basis of last digit of month in reverse order and finally sorts on basis of first field. Here, ‘1.8‘ means 8th column of first field and ‘n’ ahead of it means numerical sort, while ‘r’ indicates reverse order sorting.

6. Sorting Files Based on Time

Here, again we use find command to list out top 11 files in root directory and print the result in format: last time file was accessed and then filename.

The below command sorts the output based on first column of the first field of the output which is first digit of hour.

7. Sorting Ouptut of ls -l based on Date

This command sorts the output of ‘ls -l’ command based on 6th field month wise, then based on 7th field which is date, numerically.

Conclusion

Likewise, by having some knowledge of sort command, you can sort almost any listing based on any field and even its any column you desire. These were some of tricks to help you sort files based on Date or Time. You can have your own tricks build based on these. However, if you have any other interesting trick, you can always mention that in your comments.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

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