Linux命令行基本操作

发布时间 2023-10-10 20:52:23作者: kihdd

本例要求熟悉新装LINUX系统中命令行界面的获取方法,并通过命令行完成下列任务:

  1. pwd、cd、ls命令练习
  2. 路径练习
  3. 路径切换练习
  4. cat命令练习
  5. less命令练习
  6. hostname命令练习
  7. 显示CPU与内存
  8. 查看IP地址
  9. 创建数据练习
  10. 查看部分文件内容
  11. 过滤文件内容
  12. vim文本编辑器
  13. 关机与重启

简单命令行操作练习

1)pwd、cd、ls命令练习

  1. [root@localhost ~]# pwd #显示当前所在的位置
  2. [root@localhost ~]# cd / #切换到根目录下
  3. [root@localhost /]# pwd
  4. [root@localhost /]# ls #显示当前目录下内容
  5. [root@localhost /]# cd /boot
  6. [root@localhost boot]# ls
  7. [root@localhost boot]# cd /
  8. [root@localhost /]# ls
  9. [root@localhost /]# cd /home
  10. [root@localhost home]# ls
  11. [root@localhost home]# cd /root
  12. [root@localhost ~]# ls

2)ls命令练习

  1. [root@localhost ~]# cd /etc
  2. [root@localhost etc]# pwd
  3. [root@localhost etc]# ls /root #查看指定目录内容
  4. [root@localhost etc]# ls / #查看根目录内容
  5. [root@localhost etc]# ls /home
  6. [root@localhost etc]# ls /opt
  7. [root@localhost etc]# ls /boot
  8. [root@localhost etc]# ls /var
  9. [root@localhost etc]# ls /bin
  10. [root@localhost etc]# ls /proc
  11. [root@localhost etc]# ls /usr
  12. [root@localhost etc]# ls /tmp
  13. [root@localhost etc]# ls /mnt

3)路径练习

绝对路径:以根开始的路径

相对路径:以当前位置,为参照的路径

  1. [root@localhost ~]# cd /usr/
  2. [root@localhost usr]# ls
  3. bin config games include lib lib64 libexec local sbin share src tmp
  4. [root@localhost usr]# cd games #相对路径
  5. [root@localhost games]# pwd
  6. /usr/games
  7. [root@localhost games]# cd /
  8. [root@localhost /]# cd /usr/games/ #绝对路径
  9. [root@localhost games]# pwd
  10. /usr/games

4)路径切换练习

  1. .. 表示上一层目录(父目录)
  2. [root@localhost /]# cd /etc/pki/rpm-gpg/
  3. [root@localhost rpm-gpg]# pwd
  4. /etc/pki/rpm-gpg
  5. [root@localhost rpm-gpg]# cd ..
  6. [root@localhost pki]# pwd
  7. /etc/pki
  8. [root@localhost pki]# cd ..
  9. [root@localhost etc]# pwd
  10. /etc
  11. [root@localhost etc]# cd ..
  12. [root@localhost /]# pwd
  13. /
  14. [root@localhost /]#

5)颜色:目录为蓝色,文件为黑色(路径书写时必须没有/结尾)

cat查看文本文件内容,适合查看内容较少文件

  1. [root@localhost /]# ls /root/
  2. [root@localhost /]# ls /root/anaconda-ks.cfg
  3. [root@localhost /]# cat /root/anaconda-ks.cfg
  4. [root@localhost /]# cat /root/initial-setup-ks.cfg
  5. [root@localhost /]# cat /etc/passwd
  6. [root@localhost /]# cat /etc/fstab
  7. [root@localhost /]# cat /etc/group
  8. [root@localhost /]# cat /etc/redhat-release #查看系统版本

 6)less查看文本文件内容,适合查看内容较多文件

  1. [root@localhost /]# less /etc/passwd
  2. 按上、下键进行滚动
  3. 按q键进行退出

7)hostname命令练习

  1. [root@localhost /]# hostname
  2. localhost.localdomain
  3. [root@localhost /]# hostname abc.haha.xixi
  4. [root@localhost /]# hostname
  5. abc.haha.xixi
  6. 新开一个全新的命令行终端,查看提示符变化
  7. [root@abc ~]# hostname A.haha.com
  8. 新开一个全新的命令行终端,查看提示符变化
  9. [root@A ~]# hostname

 8)显示CPU与内存

  1. 列出CPU处理器信息
  2. [root@A ~]# lscpu
  3. ……
  4. CPU(s): 1 #核心数
  5. ……
  6. 型号名称:Intel(R) Core(TM) i5-4430 CPU @ 3.00GHz
  7. ……
  8. 列出内存信息
  9. [root@A ~]# cat /proc/meminfo
  10. MemTotal: 997956 kB #一共内存总和
  11. ……

9)查看IP地址

  1. ]# ifconfig
  2. lo: 本机回环接口(此接口专门用于测试)
  3. IP永远为127.0.0.1
  4. 127.0.0.1:永远代表本机
  5. ]# ifconfig eth0 192.168.4.1 #临时设置IP
  6. ]# ifconfig eth0
  7. ]# ping 192.168.4.1
  8. Ctrl+c:结束正在运行命令

10)创建数据

  1. mkdir创建目录
  2. [root@A ~]# mkdir /opt/test
  3. [root@A ~]# ls /opt/
  4. [root@A ~]# mkdir /root/nsd01
  5. [root@A ~]# ls /root/
  6. touch创建文本文件
  7. [root@A ~]# touch /opt/1.txt
  8. [root@A ~]# ls /opt/
  9. [root@A ~]# touch /opt/2.txt
  10. [root@A ~]# ls /opt/

11)查看部分内容

head、tail 命令(查看部分文件内容)

格式:head -n 数字 文件名

tail -n 数字 文件名

  1. [root@A /]# head -1 /etc/passwd
  2. [root@A /]# head -2 /etc/passwd
  3. [root@A /]# head -3 /etc/passwd
  4. [root@A /]# tail -1 /etc/passwd
  5. [root@A /]# tail -2 /etc/passwd
  6. [root@A /]# tail -3 /etc/passwd

12)过滤文件内容

  1. 作用:输出包含指定字符串的行
  2. [root@A /]# grep root /etc/passwd
  3. [root@A /]# grep bash /etc/passwd
  4. [root@A /]# grep lisi /etc/passwd
  5. [root@A /]# grep zhangsan /etc/passwd
  6. [root@A /]# grep haha /etc/passwd

13)vim文本编辑器

vim修改文本文件内容(文本编辑器)

三个模式:命令模式、插入模式(输入模式)、末行模式

vim当文件不存在时,会自动创建此文件

vim不能创建目录

  1. [root@A /]# vim /opt/haxi.txt
  2. --- i键 或者 o键 --->插入模式(Esc回到命令模式)
  3. --- 英文的冒号:--->末行模式(Esc回到命令模式)
  4. 末行模式 :wq #保存并退出
  5. 末行模式 :q! #强制不保存并退出

14)重启系统与关闭系统

  1. 关机poweroff与重启操作系统reboot
  2. [root@A /]# reboot
  3. [root@A /]# poweroff