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命令的方案. 我们在 ...
随机推荐
- Python读写Redis数据库
import redis class Database: def __init__(self): self.host = 'localhost' self.port = 6379 def write( ...
- BZOJ 1044
1044: [HAOI2008]木棍分割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1393 Solved: 497[Submit][Statu ...
- BZOJ 1754: [Usaco2005 qua]Bull Math
Description Bulls are so much better at math than the cows. They can multiply huge integers together ...
- 为网站添加一个图标icon
<link rel="icon" href="/favicon.ico" type="image/x-icon"/> <l ...
- 【HDU 3483】 A Very Simple Problem (二项式展开+矩阵加速)
A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- 花20分钟写的-大白话讲解如何给github上项目贡献代码
原文地址:http://site.douban.com/196781/widget/notes/12161495/note/269163206/ 本文献给对git很迷茫的新手,注意是新手,但至少会点基 ...
- ruby条件控制结构
一.比较语句 大部分和其他的语言一样,这里注意<=>. 条件语句 如下几种形式 if if ..else.. end if..elsif..else..end unless(if not) ...
- 编程实现改变win7主题
一 : 解析问题 1. Windows 7 主题在:%windir%\Resources\Themes : 2: 我们通过shell 命令 (这个是msdn中提到的) rundll32.exe ...
- dev中gridview控件 z
目录:一.客户端常用1.常用API2.聚焦行变更事件3.客户端选择多行4.客户端选择行5. 获取选择的行数目6.单击行时,选中行7.通过checkbox 选择行8.选择所有行9.启动编辑框,Conta ...
- python 零散记录(六) callable 函数参数 作用域 递归
callable()函数: 检查对象是否可调用,所谓可调用是指那些具有doc string的东西是可以调用的. 函数的参数变化,可变与不可变对象: 首先,数字 字符串 元组是不可变的,只能替换. 对以 ...