Linux基础命令

一、Linux命令组成及书写格式

# 格式
command [-options] [parameter]

# 格式解释
命令名 [-选项] [参数]
# 注意:中括号内为可选项

二、Linux的基础命令

1.ls命令

ls来源于list(列表),用于查看当前目录下所有的子级

[root@my_node1 ~]# ls
1.txt  2.txt  aa

ls -l 以行的方式展示当前目录下的所有子级(不包括隐藏文件),可简写为ll

[root@my_node1 ~]# ls -l
总用量 0
-rw-r--r--. 1 root root  0 1月   6 18:07 1.txt
-rw-r--r--. 1 root root  0 1月   6 18:07 2.txt
drwxr-xr-x. 2 root root 34 1月   6 18:08 aa

ls -a 显示当前目录下所有的子级(包括隐藏文件)

[root@node1 ~]# ls -a 
. abc .bash_logout .cache .cshrc .esd_auth hhh.txt .kettle .pentaho .superset 公 共 文 档 
.. abcd .bash_profile cba .dbus hello .ICEauthority .local .pki .tcshrc 模 板 下 载 
aaa anaconda-ks.cfg .bashrc ccc.txt ddd.txt hello.java initial-setup-ks.cfg .mozilla .python_history .viminfo 视 频 音 乐 
aaa.txt .bash_history bbb.txt .config eee.txt hello linux kettle .mysql_history .speech-dispatcher .viminfo.tmp 图 片 桌 面 

ls -lh 以人性化方式显示当前目录下的所有子级(不包括隐藏文件),可简写为ll - h 。** h选项必须搭配l一起使用,否则没啥效果 **

[root@my_node1 ~]# ls -lh
总用量 0
-rw-r--r--. 1 root root  0 1月   6 18:07 1.txt
-rw-r--r--. 1 root root  0 1月   6 18:07 2.txt
drwxr-xr-x. 2 root root 34 1月   6 18:08 aa

ls -al 以行的方式展示当前目录下所有子级(包括隐藏文件)

[root@node1 ~]# ls -al 
总 用 量 164 
dr-xr-x---. 25 root root 4096 1月 6 17:55 . 
dr-xr-xr-x. 17 root root 224 1月 6 17:53 .. 
drwxr-xr-x. 3 root root 17 1月 6 17:01 aaa 
-rw-r--r--. 1 root root 10132 1月 6 17:18 aaa.txt 
drwxr-xr-x. 2 root root 19 1月 6 17:55 abc 
drwxr-xr-x. 3 root root 30 1月 6 17:50 abcd 
-rw-------. 1 root root 1491 3月 31 2020 anaconda-ks.cfg 
-rw-------. 1 root root 15959 1月 3 22:00 .bash_history 
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout 
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile 
-rw-r--r--. 1 root root 644 4月 3 2020 .bashrc 
-rw-r--r--. 1 root root 20 1月 6 17:35 bbb.txt 
drwx------. 17 root root 4096 4月 3 2020 .cache 
drwxr-xr-x. 2 root root 6 1月 6 17:42 cba 
-rw-r--r--. 1 root root 0 1月 6 17:07 ccc.txt 

ls -alh 以行的方式人性化的展示当前目录下的所有子级(包括隐藏文件)

[root@node1 ~]# ls -alh 
总 用 量 164K 
dr-xr-x---. 25 root root 4.0K 1月 6 17:55 . 
dr-xr-xr-x. 17 root root 224 1月 6 17:53 .. 
drwxr-xr-x. 3 root root 17 1月 6 17:01 aaa 
-rw-r--r--. 1 root root 9.9K 1月 6 17:18 aaa.txt 
drwxr-xr-x. 2 root root 19 1月 6 17:55 abc 
drwxr-xr-x. 3 root root 30 1月 6 17:50 abcd 
-rw-------. 1 root root 1.5K 3月 31 2020 anaconda-ks.cfg 
-rw-------. 1 root root 16K 1月 3 22:00 .bash_history 
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout 
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile 
-rw-r--r--. 1 root root 644 4月 3 2020 .bashrc 
-rw-r--r--. 1 root root 20 1月 6 17:35 bbb.txt 

ls 指定目录 查看指定目录下的所有子级(可搭配以上选项使用)

[root@my_node1 ~]# ls aa
11.txt  22.txt

2.cd命令

cd命令来源于 change directory ,改变目录
cd 指定目录 切换到指定目录下
cd ~ 返回到家目录

[root@my_node1 aa]# cd
[root@my_node1 ~]# 

cd..返回上一级目录

[root@my_node1 cc]# pwd		# 打印当前所在目录(不认识没关系,马上就认识啦)
/root/aa/bb/cc
[root@my_node1 cc]# cd ..		# 从/root/aa/bb/cc目录跳到了/root/aa/bb目录
[root@my_node1 bb]# pwd
/root/aa/bb

cd - 在最近的两个目录之间进行切换

[root@my_node1 bb]# pwd 
/root/aa/bb
[root@my_node1 bb]# cd
[root@my_node1 ~]# pwd
/root
[root@my_node1 ~]# cd -		# 在/root目录和/root/aa/bb目录下进行跳转
/root/aa/bb

3.pwd命令

pwd命令 来源于print work directory ,打印当前的工作目录

[root@my_node1 bb]# pwd
/root/aa/bb

4.mkdir命令

mkdir命令 来源于make directory,制作目录(文件夹)
mkdir [-p] 目录 # 不加-p只能创建单级目录,如果写了-p可以创建多级目录

[root@my_node1 ~]# ls
1.txt  2.txt
[root@my_node1 ~]# mkdir -p aa/bb
[root@my_node1 ~]# ls
1.txt  2.txt  aa
[root@my_node1 ~]# cd aa
[root@my_node1 aa]# ls
bb

5.touch 命令

touch命令 用于制作文件

[root@my_node1 ~]# ls
1.txt  2.txt  aa
[root@my_node1 ~]# touch 3.txt
[root@my_node1 ~]# ls
1.txt  2.txt  3.txt  aa

6.查看命令合集

6.1 cat命令

cat命令 用于查看文件内容
cat 文件路径|文件名 # 查看所有内容

[root@my_node1 ~]# cat 1.txt	# 查看1.txt文件的内容
hello Linux
hello 小伙伴~
祝大家都能学好Linux~

6.2 more命令

more命令 用于查看文件内容,支持翻页查看
more 文件路径|文件名 # 空格:下一页, enter:下一行, b(back):上一页 , d(down):下一页

6.3 head命令

head命令 查看文件(前面)内容
格式:
head [-n] 数字 文件路径|文件

[root@my_node1 ~]# head -n 5 1.txt		# 查看1.txt的前5行内容
1 hello Linux
2 hello 小伙伴~
3 祝大家都能学好Linux~
4 祝大家都能学好Linux~
5 hello Linux

[root@my_node1 ~]# head 1.txt	# 默认查看前10行
1 hello Linux
2 hello 小伙伴~
3 祝大家都能学好Linux~
4 祝大家都能学好Linux~
5 hello Linux
6 hello Linux
7 hello Linux
8 hello Linux
9 hello Linux
10 hello Linux

6.4 tail 命令

tail命令 查看文件(后面)内容
格式:
tail [-nf] 文件路径
n表示要查看的行数,默认后10行
f表示动态查看,一般用于查看日志信息。

[root@my_node1 ~]# tail -3 1.txt		# 查看1.txt的后3行
24 hello 小伙伴~
25 祝大家都能学好Linux~
26 祝大家都能学好Linux~


[root@my_node1 ~]# tail -5f 1.txt	# 动态查看后5行的内容,持续运行中
22 祝大家都能学好Linux~
23 祝大家都能学好Linux~
24 hello 小伙伴~
25 祝大家都能学好Linux~
26 祝大家都能学好Linux~

7.echo命令

echo命令 把结果输出到控制台
格式:
echo 内容

[root@my_node1 ~]# echo 'hello world'		# 将hello world 输出到控制台
hello world

8.重定向

> 覆盖, >> 追加
示例:
搭配echo使用,将hello world 输入到2.txt

[root@my_node1 ~]# echo hello world > 2.txt
[root@my_node1 ~]# cat 2.txt
hello world

将hello Linux追加到2.txt

[root@my_node1 ~]# echo hello Linux >>  2.txt
[root@my_node1 ~]# cat 2.txt
hello world
hello Linux

将Linux hello 覆盖到2.txt

[root@my_node1 ~]# echo Linux hello > 2.txt
[root@my_node1 ~]# cat 2.txt
Linux hello

9.cp命令

cp命令 来源于copy,拷贝的意思
格式:
cp [-r] 文件a|文件夹a 文件b|文件夹b # 将a文件的内容拷贝到b文件,如果是文件夹需要加-r

[root@my_node1 ~]# cp 2.txt 3.txt		# 将2.txt的内容拷贝到3.txt
[root@my_node1 ~]# cat 3.txt
Linux hello

10.mv命令

mv命令 来源于move,剪切的意思
mv 1.txt 2.txt # 改名

[root@my_node1 ~]# ls
1.txt  3.txt  aa
[root@my_node1 ~]# mv 1.txt 2.txt
[root@my_node1 ~]# ls
2.txt  3.txt  aa

mv 1.txt aa/bb #剪切到aa/bb目录下

[root@my_node1 ~]# ls
1.txt  3.txt  aa
[root@my_node1 ~]# mv 1.txt aa/bb		# 将1.txt剪切到aa/bb目录下
[root@my_node1 ~]# ls aa/bb
1.txt
[root@my_node1 ~]# ls
3.txt  aa

11.rm命令

rm命令 来源于remove ,删除的意思
格式:
rm [-rf] 文件|文件夹 # r即recursive递归,f即force强制,删除文件夹必须带r

[root@my_node1 ~]# ls
3.txt  aa
[root@my_node1 ~]# rm -rf aa		# 删除aa目录及目录下所有子级
[root@my_node1 ~]# ls
3.txt

12.which命令

which命令 ,查找二进制脚本所在的目录

[root@my_node1 ~]# which ls		# 查看ls脚本所在目录
        /usr/bin/ls

13.find命令

find命令 ,查找命令,根据条件查找文件
格式:
find 目录路径 -size +数字K|M|G

[root@my_node1 ~]# find / -size +10M	# 从/目录开始向子级查找大于10mb的文件
/boot/initramfs-0-rescue-5b6da43ad72746b893ca5e490b8deb92.img
/boot/initramfs-3.10.0-693.el7.x86_64.img
/boot/initramfs-3.10.0-693.el7.x86_64kdump.img

find 目录路径 -name 文件名

[root@my_node1 ~]# find / -name '*.txt'		# 从/目录开始向子级查找以.txt结尾的文件
/etc/pki/nssdb/pkcs11.txt
/etc/brltty/brl-lb-all.txt
/etc/brltty/brl-lt-all.txt
/etc/brltty/brl-mb-all.txt
/etc/brltty/brl-md-all.txt
/etc/brltty/brl-mn-all.txt

14.grep命令

grep命令,去文件中筛选出包含指定内容的所有行。
格式:
grep [-n] 关键字 文件 # -n用于显示行号

[root@my_node1 ~]# grep -n hello 3.txt		# 去3.txt中找到包含hello的行,并显示行号
4:hello world
5:hello world
6:linux hello
7:hello 123

15.wc命令

wc命令 ,来源于 word count 单词,用做词频统计
格式:
wc [-c|-m|-l|-w] 文件
选项解释:
-c 统计字节数
-m 统计字符数
-l 统计行数, line
-w 统计单词数, word
注:GBK编码中1个汉字占2个字节,UTF-8中1个汉字占3个字节

[root@my_node1 ~]# wc -lwmc 3.txt 	# 统计3.txt的行数,单词数,字符数,字节数。
 7 14 74 74 3.txt
[root@my_node1 ~]# cat 3.txt
linux cp
linux ls
linux pwd
hello world
hello world
linux hello
hello 123

未完待续~
我们下期再见~

  • 40
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值