Linux最基础最常用命令(非常详细)从零基础入门

基础linux命令

1、linux目录结构

在这里插入图片描述

只有一个根目录:  /
多个目录分隔符:  /
如何区分根目录和分隔符: 以/开头的是根目录,后面的都是分隔符    举例: /usr/local

2、基础四大命令

ls: 查看当前目录了下所有内容
cd 目录名: 切换目录
pwd: 查看当前所在位置
clear: 清屏
[root@node1 ~]# cd /
[root@node1 /]# pwd
/
[root@node1 /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
[root@node1 /]# clear

3、linux命令格式

command [-options] [parameter]

说明:
- command : 命令名, 相应功能的英文单词或单词的缩写
- [-options] : 选项, 可用来对命令进行控制, 也可以省略
- parameter  : 传给命令的参数, 可以是 零个、一个 或者 多个

4、ls命令

ls [-a -l -h] [路径] : 查看指定路径下文件列表  注意: 默认都是当前路径

参数 -a : 展示出隐藏文件。all             注意: .开头就是隐藏文件
参数 -l : 以详细信息展示文件。list   	   注意: ls -l 可以简化为ll
参数 -h : 配合-l使用,人性化展示文件大小 
[root@node1 ~]# ls
anaconda-ks.cfg
[root@node1 ~]# ls -a
.   anaconda-ks.cfg  .bash_logout   .bashrc  .config  .tcshrc
..  .bash_history    .bash_profile  .cache   .cshrc
[root@node1 ~]# ls -l
总用量 4
-rw-------. 1 root root 1270 729 10:01 anaconda-ks.cfg
[root@node1 ~]# ls -a -l
总用量 28
dr-xr-x---.  4 root root  164 729 10:20 .
dr-xr-xr-x. 17 root root  237 729 10:57 ..
-rw-------.  1 root root 1270 729 10:01 anaconda-ks.cfg
-rw-------.  1 root root   34 729 10:20 .bash_history
-rw-r--r--.  1 root root   18 1229 2013 .bash_logout
-rw-r--r--.  1 root root  176 1229 2013 .bash_profile
-rw-r--r--.  1 root root  176 1229 2013 .bashrc
drwxr-xr-x.  3 root root   18 729 10:02 .cache
drwxr-xr-x.  3 root root   18 729 10:02 .config
-rw-r--r--.  1 root root  100 1229 2013 .cshrc
-rw-r--r--.  1 root root  129 1229 2013 .tcshrc
[root@node1 ~]# ll
总用量 4
-rw-------. 1 root root 1270 729 10:01 anaconda-ks.cfg
[root@node1 ~]# ll -h
总用量 4.0K
-rw-------. 1 root root 1.3K 729 10:01 anaconda-ks.cfg
[root@node1 ~]# ll -a -h
总用量 28K
dr-xr-x---.  4 root root  164 729 10:20 .
dr-xr-xr-x. 17 root root  237 729 10:57 ..
-rw-------.  1 root root 1.3K 729 10:01 anaconda-ks.cfg
-rw-------.  1 root root   34 729 10:20 .bash_history
-rw-r--r--.  1 root root   18 1229 2013 .bash_logout
-rw-r--r--.  1 root root  176 1229 2013 .bash_profile
-rw-r--r--.  1 root root  176 1229 2013 .bashrc
drwxr-xr-x.  3 root root   18 729 10:02 .cache
drwxr-xr-x.  3 root root   18 729 10:02 .config
-rw-r--r--.  1 root root  100 1229 2013 .cshrc
-rw-r--r--.  1 root root  129 1229 2013 .tcshrc
[root@node1 ~]# ls -l /
总用量 24
-rw-r--r--.   1 root root   12 729 10:57 a.txt
lrwxrwxrwx.   1 root root    7 729 09:58 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 729 10:02 boot
drwxr-xr-x.  20 root root 3220 729 10:02 dev
drwxr-xr-x.  85 root root 8192 729 10:02 etc
drwxr-xr-x.   2 root root    6 411 2018 home
lrwxrwxrwx.   1 root root    7 729 09:58 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 729 09:58 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 411 2018 media
drwxr-xr-x.   2 root root    6 411 2018 mnt
drwxr-xr-x.   3 root root   16 729 09:59 opt
dr-xr-xr-x. 127 root root    0 729 10:02 proc
dr-xr-x---.   4 root root  164 729 10:20 root
drwxr-xr-x.  32 root root  940 729 10:02 run
lrwxrwxrwx.   1 root root    8 729 09:58 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 411 2018 srv
dr-xr-xr-x.  13 root root    0 729 10:02 sys
drwxrwxrwt.   9 root root  257 729 10:43 tmp
drwxr-xr-x.  13 root root  155 729 09:58 usr
drwxr-xr-x.  21 root root 4096 729 10:02 var

5、cd和pwd和clear命令

cd [路径] : 切换到指定路径下
家目录: 普通用户的家目录:/home/普通用户名     root用户的家目录:/root       
如何快速回家: cd ~    注意: ~可以省略
快速切换到上次所在的路径: cd -
切换到上一级目录: cd ..

pwd : 查看当前路径 

clear:  清屏       快捷键: ctrl+L
[root@node1 /]# clear

[root@node1 ~]# ls
anaconda-ks.cfg
[root@node1 ~]# cd home
-bash: cd: home: 没有那个文件或目录
[root@node1 ~]# cd /home
[root@node1 home]# cd /
[root@node1 /]# cd home
[root@node1 home]# cd /
[root@node1 /]# ls
a.txt  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@node1 /]# cd /home

[root@node1 home]# pwd
/home

6、相对路径和绝对路径

1. 相对路径和绝对路径
绝对路径: 以根目录'/'做起点,描述路径的方式,路径以/开头
相对路径: 以当前目录做起点,描述路径的方式,路径不需以/开头
如无特殊需求,后续学习中,将经常使用相对路径表示

2. 特殊路径符有哪些?
.  表示当前目录,  比如:  cd .    或 cd ./Desktop
.. 表示上一级目录,比如: cd ..  或 cd ../..
[root@node1 ~]# cd .
[root@node1 ~]# pwd
/root
[root@node1 ~]# cd ..
[root@node1 /]# pwd
/
[root@node1 /]# ls
a.txt  boot  etc   lib    media  opt   root  sbin  sys  usr
bin    dev   home  lib64  mnt    proc  run   srv   tmp  var
[root@node1 /]# cd /home
[root@node1 home]# cd ..
[root@node1 /]# cd home
[root@node1 home]# cd ..
[root@node1 /]# cd ./home
[root@node1 home]# cd ..

[root@node1 /]# cd /usr/local
[root@node1 local]# cd ../..

[root@node1 /]# cd
[root@node1 ~]# ls
anaconda-ks.cfg
[root@node1 ~]# cd /home

7、mkdir命令

mkdir [-p] 目录路径 : 创建指定目录   
	-p:可选,表示自动创建不存在的父目录,适用于创建连续多层级的目录

创建一级目录: mkdir 目录路径
创建多个同级目录: mkdir 目录路径1  目录路径2 ...
创建父子嵌套目录: mkdir -p 目录路径1/目录路径2/...
[root@node1 /]# mkdir binzi
[root@node1 /]# ls
bin    boot  etc   lib    media  opt   root  sbin  sys  usr
binzi  dev   home  lib64  mnt    proc  run   srv   tmp  var
[root@node1 /]# cd binzi
[root@node1 binzi]# cd /
[root@node1 /]# cd /binzi
[root@node1 binzi]# ls
[root@node1 binzi]# mkdir sql/python/java
mkdir: 无法创建目录"sql/python/java": 没有那个文件或目录
[root@node1 binzi]# mkdir -p sql/python/java
[root@node1 binzi]# ls
sql
[root@node1 binzi]# cd sql
[root@node1 sql]# cd python/java
[root@node1 java]# pwd
/binzi/sql/python/java

8、touch命令

touch 文件路径: 创建指定空文件

创建一个文件: touch 文件路径
创建多个文件: touch 文件路径1  文件路径2 ...
[root@node1 java]# cd /binzi
[root@node1 binzi]# ls
sql
[root@node1 binzi]# touch hello.sql
hello.sql  sql
[root@node1 binzi]# ll
总用量 0
-rw-r--r--. 1 root root  0 315 15:14 hello.sql
drwxr-xr-x. 3 root root 20 315 15:10 sql
[root@node1 binzi]# cd sql
[root@node1 sql]# ls
python
[root@node1 sql]# touch 1.py 2.py 3.py 4.txt 5.txt 6.txt
[root@node1 sql]# ls
1.py  2.py  3.py  4.txt  5.txt  6.txt  python

9、echo命令

echo 要打印的内容

注意: 默认打印到控制台,但是经常配合重定向符号使用

注意: echo配合反引号``可以先识别出关键字,然后把对应的内容输出
[root@node1 sql]# echo "helloworld"
helloworld
[root@node1 sql]# echo 'helloworld'
helloworld
[root@node1 sql]# echo helloworld
helloworld
[root@node1 test]# echo 'ls'
ls
[root@node1 test]# echo `ls`
1.txt 2.txt 3.py a b c

10、重定向符号

> : 覆盖写入
>> : 追加写入

注意: 一般都是配合echo使用。工作中一般是在需要将程序运行的日志写入到指定位置的时候用到。
[root@node1 sql]# echo 'helloworld' > 4.txt
[root@node1 sql]# cat 4.txt
helloworld
[root@node1 sql]# echo '我很帅' > 4.txt             
[root@node1 sql]# cat 4.txt
我很帅
[root@node1 sql]# echo '' > 4.txt
[root@node1 sql]# cat 4.txt

[root@node1 sql]# echo '你好' >> 4.txt
[root@node1 sql]# echo '我好' >> 4.txt
[root@node1 sql]# echo '大家好' >> 4.txt
[root@node1 sql]# cat 4.txt

你好
我好
大家好
[root@node1 sql]# pwd
/binzi/sql
[root@node1 sql]# echo 'pwd' >> 4.txt
[root@node1 sql]# cat 4.txt

你好
我好
大家好
pwd
[root@node1 sql]# echo `pwd` >> 4.txt
[root@node1 sql]# cat 4.txt

你好
我好
大家好
pwd
/binzi/sql
[root@node1 sql]# ls
1.py  2.py  3.py  4.txt  5.txt  6.txt  python
[root@node1 sql]# echo `ls` >> 4.txt
[root@node1 sql]# cat 4.txt

你好
我好
大家好
pwd
/binzi/sql
1.py 2.py 3.py 4.txt 5.txt 6.txt python

11、cat_more_less命令

前提: 先利用客户端上传一个大文件test.txt

cat [-n] 文件 :  cat查看文件内容,一般建议查看小文件
		  -n :显示行号
		  
more [-num] 文件:  more查看文件内容,一般建议查看大文件(可以翻页)
		  空格: 下一页
		  b  : 上一页
		  q  : 退出
		  -x : 展示条数  举例: more -10 文件
		  
less [-N] 文件: less查看文件内容,一般建议查看大文件(可以翻页,可以搜索)
		  -N : 显示行号
		  空格: 下一页
		  b  : 上一页
		  q  : 退出
		  /内容: 搜索指定内容 
[root@node1 binzi]# cat  test.txt
[root@node1 binzi]# cat -n test.txt

[root@node1 binzi]# more test.txt
[root@node1 binzi]# more -10 test.txt

[root@node1 binzi]# less test.txt
[root@node1 binzi]# less -N test.txt

12、cp命令

复制的英文单词: copy

cp [-r] 要复制的文件或者文件夹  目标位置 : 复制文件或者文件夹到指定目标位置

注意: -r 主要用于复制文件夹

注意: cp复制文件后,源文件依然存在
[root@node1 binzi]# cp test.txt sql
[root@node1 binzi]# cd sql
[root@node1 sql]# ls
1.py  2.py  3.py  4.txt  5.txt  6.txt  python  test.txt
[root@node1 sql]# cp test.txt test2.txt
[root@node1 sql]# ls
1.py  2.py  3.py  4.txt  5.txt  6.txt  python  python2  test2.txt  test.txt
[root@node1 sql]# cat test2.txt

[root@node1 sql]# cd python2
[root@node1 python2]# ls
java
[root@node1 python2]# cd ..
[root@node1 sql]# cp -r python python2
[root@node1 sql]# ls
1.py  2.py  3.py  4.txt  5.txt  6.txt  python  python2 test2.txt test.txt
[root@node1 sql]# cd python2
[root@node1 python2]# ls
java  python

13、mv命令

移动的英文单词: move

mv 要移动的文件或者文件夹  目标位置: 移动文件或者文件夹到指定目标位置

注意: 目标位置是文件,文件不存在就会直接改名    如果想要把文件移动到目录中,此目录一定要存在,否则当成了普通文件改名

注意: mv移动文件后,源文件不存在
[root@node1 sql]# ls
1.py  2.py  3.py  4.txt  5.txt  6.txt  python  python2  test2.txt  test.txt
[root@node1 sql]# mv test.txt test3.txt
[root@node1 sql]# ls
1.py  2.py  3.py  4.txt  5.txt  6.txt  python  python2  test2.txt  test3.txt
[root@node1 sql]# mv test3.txt aaa
[root@node1 sql]# ls
1.py  2.py  3.py  4.txt  5.txt  6.txt  aaa  python  python2  test2.txt
[root@node1 sql]# ll
...
-rw-r--r--. 1 root root 455 315 16:10 aaa
-rw-r--r--. 1 root root 455 315 16:18 test2.txt
[root@node1 sql]# mv test2.txt python
[root@node1 sql]# ls
1.py  2.py  3.py  4.txt  5.txt  6.txt  aaa  python  python2
[root@node1 sql]# cd python
[root@node1 python]# ls
java  test2.txt

14、rm命令

移除的英文单词: remove

rm [-r -f] 要删除的文件或者文件夹:  删除指定的文件或者文件夹

注意: -r 主要用于删除文件夹        -f :主要用于强制删除文件或者文件夹

删除文件(有提示): rm 文件路径
删除文件(无提示): rm -f 文件路径
删除目录(有提示): rm -r 目录路径
删除目录(无提示): rm -rf 文件路径
[root@node1 sql]# ls
1.py  2.py  3.py  4.txt  5.txt  6.txt  aaa  python  python2

[root@node1 sql]# rm 1.py
rm:是否删除普通空文件 "1.py"?y
[root@node1 sql]# rm -f 2.py
[root@node1 sql]# rm -f 3.py 4.txt 5.txt 6.txt
[root@node1 sql]# ls
aaa  python  python2

[root@node1 sql]# rm python
rm: 无法删除"python": 是一个目录
[root@node1 sql]# rm -r python
rm:是否进入目录"python"? y
rm:是否删除目录 "python/java"?y
rm:是否删除普通文件 "python/test2.txt"?y
rm:是否删除目录 "python"?y
[root@node1 sql]# ls
aaa  python2
[root@node1 sql]# rm -rf python2
[root@node1 sql]# ls
aaa
[root@node1 sql]# rm -rf /binzi/
[root@node1 sql]# cd ..
cd: 获取当前目录时出错: getcwd: 无法访问父目录: 没有那个文件或目录

15、通配符

* :  任意个字符   类似于sql模糊查询中的 %

? :  任意1个字符  类似于sql模糊查询中的 _
[root@node1 /]# mkdir binzi
[root@node1 /]# ls
bin    boot  etc   lib    media  opt   root  sbin  sys  usr
binzi  dev   home  lib64  mnt    proc  run   srv   tmp  var
[root@node1 /]# touch /binzi/1.txt
[root@node1 /]# touch /binzi/2.txt /binzi/3.txt /binzi/4.txt /binzi/5.txt   
[root@node1 /]# cd binzi
[root@node1 binzi]# ls
1.txt  2.txt  3.txt  4.txt  5.txt
[root@node1 binzi]# touch 123.txt 456.txt 789.txt
[root@node1 binzi]# ls
123.txt  1.txt  2.txt  3.txt  456.txt  4.txt  5.txt  789.txt
[root@node1 binzi]# rm 1*
rm:是否删除普通空文件 "123.txt"?y
rm:是否删除普通空文件 "1.txt"?y
[root@node1 binzi]# ls
2.txt  3.txt  456.txt  4.txt  5.txt  789.txt
[root@node1 binzi]# rm -f *5*
[root@node1 binzi]# ls
2.txt  3.txt  4.txt  789.txt
[root@node1 binzi]# rm -f *.txt
[root@node1 binzi]# ls

[root@node1 binzi]# touch 1.txt 2.txt 234.txt  123.txt 456.txt 789.txt 
[root@node1 binzi]# ls
123.txt  1.txt  234.txt  2.txt  456.txt  789.txt
[root@node1 binzi]# touch 112.txt 1123.txt
[root@node1 binzi]# ls
1123.txt  112.txt  123.txt  1.txt  234.txt  2.txt  456.txt  789.txt
[root@node1 binzi]# rm -f ?2*
[root@node1 binzi]# ls
1123.txt  112.txt  1.txt  234.txt  2.txt  456.txt  789.txt
[root@node1 binzi]# rm -f ??2*
[root@node1 binzi]# ls
1.txt  234.txt  2.txt  456.txt  789.txt

16、which和find

which 命令:  查找命令所在位置

主要: find命令也可以结合通配符使用,不加路径代表当前路径下查找

find [查找路径] -name 文件名: 根据文件名查找
find [查找路径] -size [+ -]大小: 根据文件大小查找          	
					+: 大于     
					-: 小于
					
find [查找路径] -type [d f] -name 文件名: 根据文件类型查找 	
					d: 文件夹    
					f: 文件 
[root@node1 /]# find / -name 'test'
[root@node1 /]# find  -name 'test' 
[root@node1 /]# find  -name 'test*'
[root@node1 /]# find  -name '*test'
[root@node1 /]# find  -name '*test*'
[root@node1 /]# find  -size +10M 
[root@node1 /]# find -type d -name 'test'
[root@node1 /]# find -type f -name 'test'

17、管道符 |

管道使用格式:  命令1 | 命令2 | 命令3 ...

解释: 把命令1的结果传递给命令2,然后再把命令2的结果传递给命令3,依次类推...

注意: 管道一般配合grep命令使用较多

sort: 排序
[root@node1 binzi]# touch 3.txt 2.txt 4.txt 1.txt 5.txt
[root@node1 binzi]# mkdir aa bb
[root@node1 binzi]# find -name '*.txt'

[root@node1 binzi]# find -name '*.txt' | sort   技巧: 把所有txt文件查询出来,然后排序,效果明显
[root@node1 binzi]# cat test.txt | less     

18、grep命令

grep [-n] 关键字 文件路径: 在指定文件中搜索指定关键字

注意: -n 主要用于显示搜索到结果的行号

注意: 管道|一般配合grep命令使用较多
[root@node1 binzi]# ls
1.txt  234.txt  2.txt  3.txt  456.txt  789.txt
[root@node1 binzi]# cat 1.txt
python java sql
[root@node1 binzi]# echo `ls` >> 1.txt
[root@node1 binzi]# cat 1.txt         
python java sql
1.txt 234.txt 2.txt 3.txt 456.txt 789.txt
[root@node1 binzi]# grep 'sql' 1.txt
python java sql
[root@node1 binzi]# grep '234' 1.txt   
1.txt 234.txt 2.txt 3.txt 456.txt 789.txt
[root@node1 binzi]# grep -n '234' 1.txt
2:1.txt 234.txt 2.txt 3.txt 456.txt 789.txt
[root@node1 binzi]# ls
1.txt  234.txt  2.txt  3.txt  456.txt  789.txt
[root@node1 binzi]# ls | grep 234
234.txt

[root@node1 binzi]# find -name '*.txt' | grep 78
./789.txt
[root@node1 binzi]# 

19、wc命令

wc [-l -w] 文件路径:  统计指定文件内容的行数或者单词数

-l :  行数

-w :  单词数   注意:空格分隔
[root@node1 test]# echo 'java sql python' >> test.txt
[root@node1 test]# echo 'linux hadoop hive' >> test.txt
[root@node1 test]# echo 'spark flink' >> test.txt
[root@node1 test]# cat test.txt
java sql python
linux hadoop hive
spark flink

[root@node1 test]# wc test.txt
3  8 46 test.txt
[root@node1 test]# wc -c test.txt
46 test.txt
[root@node1 test]# wc -m test.txt
46 test.txt
[root@node1 test]# wc -l test.txt
3 test.txt
[root@node1 test]# wc -w test.txt
8 test.txt

[root@node1 test]# echo 'helloworld' >> test.txt
[root@node1 test]# wc -w test.txt
9 test.txt
[root@node1 test]# 

20、head和tail

head [-num] 文件路径:  查看文件的头部几行   默认前10行   当然-num可以指定任意行数

tail [-num] 文件路径:  查看文件的尾部几行   默认后10行   当然-num可以指定任意行数

tail -f 文件路径:  常用。持续跟踪查看指定文件内部变化
[root@node1 binzi]# head test.txt
[root@node1 binzi]# head -3 test.txt

[root@node1 binzi]# tail test.txt
[root@node1 binzi]# tail -3 test.txt
[root@node1 binzi]# tail -f test.txt        注意: -f持续跟踪文件内容   可以使用ctrl+c退出

在这里插入图片描述在这里插入图片描述在这里插入图片描述

21、vi编辑器

在这里插入图片描述

21.1 vi操作文件三大步
打开文件: vim 文件名.后缀名     注意: 如果文件存在就打开,不存在就创建

编辑文件: 输入i进入文件进行编辑

保存文件:
1.先进入命令模式: 按ESC键  
2.进入底线命令模式: 输入冒号:   
3.输入命令: w(保存)  q(退出)  q!(强制退出)  wq(保存并退出)   wq!(强制保存并退出)
21.2 vi其他命令

在这里插入图片描述在这里插入图片描述

  • 16
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT界的追风者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值