In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces what touch commands are in Linux, the content is very detailed, interested friends can refer to, hope to be helpful to you.
What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.
The touch command is used to create empty files and can also change the timestamps of existing files on Unix and Linux systems. The change timestamp mentioned here means updating the access and modification time of files and directories.
Let's look at the syntax and options of the touch command:
Syntax:
# touch {options} {File}
Options used in the touch command:
In this article, we will introduce nine examples of useful touch commands in Linux.
Example: 1 create an empty file using touch
To create an empty file using the touch command on a Linux system, type touch, and then enter a file name. As follows:
[root@linuxtechi ~] # touch devops.txt [root@linuxtechi ~] # ls-l devops.txt-rw-r--r--. 1 root root 0 Mar 29 22:39 devops.txt
Example: 2 create batch empty files using touch
There may be situations where we have to create a large number of empty files for some tests, which can be easily achieved using the touch command:
[root@linuxtechi ~] # touch sysadm- {1.. 20} .txt
In the above example, we created 20 empty files called sysadm-1.txt to sysadm-20.txt, and you can change the names and numbers as needed.
Example: 3 change / update the access time of files and directories
Suppose we want to change the access time of the file named devops.txt, use the-an option in the touch command, and then enter the file name. As follows:
[root@linuxtechi] # touch-a devops.txt
Now use the stat command to verify that the access time for the file has been updated:
[root@linuxtechi ~] # stat devops.txt File: 'devops.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 67324178 Links: 1Access: (0644 Maxima RW Muhammad Rafael Rameto -) Uid: (0 / root) Gid: (0 / root) Context: unconfined_u:object_r:admin_home_t:s0Access: 2018-03-29 23 015 3V 10.902000000-0400Modify: 2018-03- 29 22 39 29.365000000-0400Change: 2018-03-29 23V 03lo 10.902000000-0400
Change the access time of the directory:
Suppose we have a nfsshare folder under the / mnt directory, let's change the access time of this folder with the following command:
[root@linuxtechi ~] # touch-m / mnt/nfsshare/ [root@linuxtechi ~] # stat / mnt/nfsshare/ File:'/ mnt/nfsshare/' Size: 6 Blocks: 0 IO Block: 4096 directoryDevice: fd00h/64768d Inode: 2258 Links: 2Access: (0755/drwxr-xr-x) Uid: (0 / root) Gid: (0 / root) Context: unconfined_u:object_r:mnt_t:s0Access: 2018-03-29 23: 34 38.095000000-0400Modify: 2018-03-03 10 Birth 42purl 45.194000000-0500Change: 2018-03-29 23mer 34shul 38.095000000-0400 000:-
Example: 4 change the access time without creating a new file
In some cases, if the file exists, we want to change the access time of the file and avoid creating the file. You can use the-c option in the touch command, and if the file exists, we can change the access time of the file, or we won't create it if it doesn't exist.
[root@linuxtechi ~] # touch-c sysadm-20.txt [root@linuxtechi ~] # touch-c winadm-20.txt [root@linuxtechi ~] # ls-l winadm-20.txtls: cannot access winadm-20.txt: No such file or directory
Example: 5 change the modification time of files and directories
Using the-m option in the touch command, we can change the modification time of files and directories.
Let's change the change time to the devops.txt file:
[root@linuxtechi] # touch-m devops.txt
Now use the stat command to verify that the modification time has changed:
[root@linuxtechi ~] # stat devops.txt File: 'devops.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 67324178 Links: 1Access: (0644 Maxima RW Muhammad Rafael Rameto -) Uid: (0 / root) Gid: (0 / root) Context: unconfined_u:object_r:admin_home_t:s0Access: 2018-03-29 23 015 3V 10.902000000-0400Modify: 2018-03- 29 2315 59 49.106000000-0400Change: 2018-03-29 23 23 15 49 106000000-0400 Birth:-
Similarly, we can change the modification time of a directory:
[root@linuxtechi] # touch-m / mnt/nfsshare/
Use stat cross-validation access and modification time:
[root@linuxtechi ~] # stat devops.txt File: 'devops.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 67324178 Links: 1Access: (0644 Maxima RW Muhammad Rafael Rameto -) Uid: (0 / root) Gid: (0 / root) Context: unconfined_u:object_r:admin_home_t:s0Access: 2018-03-30 0000 Vane 06V 20.145000000-0400Modify: 2018-03- 3000 0400Change 06VR 20.145000000-0400Change: 2018-03-30 0000VOR 06purl 20.145000000-0400 Birth:-
Example: 7 set the access and modification time to a specific date and time
Whenever we use the touch command to change the access and modification time of a file or directory, it sets the current time to the access and modification time of that file or directory.
Suppose we want to set a specific date and time to the access and modification time of the file, which can be done using the-c and-t options in the touch command.
The date and time can be specified in the following format:
{CCYY} MMDDhhmm.ss
Where:
CC-the first two digits of the year
YY-the last two digits of the year
MM-month (01-12)
DD-Day (01-31)
Hh-hour (00-23)
Mm-minutes (00-59)
Let's set the access and modification time of the devops.txt file to a future time (18:20, October 19, 2025).
[root@linuxtechi] # touch-c-t 202510191820 devops.txt
Use the stat command to view update access and modification times:
Set the access and modification time based on the date string, use the-d option in the touch command, and then specify the date string, followed by the file name. As follows:
The copy code is as follows:
[root@linuxtechi] # touch-c-d "2010-02-07 20 purl 15purl 12.000000000 + 0530" sysadm-29.txt
Use the stat command to verify the status of the file:
[root@linuxtechi ~] # stat sysadm-20.txt File: 'sysadm-20.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 67324189 Links: 1Access: (0644 Maxima RW Muhammad Rameto Rameto -) Uid: (0 / root) Gid: (0 / root) Context: unconfined_u:object_r:admin_home_t:s0Access: 2010-02-07 2015 IO Block 12.0000000 + 0530Modify: 2010 -02-07 20 15 12.000000000 + 0530Change: 2018-03-30 10V 23purl 31.584000000 + 0530 Birth:-
Note: in the above command, if we do not specify-c, then the touch command will create a new file and set the timestamp to the one given in the command if the file does not exist on the system.
Example: 8 use reference files to set timestamps (- r)
In the touch command, we can use the reference file to set the timestamp of the file or directory. Suppose I want to set the same timestamp on the devops.txt file as the file sysadm-20.txt file, which can be easily achieved by using the-r option in the touch command.
Syntax:
# touch-r {reference file} Real file [root@linuxtechi ~] # touch-r sysadm-20.txt devops.txt
Example: 9 change access and modification time on symbolic link files
By default, whenever we try to change the timestamp of a symbolic link file using the touch command, it only changes the timestamp of the original file. If you want to change the timestamp of the symbolic link file, you can use the-h option in the touch command to do so.
Syntax:
# touch-h {symbolic link file} [root@linuxtechi opt] # ls-l / root/linuxgeeks.txtlrwxrwxrwx. 1 root root 15 Mar 3010: 56 / root/linuxgeeks.txt-> linuxadmins.txt [root@linuxtechi] # touch-t 203010191820-h linuxgeeks.txt [root@linuxtechi] # ls-l linuxgeeks.txtlrwxrwxrwx. 1 root root 15 Oct 19 2030 linuxgeeks.txt-> linuxadmins.txt about which touch commands in Linux are shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.