Which of the following commands can be used to update the last modified timestamp on a file
The touch command is a Linux command-line tool that can be used to create an empty file and modify the timestamps of files. Show
Timestamps stores the information of each file and folder including, access time, modify time and change time. Whenever you create or change an existing file, the timestamps value will be updated automatically. There are three types of timestamps :
In this tutorial, we will learn how to use Touch command with practical examples. Basic Syntax for Linux Touch CommandThe basic syntax of Touch command is shown below: touch [OPTION] FILENAME A brief explanation of each option is specified below:
Create a Single File with TouchYou can create a single empty file by running the following command: touch file1 You can now verify your file with the following command: ls -l file1 You should see the following output: -rw-rw-r-- 1 vyom vyom 0 Mar 29 10:44 file1 you can avoid creating new files if a file doesn’t exists, use the touch command with -c option as shown below: Once you are finished, you can proceed to the next example. Create a Multiple Files with TouchTo create multiple files named file2, file3, file4 and file5, run the following command: touch file2 file3 file4 file5 You can now verify all the files with the following command: ls -l file* You should see the following output: -rw-rw-r-- 1 vyom vyom 0 Mar 29 10:44 file1 Example: Change Access Time of File with TouchIf you want to change/update the access time of an existing file, you can use -a option file touch command. For example, to change the access time of a file named file1 to the current time, run the following command: touch -a file1 Next, verify whether the access time of a file has been changed or not using the following command: stat file1 You should see the following output:
Example: Change Modification Time of File with TouchYou can change the modification time of a file using -m option. For example, to change the modification time of a file named file2, run the following command: touch -m file2 Next, verify the modification time of a file with the following command: stat file2 You should see the following output: File: ‘file2’ Example: Change Access and Modification Time of File with TouchYou can change both access and modification time of a file using the option -a and -m together. For example, change the access and modification time of a file named file3 to the current time using the following command: touch -am file3 You can now verify the access and modification time of a file with the following command: stat file3 You should see the following output: File: ‘file3’ Example: Set Specific Access and Modification Time of a File with TouchYou can also set specific access and modification time of a file using the following syntax: touch -c -t YYYYMMDDhhmm FILENAME Where:
For example, set the access and modification time of a file named file4 to 11:25 AM Jan 10 of the year 2020, run the following command: touch -c -t 202001101125 file4 You can verify the access and modification time of a file with the following command: stat file4 You should see the following output: File: ‘file4’ Example: You can also set specific date and time in human readable format by using the -d option. For example, set the date of a file named file4 to 15 Feb run the following command: touch -d '15 Feb' file4 This command will set the date of a file4 to 15 Feb and the time is automatically set to 00:00. You can verify it with the following command: stat file4 You should see the following output: File: ‘file4’ Example: Set the Timestamp of Another FileYou can also take a timestamp from another file and use it to set the time for other files using the -r option. For example, set the timestamp of a file named file5 using the timestamp value of file1 using the following command: touch file5 -r file1 You can now verify the timestamp value of file5 with the following command: stat file5 You should see the following output: File: ‘file5’ Example: ConclusionIn the above guide, we’ve learned How to Use the Touch Command with several examples, including how to change Access, Modified and Created times. We hope you can now use the touch command to change the access and modification time of file and create files as well. Feel free to ask any questions below and post your comments! How do I find the latest modified file in Linux?Finding Files Modified on a Specific Date in Linux:
You can use the ls command to list files including their modification date by adding the -lt flag as shown in the example below. The flag -l is used to format the output as a log. The flag -t is used to list last modified files, newer first.
Which of the following commands can be used to recursively search through the directory tree in search?You can use grep command or find command as follows to search all files for a string or words recursively.
How can I see who modified a file in Linux?You might be able to narrow the list down.. use stat command (ex: stat , See this). Find the Modify time.. Use last command to see the log in history (see this). Compare the log-in/log-out times with the file's Modify timestamp.. What command can be used to display the last five lines of a text file?To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file. Try using tail to look at the last five lines of your .
|