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

用途说明

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

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

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

[root@jfht ~]# touch -t [1**********]0 new.txt

[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

Bash代码

#!/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

#!/bin/shF=touch_5.runif [ -e $F ]; then echo "$0 is running..." exit 1fitouch $Fecho "I'm doing..."sleep 30rm -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

用途说明

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

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

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

[root@jfht ~]# touch -t [1**********]0 new.txt

[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

Bash代码

#!/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

#!/bin/shF=touch_5.runif [ -e $F ]; then echo "$0 is running..." exit 1fitouch $Fecho "I'm doing..."sleep 30rm -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真题详解
  • ● 在Linux操作系统中, (31) 文件负责配置DNS,它包含了主机的域名搜索顺序和DNS服务器的地址. (31)A./etc/hostname B./etc/host.conf C./etc/resolv.conf D./etc/name.conf 试题解析: 常识. 答案:C ● Linux ...

  • linux文件名命名规则
  • linux文件名命名规则 Linux 系统区分英文字符的大小写.命名目录和命名文件的规则是相同的.除非有特别的原 因,否则用户创建的文件和目录名要使用小写字符.大多数的 Linux 命令也使用小写字符. Linux 系统下的文件名长度最多可到256个字符.通常情况下,文件名的字符包括:字母.数字.& ...

  • linux的基本操作
  • linux的基本操作 1.用户的登录.注销与关机 摁下电源开关,屏幕上刷刷闪过一串串启动内容的文字提示,是不是感觉有点兴奋?如果这是你的linux第一次启动,那么在服务启动的一串绿色的"[OK]",突然出现一个红色的false,仔细一看好象是一个叫httpd的服务.不要害怕,这个 ...

  • Linux.课后题答案
  • 第二章 常用命令 选择题 1. 若要设置/usr/myprog文件的拥有者有读.写和可执行权限,用户组和其他用户均没有对该文件的操作权限,以下操作命令中,正确的是( A ). A. chmod 700 /usr/myprog B. Chown 700 /usr/myprog C. Chmod u=r ...

  • 服务器安装
  • 1.配置与管理Samba服务器(掌握Samba服务器的安装.配置与调试.) 1.1 了解Samba应用环境 ●文件和打印机共享:文件和打印机共享是Samba的主要功能,SMB进程实现 资源共享,将文件和打印机发布到网络之中,以供用户可以访问. ●身份验证和权限设置:smbd服务支持user mode ...

  • linux运维工程师面试题
  • Linux运维工程师面试题 一.有文件file1 1.请用shell查询file1 里面空行的所在行号 awk ',if($0~/^$/)print NR-' file or grep -n ^$ file |awk 'BEGIN,FS=":"-,print $1-' 2.编写S ...

  • 西北大学linux选修课考试答案
  • 1. 安装Linux至少需要哪两个分区?还有哪些常用分区? 至少有/和swap分区.其它常用分区有:/bin /home /usr /var /usr/local /tmp等 2. 安全专家建议,安装Linux时,最好为主要的目录建立单独的分区,试分析这样做 的优点. 由于每个分区指定了自己的大小, ...

  • 在Ubuntu系统上使用Samba4来创建活动目录架构(一)
  • Samba 是一个自由的开源软件套件,用于实现 Windows 操作系统与 Linux/Unix 系统之间的无缝连接及共享资源. Samba 不仅可以通过 SMB/CIFS 协议组件来为 Windows 与 Linux 系统之间提供独立的文件及打印机共享服务,它还能实现活动目录(Active Dir ...

  • Linux的审计功能(Audit)
  • Linux 的审计功能(audit ) Linux 内核有用日志记录事件的能力,比如记录系统调用和文件访问.然后,管理员可以评审这些日志,确定可能存在的安全裂口,比如失败的登录尝试,或者用户对系统文件不成功的访问.这种功能称为Linux 审计系统,在Red Hat Enterprise Linux ...