- Linux – Fake File Access, Modify and Change TimeStamps
- Get a File’s TimeStamps
- Difference Between “atime”, “mtime” and “ctime”
- Change File “Access” & “Modification” Time
- Change File “Change” Time
- Stay Stealthy
- 5 Linux Touch Command Examples (How to Change File Timestamp)
- 1. Create an Empty File using touch
- 2. Change File’s Access Time using -a
- 3. Change File’s Modification Time using -m
- 4. Explicitly Setting Access and Modification time using -t and -d
- 5. Copy the Time-stamp from Another File using -r
- How to Get Last Modified Date of File in Linux
- 1. Using stat command
- 2. Using date command
- 3. Using ls -l command
- 4. Using httpie
- Output
- Conclusion
- More Articles You May Like
- 1 thought on “How to Get Last Modified Date of File in Linux”. add one
- Date Command in Linux: How to Set, Change, Format and Display Date
- Linux date Command Syntax
- How to Use date Command in Linux
- Linux date Command Format Options
- Set or Change Date in Linux
- Display Past Dates
- Display Future Dates
- Display the Date String at Line of File
- Display Last Modified Timestamp of a Date File
- Override a Time Zone
- Use date with Other Commands
- Use Unix Epoch Time (Epoch Converter)
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.
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.
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.
More Articles You May Like
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»
Источник
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.
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.
Источник