8 Pratical Examples of Linux “Touch” Command--reference
In Linux every single file is associated with timestamps, and every file stores the information of last access time, last modification time and last change time. So, whenever we create new file, access or modify an existing file, the timestamps of that file automatically updated.
In this article we will cover some useful practical examples of Linux touch command. Thetouch command is a standard program for Unix/Linux operating systems, that is used to create, change and modify timestamps of a file. Before heading up for touch command examples, please check out the following options.
Touch Command Options
- -a, change the access time only
- -c, if the file does not exist, do not create it
- -d, update the access and modification times
- -m, change the modification time only
- -r, use the access and modification times of file
- -t, creates a file using a specified time
1. How to Create an Empty File
The following touch command creates an empty (zero byte) new file called sheena.
# touch sheena
2. How to Create Multiple Files
By using touch command, you can also create more than one single file. For example the following command will create 3 files named, sheena, meena and leena.
# touch sheena meena leena
3. 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:
$ stat tgs.txt File: `tgs.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 394283 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan)
Access: 2012-10-18 23:58:21.663514407 +0530
Modify: 2012-10-18 23:58:21.663514407 +0530
Change: 2012-10-18 23:58:21.663514407 +0530
$ touch -a tgs.txt
After the above touch command (Please note that the access time is changed):
$ stat tgs.txt File: `tgs.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 394283 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan)
Access: 2012-10-19 00:08:23.559514525 +0530
Modify: 2012-10-18 23:58:21.663514407 +0530
Change: 2012-10-19 00:08:23.559514525 +0530
4. How to Avoid Creating New File
Using -c option with touch command avoids creating new files. For example the following command will not create a file called leena if it does not exists.
# touch -c leena
5. How to Change File Modification Time
If you like to change the only modification time of a file called leena, then use the -moption with touch command. Please note it will only updates the last modification times (not the access times) of the file.
# touch -m leena
6. 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]
$ touch -t [[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
For example:
$ touch -a -m -t 203801181205.09 tgs.txt
Verify the above change using stat command:
$ stat tgs.txt
File: `tgs.txt'
Size: 3 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 394283 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan)
Access: 2038-01-18 12:05:09.000000000 +0530
Modify: 2038-01-18 12:05:09.000000000 +0530
Change: 2012-10-19 00:40:58.763514502 +0530
You can also use a string to change the time
Another example:
$ touch -d "2012-10-19 12:12:12.000000000 +0530" tgs.txt
For developers, touch command will be really helpful when you are working with Makefiles
7. How to Use the time stamp of another File
The following touch command with -r option, will update the time-stamp of file leena with the time-stamp of meena file. So, both the file holds the same time stamp.
# touch -r leena meena
8. Create a File using a specified time
If you would like to create a file with specified time other than the current time, then the format should be.
# touch -t YYMMDDHHMM.SS tecmint
For example the below command touch command with -t option will gives the tecmint file a time stamp of 18:30:55 p.m. on December 10, 2012.
# touch -t 201212101830.55 tecmint
9. 批量创建文件
#!/bin/bash
#create file in batch
for name in {..}.txt
do
touch $name
done
We’ve almost covered all the options available in the touch command for more options use “man touch“. If we’ve still missed any options and you would like to include in this list, please update us via comment box.
reference :
http://www.tecmint.com/8-pratical-examples-of-linux-touch-command/
http://www.thegeekstuff.com/2012/11/linux-touch-command/
linux shell 脚本攻略
8 Pratical Examples of Linux “Touch” Command--reference的更多相关文章
- 15 Practical Grep Command Examples In Linux / UNIX
You should get a grip on the Linux grep command. This is part of the on-going 15 Examples series, wh ...
- 13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法
Cat (串联) 命令是Linux/Unix开源系统中比较常用的一个命令.我们可以通过Cat命令创建一个或多个文件,查看文件内容,串联文件并将内容输出到终端设备或新的文件当中,这篇文章我们将会以实例的 ...
- 12 Linux Which Command, Whatis Command, Whereis Command Examples
This Linux tutorial will explain the three "W" commands. The three "W"s are what ...
- 13 Basic Cat Command Examples in Linux
FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/ The cat (short for “concatenate ...
- 15 Basic ‘ls’ Command Examples in Linux
FROM: http://www.tecmint.com/15-basic-ls-command-examples-in-linux/ ls command is one of the most fr ...
- 18 Tar Command Examples in Linux
FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ...
- Linux touch命令详解
Linux touch命令 Linux touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 用法: touch [-acfm][-d<日 ...
- Linux Touch命令的8种使用技巧
Linux touch命令不仅可以用于在Linux上创建空文件. 您可以使用它来更改现有文件的时间戳,包括其访问权限和修改时间. 本文介绍了8种可以通过Linux终端使用touch命令的方案. 我们在 ...
- Linux Touch命令的8种常见使用方法
Linux touch命令不仅可以用于在Linux上创建空文件. 您可以使用它来更改现有文件的时间戳,包括其访问权限和修改时间. 本文介绍了8种可以通过Linux终端使用touch命令的方案. 我们在 ...
随机推荐
- awsomeplayer结构认识
把这个搞明白,算是顿悟的一个真实例子.怎么也搞不懂的架构,突然就想明白了.不过这其实是一个思维的过程. 当然如果你想明白这些东西,至少要非常清楚一个概念:接口. 我只是一个半路出家的开发者,我真正明白 ...
- View以自身中心旋转的代码解惑
matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); 经常在中心旋转的应用中看到这段代码. ...
- Excel列表部分列表隐藏与取消隐藏
Excel列表部分列表隐藏与取消隐藏 2014-2-19 隐藏:选中需要隐藏的列(选中A.B.C....),右键单击所选部分,选择"隐藏"即可. 取消隐藏:从A选中至所见表格最后的 ...
- linux权限掩码
我的博客:www.while0.com 主要是在新建文件或目录的时候,控制新文件或目录的默认权限. 文件:新建文件默认没有x权限,故新建文件在umask为000时最大权限是666. 目录:新建目录默认 ...
- 4.android.mk编写规范
Android.mk是Android提供的一种makefile文件,用来指定诸如编译生成so库名.引用的头文件目录.需要编译的.c/.cpp文件和.a静态库文件等.要掌握jni,就必须熟练掌握Andr ...
- Oracle DBMS_SESSION
Version 11.1 General Purpose Try dbms_session.reset_package. This call will reset all packages var ...
- POJ_3045_Cow_Acrobats_(贪心)
描述 http://poj.org/problem?id=3045 n头牛,每头牛都有重量w[i]和力量s[i].把这n头牛落起来,每头牛会有一个危险值,危险值是它上面所有牛的重量和减去它的力量.求危 ...
- 修改mysql 数据库密码
第1种︰使用 mysqladmin命令 shell>mysqladmin -u root password new_password 如果忘记了MySQL的root密码,可以用以下方法重新设置: ...
- 利用dhtmlxGrid做的表格排序的功能。
dhtmlxGrid支持tree和grid. grid之间.grid内部进行拖拽, 如在grid内部进行拖拽,可以增加一行:在grid之间拖拽,第一个grid的记录删除,第二个grid增加一行记录.
- Android优秀开源项目
本文转自:http://blog.tisa7.com/android_open_source_projects Android优秀开源项目 Android经典的开源项目其实非常多,但是国内的博客总是拿 ...