2017-07-15 每天2个Linux命令 touch命令
touch命令有两个功能:一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来;二是用来创建新的空文件。
(1)用法
用法:touch [选项]... 文件...
(2)功能
1)将每个文件的访问时间和修改时间改为当前时间;
2)不存在的文件将会被创建为空文件,除非使用-c 或-h 选项;
(3)选项参数
1) -f, --force 忽略不存在的文件,从不给出提示
2) -i, --interactive 进行交互式删除
3) -r, -R, --recursive 指示rm将参数中列出的全部目录和子目录均递归地删除
4) -v, --verbose 详细显示进行的步骤
5) --help 显示此帮助信息并退出
6) --version 输出版本信息并退出
7) -a 只更改访问时间
8) -d, --date=字符串 使用指定字符串表示时间替代当前时间
9) -t STAMP 使用[[CC]YY]MMDDhhmm[.ss] 格式的时间替代当前时间
(4)实例:
1)[root@localhost /]# touch /home/sunjimeng/Document/touch_test_file
在指定目下创建文件,如果没有指定,则默认在当前文件夹下(若文件已经存在,则可以看作重新更改文件档案时间)
复制代码
[root@localhost /]# ls -l /home/sunjimeng/Document
总用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father
[root@localhost /]# touch /home/sunjimeng/Document/touch_test_file
[root@localhost /]# cd home/sunjimeng/Document
[root@localhost Document]# ll
总用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father
-rw-r--r--. 1 root root 0 5月 1 18:30 touch_test_file //第一次创建的时间
[root@localhost Document]#
[root@localhost Document]# touch touch_test_file
[root@localhost Document]# ll
总用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father
-rw-r--r--. 1 root root 0 5月 1 18:35 touch_test_file
//第二次更改文件的时间为当前时间,若文件中存有内容,则内容不变
2)[root@localhost Document]# touch -t 06061806 touch_test_file 不创建文件,只是更改文档时间(这里加不加-c操作是一样的)
[root@localhost Document]# touch -t 06061806 touch_test_file
[root@localhost Document]# ll
总用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father
-rw-r--r--. 1 root root 0 6月 6 2016 touch_test_file
3)[root@localhost Document]# touch touch_test_file1 touch_test_file2 将touch_test_file2的时间更改为与touch_test_file1的时间相同
复制代码
[root@localhost Document]# touch touch_test_file2
[root@localhost Document]# ll
总用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father
-rw-r--r--. 1 root root 0 6月 6 2016 touch_test_file //两个文件时间不相同
-rw-r--r--. 1 root root 0 5月 1 18:42 touch_test_file2
[root@localhost Document]# touch -r touch_test_file touch_test_file2
[root@localhost Document]# ll
总用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father
-rw-r--r--. 1 root root 0 6月 6 2016 touch_test_file //两个文件时间相同
-rw-r--r--. 1 root root 0 6月 6 2016 touch_test_file2
4)[root@localhost Document]# touch -d "10 days ago" touch_test_file2 将当前指定文件时间更改为相对现在的过去或未来时间
复制代码
[root@localhost Document]# touch -d "10 days ago" touch_test_file2
[root@localhost Document]# ll
总用量 0
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father
-rw-r--r--. 1 root root 0 6月 6 2016 touch_test_file
-rw-r--r--. 1 root root 0 4月 21 18:48 touch_test_file2
//不是6月6号的10天前,而是当前时间的10天前
5)[root@localhost Document]# touch --help 与touch --version
复制代码
[root@localhost Document]# touch --help
用法:touch [选项]... 文件...
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty, unless -c or -h
is supplied.
A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.
Mandatory arguments to long options are mandatory for short options too.
-a 只更改访问时间
-c, --no-create 不创建任何文件
-d, --date=字符串 使用指定字符串表示时间而非当前时间
-f (忽略)
-h, --no-dereference 会影响符号链接本身,而非符号链接所指示的目的地
(当系统支持更改符号链接的所有者时,此选项才有用)
-m 只更改修改时间
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
--help 显示此帮助信息并退出
--version 显示版本信息并退出
请注意,-d 和-t 选项可接受不同的时间/日期格式。
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告touch 的翻译错误
要获取完整文档,请运行:info coreutils 'touch invocation'
[root@localhost Document]# touch --version
touch (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。
由Paul Rubin、Arnold Robbins、Jim Kingdon、
David MacKenzie 和 Randy Smith 编写。
复制代码
(5)其他
说明:
-t time 使用指定的时间值 time 作为指定文件相应时间戳记的新值.此处的 time规定为如下形式的十进制数:
[[CC]YY]MMDDhhmm[.SS]
这里,CC为年数中的前两位,即”世纪数”;YY为年数的后两位,即某世纪中的年数.如果不给出CC的值,则touch
将把年数CCYY限定在1969--2068之内.MM为月数,DD为天将把年数CCYY限定在1969--2068之内.MM为月数,D
D为天数,hh 为小时数(几点),mm为分钟数,SS为秒数.此处秒的设定范围是0--61,这样可以处理闰秒.
这些数字组成的时间是环境变量TZ指定的时区中的一个时 间.由于系统的限制,早于1970年1月1日的时间是错误的。
2017-07-15 每天2个Linux命令 rm命令
rm命令可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉。对于链接文件,只是删除整个链接文件,而原有文件保持不变。
注意:使用rm命令要格外小心。因为一旦删除了一个文件,就无法再恢复它。所以,在删除文件之前,最好再看一下文件的内容,确定是否真要删除。rm命令可以用-i选项,这个选项在使用文件扩展名字符删除多个文件时特别有用。使用这个选项,系统会要求你逐一确定是否要删除。这时,必须输入y并按Enter键,才能删除文件。如果仅按Enter键或其他字符,文件不会被删除。
(1)用法:
用法:rm [选项]... 文件...
(2)功能:
删除 (unlink) 文件。
(3)选项参数:
1) -f, --force 强制删除。忽略不存在的文件,不提示确认
2) -i 在删除前需要确认
3) -I 在删除超过三个文件或者递归删除前要求确认。此选项比-i 提示内容更少,但同样可以阻止大多数错误发生
4) --interactive[=WHEN] 根据指定的WHEN 进行确认提示:never,once (-I), 或者always (-i)。如果此参数不加WHEN 则总是提示
5) --one-file-system 递归删除一个层级时,跳过所有不符合命令行参 数的文件系统上的文件
6) --no-preserve-roo 不特殊对待"/" --preserve-root 不允许删除"/"(默认)
7) -r, -R, --recursive 递归删除目录及其内容
8) -v, --verbose 详细显示进行的步骤 --help 显示此帮助信息并退出 --version 显示版本信息并退出
9) -d 直接把欲删除的目录的硬连接数据删除成0,删除该目录
(4)实例:
1)[root@localhost Document]# rm -i touch_test_file 等同于rm touch_test_file,交互式删除,y键确认删除,n键不删除
[root@localhost Document]# rm -i touch_test_file
rm:是否删除普通空文件 "touch_test_file"?n
[root@localhost Document]# rm -i {touch_test_file,touch_test_file2}
rm:是否删除普通空文件 "touch_test_file"?y
rm:是否删除普通空文件 "touch_test_file2"?y
2)[root@localhost Document]# rm -f test 强制删除文件不做交互提醒
复制代码
[root@localhost Document]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月 1 18:49 10 days ago
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father
-rw-r--r--. 1 root root 0 5月 1 18:58 test
[root@localhost Document]# rm -f test
[root@localhost Document]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月 1 18:49 10 days ago
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father
3)[root@localhost Document]# rm -r * 删除当前目录下除隐含文件外的所有文件和子目录
复制代码
[root@localhost Document]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月 1 18:49 10 days ago
drwxrwxr-x. 3 sunjimeng sunjimeng 17 5月 1 03:13 bin
drwxrwxr-x. 3 sunjimeng sunjimeng 18 5月 1 03:21 Father
[root@localhost Document]# rm -r *
rm:是否删除普通空文件 "10 days ago"?y
rm:是否进入目录"bin"? y
rm:是否删除目录 "bin/os_1"?y
rm:是否删除目录 "bin"?y
rm:是否进入目录"Father"? y
rm:是否删除目录 "Father/Child"?y
rm:是否删除目录 "Father"?y
[root@localhost Document]# ll
总用量 0
4)[root@localhost Document]# rm -v test1 显示删除的详细步骤
复制代码
[root@localhost Document]# touch {test1,test2}
[root@localhost Document]# rm -v test1
rm:是否删除普通空文件 "test1"?y
已删除"test1"
[root@localhost Document]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月 1 19:24 test2
[root@localhost Document]#
5)[root@localhost Document]# rm f* 与[root@localhost Document]# rm ./t* 删除以某个或某些字符结尾或开头的文件
复制代码
[root@localhost Document]# touch {file1,file2,test1,test2}
[root@localhost Document]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月 1 19:28 file1
-rw-r--r--. 1 root root 0 5月 1 19:28 file2
-rw-r--r--. 1 root root 0 5月 1 19:28 test1
-rw-r--r--. 1 root root 0 5月 1 19:28 test2
[root@localhost Document]# rm f*
rm:是否删除普通空文件 "file1"?y
rm:是否删除普通空文件 "file2"?y
[root@localhost Document]# rm ./t*
rm:是否删除普通空文件 "./test1"?y
rm:是否删除普通空文件 "./test2"?y
[root@localhost Document]# ll
总用量 0
6)[root@localhost Document]# touch – -test与[root@localhost Document]# rm – -test 删除以-字符开头的文件,另外ls – -test可以列出此-开头的文件
复制代码
[root@localhost Document]# touch -test
touch: 日期格式"est" 无效
[root@localhost Document]# touch -- -test
[root@localhost Document]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月 1 19:37 -test
[root@localhost Document]# rm -test
rm:无效选项 -- t
Try 'rm ./-test' to remove the file "-test".
Try 'rm --help' for more information.
[root@localhost Document]# rm -- -test
rm:是否删除普通空文件 "-test"?y
7)myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv “$@” $D && echo “moved to $D ok”; } 自定义回收站功能
复制代码
[root@localhost test]# myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }
[root@localhost test]# alias rm='myrm'
[root@localhost test]# touch 1.log 2.log 3.log
[root@localhost test]# ll
总计 16
-rw-r--r-- 1 root root 0 10-26 15:08 1.log
-rw-r--r-- 1 root root 0 10-26 15:08 2.log
-rw-r--r-- 1 root root 0 10-26 15:08 3.log
drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# rm [123].log
moved to /tmp/20121026150901 ok
[root@localhost test]# ll
总计 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# ls /tmp/20121026150901/
1.log 2.log 3.log
[root@localhost test]#
复制代码
说明:
上面的操作过程模拟了回收站的效果,即删除文件的时候只是把文件放到一个临时目录中,这样在需要的时候还可以恢复过来。
8)
复制代码
[root@localhost Document]# rm --help
用法:rm [选项]... 文件...
Remove (unlink) the FILE(s).
-f, --force ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-I prompt once before removing more than three files, or
when removing recursively; less intrusive than -i,
while still giving protection against most mistakes
--interactive[=WHEN] prompt according to WHEN: never, once (-I), or
always (-i); without WHEN, prompt always
--one-file-system 递归删除一个层级时,跳过所有不符合命令行参
数的文件系统上的文件
--no-preserve-root do not treat '/' specially
--preserve-root do not remove '/' (default)
-r, -R, --recursive remove directories and their contents recursively
-d, --dir remove empty directories
-v, --verbose explain what is being done
--help 显示此帮助信息并退出
--version 显示版本信息并退出
默认时,rm 不会删除目录。使用--recursive(-r 或-R)选项可删除每个给定
的目录,以及其下所有的内容。
To remove a file whose name starts with a '-', for example '-foo',
use one of these commands:
rm -- -foo
rm ./-foo
请注意,如果使用rm 来删除文件,通常仍可以将该文件恢复原状。如果想保证
该文件的内容无法还原,请考虑使用shred。
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告rm 的翻译错误
要获取完整文档,请运行:info coreutils 'rm invocation'
9)[root@localhost Document]# rm –version 版本信息
复制代码
[root@localhost Document]# rm --version
rm (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。
由Paul Rubin、David MacKenzie、Richard M. Stallman 和
Jim Meyering 编写。