linux和unix文件没有创建时间,小弟我使用过的Linux命令之touch - 创建文件或修改文件时间...

我使用过的Linux命令之touch - 创建文件或修改文件时间

我使用过的Linux命令之touch - 创建文件或修改文件时间

本文链接:http://codingstandards.iteye.com/blog/781916

(转载请注明出处)

用途说明

touch命令经常用来创建空文件或者更新文件时间。创建空文件的目的通常是作为程序运行的标志,当程序执行结束前又将该文件删除。而更新文件时间通常是为了让某些软件能够正常执行。

常用参数

-t 用于指定时间。格式可以是MMDDhhmm或者yyyyMMDDhhmm。

-r 设置与file相同的时间。

使用示例

示例一 创建新文件

[root@jfht ~]# ls -l new.txt

ls: new.txt: 没有那个文件或目录

[root@jfht ~]# touch new.txt

[root@jfht ~]#ls -l new.txt

-rw-r--r-- 1 root root 0 10-11 22:40 new.txt

[root@jfht ~]#

示例二 更改文件时间为当前时间

[root@jfht ~]# ls -l new.txt

-rw-r--r-- 1 root root 0 10-11 22:40 new.txt

[root@jfht ~]# touch new.txt

[root@jfht ~]# ls -l new.txt

-rw-r--r-- 1 root root 0 10-11 22:41 new.txt

示例三 更改文件时间为指定时间

[root@jfht ~]# date

2010年 10月 11日 星期一 22:42:54 CST

[root@jfht ~]# touch -t 10112200 new.txt

<=== 格式 MMDDhhmm

[root@jfht ~]#ls -l new.txt

-rw-r--r-- 1 root root 0 10-11 22:00 new.txt

[root@jfht ~]# touch -t 200910112200 new.txt

<=== 格式 yyyyMMDDhhmm

[root@jfht ~]# ls -l new.txt

-rw-r--r-- 1 root root 0 2009-10-11 new.txt

[root@jfht ~]#

实例四 将文件时间改成与别的文件相同

[root@jfht ~]#ls -l new.txt

-rw-r--r-- 1 root root 0 2009-10-11 new.txt

[root@jfht ~]#

[root@jfht ~]#

[root@jfht ~]# ls -l /etc/passwd

-rw-r--r-- 1 root root 1606 07-05 15:46 /etc/passwd

[root@jfht ~]# touch -r /etc/passwd new.txt

[root@jfht ~]#ls -l new.txt

-rw-r--r-- 1 root root 0 07-05 15:46 new.txt

[root@jfht ~]#

[root@jfht ~]# stat new.txt

File: “new.txt”

Size: 0               Blocks: 8          IO Block: 4096   一般空文件

Device: fd00h/64768d    Inode: 194805821   Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2010-10-11 22:49:17.000000000 +0800

Modify: 2010-07-05 15:46:46.000000000 +0800

Change: 2010-10-11 22:49:44.000000000 +0800

[root@jfht ~]# stat /etc/passwd

File: “/etc/passwd”

Size: 1606            Blocks: 16         IO Block: 4096   一般文件

Device: fd00h/64768d    Inode: 238127091   Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2010-10-11 22:53:01.000000000 +0800

Modify: 2010-07-05 15:46:46.000000000 +0800

Change: 2010-07-05 15:46:46.000000000 +0800

从上面看出,touch设置的时间是Modify time。

示例五 在脚本中用作运行标志

文件 touch_5.sh

#!/bin/sh

F=touch_5.run

if [ -e $F ]; then

echo "$0 is running..."

exit 1

fi

touch $F

echo "I'm doing..."

sleep 30

rm -f $F

[root@jfht ~]# cat touch_5.sh

#!/bin/sh

F=touch_5.run

if [ -e $F ]; then

echo "$0 is running..."

exit 1

fi

touch $F

echo "I'm doing..."

sleep 30

rm -f $F

[root@jfht ~]# chmod +x touch_5.sh

[root@jfht ~]# ./touch_5.sh

I'm doing...

在这个程序还没有结束的时候,在另外一个终端执行

Last login: Mon Oct 11 22:30:38 2010 from 222.70.144.138

[root@jfht ~]# ./touch_5.sh

./touch_5.sh is running...

[root@jfht ~]#

问题思考

1. 文件有哪几种时间?

2. 如果文件不存在,touch时不创建,用什么参数?

3. 如示例五中所示,用文件作为运行标志,可能会存在什么问题?

相关资料

【1】Linux宝库 Linux指令篇:档案目录管理--touch

【2】Computer Hope Linux / Unix settime and touch

返回 我使用过的Linux命令系列总目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值