本节是shell脚本编程入门教程的第二节,介绍linux shell中文件操作的常用命令,包括rm命令、mkdir命令、stat命令、cat命令等的用法,需要的朋友参考下。
本节内容:
linux shell文件操作命令
1)rm删除命令,删除文件或目录 rm -rf source ,如果想删除某个目录下所有文件,使用命令rm -rf * ,这是一个非常谨慎的操作,删除后就无法恢复了,尽量不要使用root用户操作这个命令。
[root@h1 ~]# man rm
RM(1) User Commands RM(1)
NAME
rm - remove files or directories
SYNOPSIS
rm [OPTION]... FILE...
DESCRIPTION
This manual page documents the GNU version of rm. rm removes each specified file. By default, it does
not remove directories.
If the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or
--recursive are given, then rm prompts the user for whether to proceed with the entire operation. If the
response is not affirmative, the entire command is aborted.
Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not
given, or the -i or --interactive=always option is given, rm prompts the user for whether to remove the
file. If the response is not affirmative, the file is skipped.
OPTIONS
Remove (unlink) the FILE(s).
-f, --force
ignore nonexistent files, 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
when removing a hierarchy recursively, skip any directory that is on a file system different from
that of the corresponding command line argument
--no-preserve-root
:
2)mkdir命令,创建一个目录,用法 mkdir 目录名
3)查看文件统计信息,stat命令 用法: stat 文件名或目录名,例子如下:
复制代码 代码示例:
[root@h1 ~]# stat count.txt
File: "count.txt"
Size: 52 Blocks: 8 IO Block: 4096 普通文件
Device: fd00h/64768d Inode: 674329 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-07-31 19:47:19.826696827 +0800
Modify: 2014-07-31 19:46:57.271696962 +0800
Change: 2014-07-31 19:47:11.123698114 +0800
[root@h1 ~]# stat login/
File: "login/"
Size: 4096 Blocks: 8 IO Block: 4096 目录
Device: fd00h/64768d Inode: 663800 Links: 3
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-07-31 19:42:37.128697676 +0800
Modify: 2014-07-09 04:08:01.337714013 +0800
Change: 2014-07-09 04:08:01.337714013 +0800
[root@h1 ~]#
扩展阅读:
4)file命令,查看文件类型,总共分3类文件,文本文件,可执行文件,数据文件,例子如下:
复制代码 代码示例:
[root@h1 ~]# file hh.txt
hh.txt: very short file (no magic)
[root@h1 ~]# file login/
login/: directory
[root@h1 ~]# file setlimit.sh
setlimit.sh: ASCII text
[root@h1 ~]#
5)查看文件命令,cat 用法,cat 文件名
复制代码 代码示例:
[root@h1 ~]# cat count.txt
中国#23
美国#90
中国#100
中国#10
法国#20
[root@h1 ~]#
cat -n 文件名,给输出加上行号
[root@h1 ~]# cat -n count.txt
1 中国#23
2 美国#90
3 中国#100
4 中国#10
5 法国#20
6
[root@h1 ~]#
上面的例子,最后一行也加了,这种空白行,不应该算行,我么应该使用
cat -b 命令:
复制代码 代码示例:
[root@h1 ~]# cat -b count.txt
1 中国#23
2 美国#90
3 中国#100
4 中国#10
5 法国#20
6) more命令,适合查看大型文本,cat命令适合查看较小的文件,例如我们查看hadoop的日志文件,就不适合用cat命令:
[search@h1 logs]$ more hadoop-search-namenode-h1.log
2014-07-31 22:07:30,494 INFO org.
apache.hadoop.hdfs.server.namenode.NameNode: STARTUP_MSG:
/************************************************************
STARTUP_MSG: Starting NameNode
STARTUP_MSG: host = h1/192.168.46.32
STARTUP_MSG: args = []
STARTUP_MSG: version = 2.2.0
STARTUP_MSG: classpath = /home/search/hadoop/etc/hadoop/:/home/search/hadoop/share/hadoop/common/lib/guava-11.0.2.jar:/home/search/hadoop/share/hadoop/common/lib/netty-3.6.2.
Final.jar:/home/search/hadoop/share/hadoop/common/lib/protobuf-java-2.5.0.jar:/home/search/hadoop/share/hadoop/common/lib/commons-compress-1.4.1.jar:/home/search/hadoop/share/h
adoop/common/lib/commons-httpclient-3.1.jar:/home/search/hadoop/share/hadoop/common/lib/commons-beanutils-1.7.0.jar:/home/search/hadoop/share/hadoop/common/lib/jaxb-api-2.2.2.j
ar:/home/search/hadoop/share/hadoop/common/lib/mockito-all-1.8.5.jar:/home/search/hadoop/share/hadoop/common/lib/jsch-0.1.42.jar:/home/search/hadoop/share/hadoop/common/lib/com
mons-net-3.1.jar:/home/search/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar:/home/search/hadoop/share/hadoop/common/lib/jets3t-0.6.1.jar:/home/search/hadoop/share/hado
op/common/lib/xmlenc-0.52.jar:/home/search/hadoop/share/hadoop/common/lib/jackson-jaxrs-1.8.8.jar:/home/search/hadoop/share/hadoop/common/lib/jasper-runtime-5.5.23.jar:/home/se
arch/hadoop/share/hadoop/common/lib/jersey-json-1.9.jar:/home/search/hadoop/share/hadoop/common/lib/commons-io-2.1.jar:/home/search/hadoop/share/hadoop/common/lib/paranamer-2.3
.jar:/home/search/hadoop/share/hadoop/common/lib/jsp-api-2.1.jar:/home/search/hadoop/share/hadoop/common/lib/commons-beanutils-core-1.8.0.jar:/home/search/hadoop/share/hadoop/c
ommon/lib/hadoop-annotations-2.2.0.jar:/home/search/hadoop/share/hadoop/common/lib/xz-1.0.jar:/home/search/hadoop/share/hadoop/common/lib/jersey-core-1.9.jar:/home/search/hadoo
注意最后有百分比,可以按enter键,一点点向下滚动,也可以按z键,翻一页,,最后使用q命令退出。