1:查看其切换目录
1.1:ls
功能描述:列表(List)显示目录内容
语法:ls [选项] [文件 / 目录]
选项:
- -a 显示所有文件,包括隐藏文件与目录
- -l 长格式详细信息显示
- -d 查看目录属性,而非目录下的资料信息
- -h 人性化显示容量信息
- -c 显示文件或目录属性最后修改的时间
- -t 以修改时间排序,默认按照文件名称排序
- -u 显示文件或目录最后被访问的时间
1 [root@localhost ~]# ls #显示当前目录下的子文件与目录名称
2 [root@localhost ~]# ls /etc #显示/etc 目录下的子文件与目录名称
3 [root@localhost ~]# ls -a #查看以.开始的隐藏文件与目录
4 [root@localhost ~]# ls -l #查看文件与目录的详细信息
5 [root@localhost ~]# ls -ld /root #查看当前root目录自身的详细信息
6 [root@localhost ~]# ls -lh #人性化显示容量信息
7 [root@localhost ~]# ls -lc /etc/passwd #查看/etc/passwd文件的属性变更时间
8 [root@localhost ~]# ls -lu /etc/passwd #查看/etc/passwd最后访问时间
9 [root@localhost ~]# ls -lt #查看档案信息并以修改时间排序
1.2:cd
功能描述:切换目录,其中。和 .. 可以理解为相对路径,例如 cd ./test 表示以当前目录为参考,表示相对于当前,而 cd /home/test 表示完整的路径,理解为绝对路径
语法:cd [目录]
[root@localhost ~]# cd /usr/local/ #切换工作目录到/usr/local/
[root@localhost local]# cd .. #切换工作目录到当前目录的上一级目录
[root@localhost usr]# cd - #返回到/usr/local目录
[root@localhost local]# cd #切换工作目录到当前用户的家目录(或使用命令 cd ~)
[root@localhost ~]#
1.3:pwd
功能描述:显示当前所在的工作目录
语法:pwd [选项]
选项:
- -P 显示链接的真实路径
[root@localhost bin]# pwd #表示返回当前工作目录 /bin /bin [root@localhost bin]# pwd -P #表示返回链接的真实路径 /usr/bin /usr/bin -
1.4:du
功能描述:统计目录及文件的空间占用情况
语法:du [选项] [文件或目录]
选项:
- -a:统计时包括所有的文件,而不仅仅只统计目录
- -h:以更易读的字节单位(K、M 等)显示信息
- -s:只统计每个参数所占用空间总的大小[root@localhost ~]# du -sh / #查看根目录大小
[root@localhost ~]# du -sh / #查看根目录大小
2:创建目录和文件
文件命名规则:
(1)不能使用 / 来当文件名,/ 是用来做根的,也是用来做路径分隔符的
(2)文件名不能超过 255 个字符
(3)区分大小写 file File FILE flle
(4)目录也是文件,在同一路径下,两个文件不能同名
2.1:touch
功能描述:新建空文件,或更新文件时间标记
语法:touch [文件名]
[root@localhost ~]# touch hellotest.txt
#创建一个名字为hellotest.txt 文件
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1213 9月 3 15:43 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 9月 11 10:13 hellotest.txt
#10:13
[root@localhost ~]# touch hellotest.txt
#修改名字为hellotest.txt 文件的时间
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1213 9月 3 15:43 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 9月 11 10:14 hellotest.txt
#10:14
2.2:mkdir
功能描述:创建新的目录(Make Directory)
语法:mkdir [选项] [/ 路径 /] 目录名
选项:
- -p 创建多级目录
[root@localhost ~]# mkdir 123
#创建了一个目录123
[root@localhost ~]# ls
123 anaconda-ks.cfg hellotest.txt
[root@localhost ~]# mkdir -p aaa/bbb/ccc/ddd
#创建了层级目录aaa/bbb/ccc/ddd
[root@localhost ~]# tree ./ #这个命令后面在学,用来显示目录层级信息
./
├── 123
├── aaa
│ └── bbb
│ └── ccc
│ └── ddd
├── anaconda-ks.cfg
└── hellotest.txt
5 directories, 2 files
[root@localhost ~]#
2.3:ln
Linux 中的链接文件不同于 windows 的快捷方式,linux 的链接文件份为软链接和硬链接。软链接是可以跨分区,但源文件不可删除,硬链接不可以跨分区,但可以将源文件删除
软链接:
格式:ln -s 原文件 链接文件 (目标文件)
- 软链接,以路径的形式存在。类似于 Windows 操作系统中的快捷方式
- 软链接可以跨文件系统 ,硬链接不可以
- 软链接可以对一个不存在的文件名进行链接
- 软链接可以对目录进行链接
硬链接:
格式:ln 原文件 链接文件
- 硬链接,以文件副本的形式存在。但不占用实际空间。
- 不允许给目录创建硬链接
- 硬链接只有在同一个文件系统中才能创建
- 可以以不同的文件名存在同一个目录中,或者以相同的文件名存在于不同的目录下
- 建立硬链接时,在另外的目录或本目录中增加目标文件的一个目录项,这样,一个文件就登记在多个目录中。
#软链接
[root@localhost ~]# touch hello.txt
[root@localhost ~]# ln -s /root/hello.txt /tmp/hi.txt #创建文件的软链接
[root@localhost ~]# ll /tmp/hi.txt
lrwxrwxrwx. 1 root root 15 9月 11 11:33 /tmp/hi.txt -> /root/hello.txt
[root@localhost ~]# mkdir test
[root@localhost ~]# ln -s /root/test/ /var/test #创建目录的软链接
[root@localhost ~]# ll /var/test
lrwxrwxrwx. 1 root root 11 9月 11 11:33 /var/test -> /root/test/
#硬链接
[root@localhost ~]# ln /root/hello.txt /root/hello-1.txt #创建硬链接
[root@localhost ~]# rm -rf hello.txt
#源文件删除后,链接文件仍可以正常使用
[root@localhost ~]# ll /root/hello-1.txt
-rw-r--r--. 1 root root 0 9 月 11 11:32 /root/hello-1.txt
3:复制、删除、移动目录和文件
3.1:cp
功能描述:复制(Copy)文件或目录
语法:cp [选项]... 源文件或目录... 目标文件或目录
选项:
- -r:递归复制整个目录树
- -p:保持源文件的属性不变
- -f:强制覆盖目标同名文件或目录
- -i:需要覆盖文件或目录时进行提醒
[root@localhost ~]# cp /etc/passwd /tmp/
#复制文件/etc/passwd 到 /tmp目录下
[root@localhost ~]# cp /etc/passwd /tmp/password
#复制文件 /etc/passwd 到 /tmp目录下并改名为password
[root@localhost ~]# cp -r /var/log/ /tmp/
#复制目录/var/log 到/tmp/目录下
3.2:mv
功能描述:移动(Move)文件或目录 —— 若如果目标位置与源位置相同,则相当于改名
语法:mv [选项]... 源文件或目录... 目标文件或目录
[root@localhost ~]# mv hellotest.txt hellotest.doc
#将hellotest.txt改名为hellotest.doc
[root@localhost ~]# mv hellotest.doc 123/
#将hellotest.doc移动到123目录下
3.3:rm
功能描述:删除(Remove)文件或目录
语法:rm [选项] [文件或目录]
选项:
- -r 删除目录以及目录下的所有内容
- -i 删除前,提示是否删除
- -f 不提示,强制删除
[root@localhost ~]# touch test.txt #创建了一个名为test.txt的文件
[root@localhost ~]# rm test.txt #删除文件test.txt
rm:是否删除普通空文件 'test.txt'? y #输入y确认删除
[root@localhost ~]# rm -rf 123/ #删除目录且不提示
4:查找目录和文件
4.1:which
功能描述:查找 Linux 命令文件并显示所在的位置 —— 搜索范围由 PATH 环境变量指定
语法:which 命令或程序名
[root@localhost ~]# which mkdir
/bin/mkdir
[root@localhost ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# which cd #cd是Shell内部命令,因此查不到
/usr/bin/which: no cd in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
4.2:find
功能描述:用于查找文件或目录
语法:find [查找范围] [查找条件]
常用查找条件
- -name:按文件名称查找
- -size:按文件大小查找
- -user:按文件属主查找
- -type:按文件类型查找
[root@localhost ~]# find /etc -name "resol*.conf"
/etc/resolv.conf
/etc/sysconfig/networking/profiles/default/resolv.conf
5:查看文件内容
5.1:cat
功能描述:显示文件内容 语法:cat [选项] [文件] 选项:
- -b 显示行号,空白行不显示行号。
- -n 显示行号,包括空白行
[root@localhost ~]# cat /etc/services
[root@localhost ~]# cat -n /etc/services
[root@localhost ~]# cat -b /etc/services
5.2:more
功能描述:全屏方式分页显示文件内容
语法:more [选项] 文件名... 操作按键:
(空格)或 f 显示下一页
(Enter) 显示下一行
q 或 Q 退出
[root@localhost ~]# more /var/log/messages
5.3:head
功能描述:查看文件的前几行,默认显示前 10 行内容。
语法:head [选项] [文件名]
选项:
-c nK 显示文件前 nKB 的内容
-n 显示文件的前 n 行的内容
[root@localhost ~]# head -c 2k /var/log/messages #查看文件的前2KB的内容
[root@localhost ~]# head -20 /var/log/messages #查看文件的前20行的内容
5.4:tail
功能描述:查看文件的尾部内容,默认显示末尾 10 行
语法:tail [选项] [文件]
选项:
-n 显示文件的后 n 行
-f 动态显示文件内容
-c nK 显示文件末尾 nKB 的内容
[root@localhost ~]# tail -c 2K /var/log/messages #查看文件末尾2KB的内容
[root@localhost ~]# tail -20 /var/log/messages #查看文件末尾20行内容
[root@localhost ~]# tail -f /var/log/messages #实时动态查看文件内容
5.5:less
功能描述:查看分页文件内容,空格(下一页)、方向键(上下回翻)、q 键(退出查看)
[root@localhost ~]# less /var/log/messages
6:统计文件内容
6.1:wc
功能描述:统计文件中的单词数量(Word Count)等信息
语法:wc [选项]... 目标文件...
选项:
-l 统计行数
-w 统计单词个数
-c 统计字节数
[root@localhost ~]# wc /etc/passwd #依次显示文件的行数、单词数、字节数
23 49 1147 /etc/passwd
[root@localhost ~]# wc -c /etc/passwd #显示文件的字节信息
1147 /etc/passwd
[root@localhost ~]# wc -l /etc/passwd #显示文件的行数
23 /etc/passwd
[root@localhost ~]# wc -w /etc/passwd #显示文件的单词个数
49 /etc/passwd
7:检索和过滤文件内容
7.1:grep
功能描述:在文件中查找并显示包含指定字符串的行
语法:grep [选项]... 查找条件 目标文件
常用命令选项:
-i 查找时忽略大小写
-v 反转查找,输出与查找条件不相符的行
查找条件设置
要查找的字符串以双引号括起来
“^……” 表示以…… 开头
“……$”表示以……结尾
“^$” 表示空行
[root@localhost ~]# grep root /etc/passwd #在/etc/passwd文件中过滤出包含root的行
[root@localhost ~]# grep --color root /etc/passwd #对匹配的关键字显示颜色
[root@localhost ~]# grep -i ROOT /etc/passwd #不区分大小写
[root@localhost ~]# grep -v root /etc/passwd #反向过滤
#过滤出hosts文件中的非注释行(不以“#”号开头的行)
[root@localhost ~]# grep -v "^#" /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost6
#查找系统启动时识别的USB总线信息
[root@localhost ~]# dmesg | grep "USB"
drivers/usb/input/hid-core.c: v2.6:USB HID core driver
USB Universal Host Controller Interface driver v3.0
uhci_hcd 0000:02:00.0: new USB bus registered, assigned bus number 1
hub 1-0:1.0: USB hub found
ehci_hcd 0000:02:03.0: new USB bus registered, assigned bus number 2
ehci_hcd 0000:02:03.0: USB 2.0 started, EHCI 1.00, driver 10 Dec 2004
hub 2-0:1.0: USB hub found
8:归档和压缩命令
8.1:gzip 命令、bzip2 命令
用途:制作压缩文件、解开压缩文件
命令格式:
gzip [-9] 文件名…
bzip2 [-9] 文件名…
gzip -d .gz 格式的压缩文件
bzip2 -d *.bz2 格式的压缩文件
常用命令选项
-9 表示高压缩比,多在创建压缩包时用
-d 用于解开已经压缩过的文件
8.2:tar 命令
用途:制作归档文件、释放归档文件
格式:tar [选项]... 归档文件名 源文件或目录
tar [选项]... 归档文件名 [-C 目标目录]
常用命令选项
-c 创建.tar 格式的包文件 -x 解开.tar 格式的包文件
-v 输出详细信息
-f 表示使用归档文件
-p 打包时保留原始文件及目录的权限
-t 列表查看包内的文件
-C 解包时指定释放的目标文件夹
-z 调用 gzip 程序进行压缩或解压
-j 调用 bzip2 程序进行压缩或解压
[root@localhost ~]# tar -cjvf test.tar.bz2 /etc/httpd
#从成员名中删除开头的“/”
[root@localhost ~]# ls -lh test.tar.bz2
[root@localhost ~]# tar -xjf test.tar.bz2 -C /tmp
[root@localhost ~]# ls -ld /tmp/etc/httpd/
drwxr-xr-x 4 root root 4096 09-08 16:37 /tmp/etc/httpd/
[root@localhost ~]# rm -rf /tmp/etc/
9:帮助命令
9.1:man
命令名称:man 语法:man [命令或配置文件] 功能描述:获得帮助信息
范例:
$ man ls
……
NAME
ls -- list directory contents
SYNOPSIS
ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1%] [file ...]
DESCRIPTION
For each operand that names a file of a type other than directory, ls
displays its name as well as any requested, associated information. For
each operand that names a file of type directory, ls displays the names
of files contained within that directory, as well as any requested, asso-
ciated information.
If no operands are given, the contents of the current directory are dis-
played. If more than one operand is given, non-directory operands are
displayed first; directory and non-directory operands are sorted sepa-
rately and in lexicographical order.
……
$ man services
查看配置文件services的帮助信息
9.2:info
指令名称:info
语法:info [任何命令]
功能描述:获得帮助信息
$ info ls
#查看ls指令的帮助信息
9.3:whatis
语法:whatis [任何命令]
功能描述:获取索引的间断说明信息
$ whatis ls
$ whatis ls tab
#makewhatis 相当于man -k
#建立whatis和apropos搜索使用的数据库,当使用这两个命令发生错误时,就是whatis database没有建立
350



被折叠的 条评论
为什么被折叠?



