本文給大家介紹了一些touch命令的用法。touch一般用來創建一個新文件。同時,touch還可以用來修改文件時間戳。
創建新文件:
$ ls -l /tmp/foo ls: /tmp/foo: No such file or directory $ touch /tmp/foo $ ls -l /tmp/foo -rw-r--r-- 1 andy wheel 0 Jul 10 11:56 /tmp/foo
touch還可以用來修改文件時間戳。
-t 參數可以指定要修改成的具體時間
$ ls -l /tmp/foo ls: /tmp/foo: No such file or directory $ touch -t 201107010930 /tmp/foo $ ls -l /tmp/foo -rw-r--r-- 1 andy wheel 0 Jul 1 09:30 /tmp/foo
若沒有指定,則設置為當前時間:
$ ls -l /tmp/foo -rw-r--r-- 1 andy wheel 0 Jul 1 09:30 /tmp/foo $ touch /tmp/foo $ ls -l /tmp/foo -rw-r--r-- 1 andy wheel 0 Jul 10 11:58 /tmp/foo
You can also use the timestamp from another file and apply it to another file by using the -r switch.
$ ls -l /tmp/someotherfile -rw-r--r-- 1 andy wheel 0 Aug 12 2005 /tmp/someotherfile $ touch -r /tmp/someotherfile /tmp/foo /tmp/foo2 /tmp/foo3 $ ls -al /tmp/ -rw-r--r-- 1 andy wheel 0 Aug 12 2005 /tmp/foo -rw-r--r-- 1 andy wheel 0 Aug 12 2005 /tmp/foo2 -rw-r--r-- 1 andy wheel 0 Aug 12 2005 /tmp/foo3 -rw-r--r-- 1 andy wheel 0 Aug 12 2005 /tmp/someotherfile