文章目录
linux基础命令
ls命令
ls [-a -l -h] [路径]:查看指定路径下文件列表 注意:默认都是当前路径
参数 -a:展示出隐藏目录
参数 -l:以详细信息展示
参数 -h:一般配合-l使用,个性化展示文件大小
# 列出当前目录内容[平铺模式]
[root@node1 test]# ls
a a.txt b.txt index.html t.tar
# 列出当前目录内容[列表模式]
[root@node1 test]# ls -l
drwxr-xr-x 3 root root 56 7月 13 16:30 a
lrwxrwxrwx 1 root root 13 7月 14 15:42 a.txt -> /test/a/a.txt
-rw-r--r-- 1 root root 0 7月 13 16:23 b.txt
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
# 列出当前目录内容[列表模式-包含隐藏文件]
[root@node1 test]# ls -a -l
drwxr-xr-x 3 root root 72 7月 14 15:42 .
dr-xr-xr-x. 20 root root 4096 7月 14 19:56 ..
drwxr-xr-x 3 root root 56 7月 13 16:30 a
lrwxrwxrwx 1 root root 13 7月 14 15:42 a.txt -> /test/a/a.txt
-rw-r--r-- 1 root root 0 7月 13 16:23 b.txt
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
# 列出指定目录内容
[root@node1 test]# ls -l /test
drwxr-xr-x 3 root root 56 7月 13 16:30 a
lrwxrwxrwx 1 root root 13 7月 14 15:42 a.txt -> /test/a/a.txt
-rw-r--r-- 1 root root 0 7月 13 16:23 b.txt
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
clear和cd和pwd命令
clear: 清屏
==cd [路径] :==切换到指定路径下
cd #回到用户主目录
cd . #去当前目录
cd … #去上一级目录
cd - #去上一次所在的目录
pwd : 查看当前路径
相对路径和绝对路径
-
相对路径和绝对路径
绝对路径: 以根目录做起点,描述路径的方式,路径以/开头
相对路径: 以当前目录做起点,描述路径的方式,路径不需以/开头
如无特殊需求,后续学习中,将经常使用相对路径表示 -
特殊路径符有哪些?
.
表示当前目录,比如:cd .
或cd ./Desktop
..
表示上一级目录,比如:cd ..
或cd ../..
[root@node1 zz20]# cd / #进入根目录
[root@node1 /]# cd #回到用户目录
[root@node1 ~]# cd - #回到上一次所在的目录
/
[root@node1 /]# cd opt
[root@node1 opt]# cd .. #回到上一级目录
[root@node1 /]# cd /usr/local
[root@node1 local]# pwd #列出当前所在的绝对路径./usr/local
mkdir命令
mkdir [-p] 目录路径
: 创建指定目录
-p:可选,表示自动创建不存在的父目录,适用于创建连续多层级的目录
[root@node1 test]# ls
index.html t.tar
[root@node1 test]# mkdir ./bbb # 当前目录下创建文件夹./可以省略
[root@node1 test]# ls
bbb index.html t.tar
[root@node1 test]# mkdir /test/ccc #使用绝对路径创建目录
[root@node1 test]# ls
bbb ccc index.html t.tar
[root@node1 test]# mkdir test1 test2 # 创建多个目录
[root@node1 test]# ls
bbb ccc index.html test1 test2 t.tar
touch命令
touch 文件路径
: 创建指定文件
注意: 可以同时创建多个文件
# 创建一个文件
[root@node1 test]# touch a.txt
[root@node1 test]# ll
-rw-r--r-- 1 root root 0 7月 15 11:31 a.txt
#创建多个文件
[root@node1 test]# touch 1.txt 2.txt
[root@node1 test]# ll
-rw-r--r-- 1 root root 0 7月 15 11:31 1.txt
-rw-r--r-- 1 root root 0 7月 15 11:31 2.txt
-rw-r--r-- 1 root root 0 7月 15 11:31 a.txt
echo命令
echo 要打印的内容
注意: 默认打印到控制台,但是经常配合重定向符号使用
[root@node1 test]# echo helloworld
helloworld
重定向符号
>
: 覆盖写入
>>
: 追加写入
注意: 一般都是配合echo使用
[root@node1 test]# echo hello >1.txt
[root@node1 test]# echo world >>1.txt
[root@node1 test]# cat 1.txt
hello
world
# >左边产生的结果 会定向到 > 右边的文件中. >覆盖写入,>>追加写入
[root@node1 test]# ls -l /test >1.txt
[root@node1 test]# cat 1.txt
-rw-r--r-- 1 root root 0 7月 15 11:48 1.txt
-rw-r--r-- 1 root root 0 7月 15 11:31 2.txt
-rw-r--r-- 1 root root 0 7月 15 11:31 a.txt
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
查看文件内容命令
cat [-n] 文件
: cat查看文件内容,一般建议查看小文件
-n :显示行号
more 文件
: more查看文件内容,一般建议查看大文件(可以翻页)
空格: 下一页
b : 上一页
q : 退出
less [-N] 文件
: less查看文件内容,一般建议查看大文件(可以翻页,可以搜索)
-N : 显示行号
空格: 下一页
b : 上一页
q : 退出
/内容: 搜索指定内容
cat a.txt
cat -n a.txt # 显示行号 -- 查看小文件
more b.txt # 翻页查看大文件
less b.txt # 带有搜索文件内容功能
less -N b.txt # 显示行号 -- 查看大文件
cp命令
cp [-r] 要复制的文件或者文件夹 目标位置
: 复制文件或者文件夹到指定目标位置
注意: -r 主要用于复制文件夹
# 当前目录下复制
[root@node1 test]# cp 2.txt 3.txt
[root@node1 test]# ll
-rw-r--r-- 1 root root 267 7月 15 11:48 1.txt
-rw-r--r-- 1 root root 0 7月 15 11:31 2.txt
-rw-r--r-- 1 root root 0 7月 15 11:51 3.txt
# 指定目录下复制--[未改名]
[root@node1 test]# cp 1.txt /
[root@node1 test]# ls /
1.txt bin data.cnf dirfile export lib media opt root sbin sys tmp var
a.txt boot dev etc home lib64 mnt proc run srv test usr
# 指定目录下复制--[改名]
[root@node1 test]# cp 2.txt /b.txt
[root@node1 test]# ls /
1.txt bin b.txt dev etc home lib64 mnt proc run srv test usr
a.txt boot data.cnf dirfile export lib media opt root sbin sys tmp var
#复制目录失败---[目录a有子文件]
[root@node1 test]# cp a /
cp: 略过目录"a"
[root@node1 test]# ls /
1.txt bin b.txt dev etc home lib64 mnt proc run srv test usr
a.txt boot data.cnf dirfile export lib media opt root sbin sys tmp var
# 复制目录成功 -- 注意加上-r参数;
[root@node1 test]# cp -r a /
[root@node1 test]# ls /
1.txt a.txt boot data.cnf dirfile export lib media opt root sbin sys tmp var
a bin b.txt dev etc home lib64 mnt proc run srv test usr
mv命令
mv 要移动的文件或者文件夹 目标位置
: 移动文件或者文件夹到指定目标位置
注意: 目标位置是文件,文件不存在就会直接改名 目标位置如果是目录,一定要存在,否则当成了普通文件改名
# 移动文件到指定目录
[root@node1 test]# mv 2.txt a
[root@node1 test]# ls
3.txt a a.txt index.html t.tar
[root@node1 test]# ls a
1.txt 2.txt
# 移动目录到指定目录
[root@node1 test]# mv a b
[root@node1 test]# ls b
a
# 移动目的地是文件路径表示改名.
[root@node1 test]# mv 3.txt 333.txt
[root@node1 test]# ll
-rw-r--r-- 1 root root 0 7月 15 11:51 333.txt
-rw-r--r-- 1 root root 0 7月 15 11:31 a.txt
drwxr-xr-x 3 root root 15 7月 15 12:05 b
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
rm命令
rm [-r -f] 要删除的文件或者文件夹
: 删除指定的文件或者文件夹
注意:-r
主要用于删除文件夹 -f
:主要用于强制删除文件或者文件夹
# rm删除文件 -- [root用户有提示][手动输入y,然后敲回车键]
[root@node1 test]# rm 333.txt
rm:是否删除普通空文件 "333.txt"?y
[root@node1 test]# ll
-rw-r--r-- 1 root root 0 7月 15 11:31 a.txt
drwxr-xr-x 3 root root 15 7月 15 12:05 b
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
# 一次删除多个文件,删除文件不提示.[-f]
[root@node1 test]# ll
-rw-r--r-- 1 root root 0 7月 15 14:33 a.txt
drwxr-xr-x 3 root root 15 7月 15 12:05 b
-rw-r--r-- 1 root root 0 7月 15 14:33 b.txt
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
[root@node1 test]# rm -f a.txt b.txt
[root@node1 test]# ll
drwxr-xr-x 3 root root 15 7月 15 12:05 b
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
# rm 无法直接删除目录:[需要添加参数 -r]
[root@node1 test]# rm b
rm: 无法删除"b": 是一个目录
# rm 删除目录成功[-r]
[root@node1 test]# rm -rf b
[root@node1 test]# ll
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
# rm结合通配符 * 来删除多个文件 : * 表示任意多个字符.
[root@node1 test]# ll
-rw-r--r-- 1 root root 0 7月 15 14:36 1.txt
-rw-r--r-- 1 root root 0 7月 15 14:36 2.txt
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
[root@node1 test]# rm -rf *.txt
[root@node1 test]# ll
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
通配符
*
: 任意个字符 类似于sql模糊查询中的%
?
: 任意1个字符 类似于sql模糊查询中的_
[root@node1 test]# rm -rf *.txt # 删除后缀名是.txt的文件
[root@node1 test]# ll
-rw-r--r-- 1 root root 0 7月 15 14:40 1.txt
-rw-r--r-- 1 root root 0 7月 15 14:40 aa.txt
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
[root@node1 test]# rm -rf ?.txt # 删除名字是一个字的.txt文件.
[root@node1 test]# ll
-rw-r--r-- 1 root root 0 7月 15 14:40 aa.txt
-rw-r--r-- 1 root root 10206 8月 14 2023 index.html
-rw-r--r-- 1 root root 119 7月 13 16:30 t.tar
which和find
which 命令
: 查找命令所在位置
主要: find命令也可以结合通配符使用
find [查找路径] -name 文件名
: 根据文件名查找
find [查找路径] -size [+ -]大小
: 根据文件大小查找 +: 大于 -: 小于
find [查找路径] -type [d f] -name 文件名
: 根据文件类型查找 d: 文件夹 f: 文件
# 查询cd命令在哪里安装?
[root@node1 test]# which cd
/usr/bin/cd
#查询.txt结尾的文件
[root@node1 test]# find /test/ -name '*.txt'
/test/aa.txt
#查询文件名为一个字的.txt文件
[root@node1 test]# find /test/ -name '?.txt'
/test/1.txt
#查询内存大于1k的文件!
[root@node1 test]# find /test/ -size +1k
/test/index.html
/test/1.txt
管道符 |
管道使用格式: 命令1 | 命令2 | 命令3 ...
解释: 把命令1的结果传递给命令2,然后再把命令2的结果传递给命令3,依次类推…
注意: 管道一般配合grep命令使用较多
# 在进程信息中搜索 包含 ssh的进程信息.
[root@node1 test]# ps -ef | grep 'ssh'
root 1126 1 0 11:01 ? 00:00:00 /usr/sbin/sshd -D
root 2000 1126 0 11:08 ? 00:00:00 sshd: root@pts/0
root 4078 2002 0 14:50 pts/0 00:00:00 grep --color=auto ssh
# 在aa.txt中 搜索 包含he的行信息.
[root@node1 test]# cat aa.txt | grep 'he'
helloworld
grep命令
grep [-n] 关键字 文件路径
: 在指定文件中搜索指定关键字
注意: -n
主要用于显示搜索到结果的行号,管道|
一般配合grep命令使用较多
[root@node1 test]# grep 'w' aa.txt #不加行号查询内容
helloworld
[root@node1 test]# grep -n 'w' aa.txt #加行号查询内容
1:helloworld
wc命令
wc [-l -w] 文件路径
: 统计指定文件内容的行数或者单词数
-l
: 行数
-w
: 单词数 注意:空格分隔
[root@node1 test]# cat aa.txt
helloworld
[root@node1 zz20]# wc a.txt
2 2 12 a.txt # 2行 2个单词,12个字符
------------------------------------
[root@node1 test]# wc -c aa.txt #查询字节
11 aa.txt
[root@node1 test]# wc -m aa.txt #查询字符
11 aa.txt
[root@node1 test]# wc -l aa.txt #查询行数
1 aa.txt
[root@node1 test]# wc -w aa.txt #查询单词个数
1 aa.txt
head和tail
head [-num] 文件路径
: 查看文件的头部几行 默认前10行 当然-num可以指定任意行数
tail [-num] 文件路径
: 查看文件的尾部几行 默认后10行 当然-num可以指定任意行数
tail -f 文件路径
: 持续跟踪查看指定文件内部变化
# 查看文件的前3行
[root@node1 test]# head -3 2.txt
hello
world
java
# 查看文件的后3行
[root@node1 test]# tail -3 2.txt
aaa
bbb
ccc
#动态查看文件
[root@node1 test]# tail -f 2.txt
hello
world
java
python
sql
aaa
bbb
ccc
vi命令
打开文件: vim 文件名.后缀名
注意: 如果文件存在就打开,不存在就创建
编辑文件: 输入i
进入文件进行编辑
保存文件:
1.先进入命令模式: 按ESC
键
2.进入底线命令模式: 输入冒号:
3.输入命令: w(保存) q(退出) q!(强制退出) wq(保存并退出) wq!(强制保存并退出)