Linux——基础指令(中)

📅 2026/7/16 5:12:06
Linux——基础指令(中)
‍♂️个人主页进击的荆棘作者其它专栏《数据结构与算法》《算法》《C起始之路》《Linux》目录1.Linux背景2.使用XShell远程登录Linux3.Linux下基本指令4.Linux权限的问题3.Linux下基本指令3.9mv指令mv命令是move的缩写可以用来移动文件或者将文件改名moverenamefiles经常用来备份文件或目录语法mv [选项] 源文件或目录 目标文件或目录功能1.视mv命令中第二个参数类型的不同是目标文件还是目标目录mv命令将文件重命名或将其移至一个新的目录中。2.当第二个参数类型是文件时mv命令完成文件重命名此时源文件只能有一个也可以是源目录名它将所给的源文件或目录重命名为给定的目标文件名。3.当第二个参数是已存在的目录名称时源文件或目录参数可以有多个mv命令将各参数指定的源文件均移至目标目录中。常用选项●-fforce强制的意思若目标文件已经存在时不会询问而直接覆盖●-i若目标文件destination已经存在时就会询问是否覆盖例如//更改名称 [rootVM-0-6-centos ~]# touch myfile.txt [rootVM-0-6-centos ~]# ll total 0 -rw-r--r-- 1 root root 0 Jul 9 21:45 myfile.txt [rootVM-0-6-centos ~]# mv myfile.txt yourfile.txt [rootVM-0-6-centos ~]# ll total 0 -rw-r--r-- 1 root root 0 Jul 9 21:45 yourfile.txt //若当前路径存在同名文件改名即覆盖 [rootVM-0-6-centos ~]# touch myfile.txt [rootVM-0-6-centos ~]# ll total 0 -rw-r--r-- 1 root root 0 Jul 9 21:46 myfile.txt -rw-r--r-- 1 root root 0 Jul 9 21:45 yourfile.txt [rootVM-0-6-centos ~]# mv yourfile.txt myfile.txt mv: overwrite ‘myfile.txt’? y [rootVM-0-6-centos ~]# ll total 0 -rw-r--r-- 1 root root 0 Jul 9 21:45 myfile.txt //若当前路径存在同名文件改名前可以添加i选项让系统提出警告供用户做出选择 [rootVM-0-6-centos ~]# touch yourfile.txt [rootVM-0-6-centos ~]# ll total 0 -rw-r--r-- 1 root root 0 Jul 9 21:45 myfile.txt -rw-r--r-- 1 root root 0 Jul 9 21:49 yourfile.txt [rootVM-0-6-centos ~]# mv -i yourfile.txt myfile.txt mv: overwrite ‘myfile.txt’? y [rootVM-0-6-centos ~]# ll total 0 -rw-r--r-- 1 root root 0 Jul 9 21:49 myfile.txt //mv整个文件 [rootVM-0-6-centos ~]# touch myfile.txt [rootVM-0-6-centos ~]# mkdir temp [rootVM-0-6-centos ~]# ll total 4 -rw-r--r-- 1 root root 0 Jul 9 21:55 myfile.txt drwxr-xr-x 2 root root 4096 Jul 9 21:55 temp [rootVM-0-6-centos ~]# mv myfile.txt temp [rootVM-0-6-centos ~]# ll total 4 drwxr-xr-x 2 root root 4096 Jul 9 21:55 temp [rootVM-0-6-centos ~]# mv temp ../ [rootVM-0-6-centos ~]# ls [rootVM-0-6-centos ~]# ls -d ../temp //-d只显示目录本身不显示目录内容 ../temp3.10cat指令语法cat [选项] [文件]功能查看目标文件的内容常用文件●-n对输出的所有行编号例如//测试cat基本命令 [rootVM-0-6-centos ~]# echo hello linux myfile.txt [rootVM-0-6-centos ~]# cat myfile.txt hello linux //cat输出携带行号 [rootVM-0-6-centos ~]# cat -n myfile.txt 1 hello linux3.11more指令语法more [选项]功能more命令功能类似cat常用选项●-n指定输出行数●q退出more例如//命令行输出多行文本 [rootVM-0-6-centos ~]# nano myfile.txt [rootVM-0-6-centos ~]# more myfile.txt hello linux hello me hello you hello hello linux hello me hello you hello linux hello me hello you hello linux hello me hello you hello linux hello me hello you //-n输出指定行数文本 [rootVM-0-6-centos ~]# more -10 myfile.txt hello linux hello me hello you hello hello linux hello me hello you hello linux hello me hello you hello linux --More--(68%)3.12less指令●less工具也是对文件或其它输出进行分页显示的工具应该说是linux正统查看文件内容的工具功能及其强大●less的用法比起more更加的有弹性在more的时候并没有办法向前面翻只能往后面看●但若使用了less就可以使用[pageup][pagedown]等按键的功能来往前往后翻看文件更容易用来查看一个文件的内容●除此之外在less里可以拥有更多的搜索功能不止可以向下搜也可以向上搜语法less [参数] 文件功能less与more类似但使用less可以随意浏览文件而more只能向前移动却不能向后移动而less在查看之前不会加载整个文件选项-N 显示每行的行号qquit例如//测试搜索和-N等功能 [rootVM-0-6-centos ~]# less -N myfile.txt 1 hello linux 2 hello me 3 hello you 4 hello hello linux 5 hello me 6 hello you 7 hello linux 8 hello me 9 hello you 10 hello linux 11 hello me 12 hello you 13 hello linux 14 hello me 15 hello you 16 myfile.txt (END)3.13head指令head与tail就像它的名字一样它是用来显示开头或结尾某个数量的文字区块head用来显示档案的开头到标准输出中而tail想当然就是看档案的结尾。语法head [参数]… [文件]…功能head用来显示档案的开头至标准输出中默认head命令打印其相应文件的开头10行选项-n行数 显示的行数例如[rootVM-0-6-centos ~]# head myfile.txt hello linux hello me hello you hello hello linux hello me hello you hello linux hello me hello you hello linux [rootVM-0-6-centos ~]# head -5 myfile.txt hello linux hello me hello you hello hello linux hello me3.14tail指令tail命令从指定点开始将文件写到标准输出。使用tail命令的-f选项可以方便的查阅正在改变的日志文件tail -f filename会把filename里最尾部的内容显示在屏幕上并且不断刷新使你看到最新的文件内容。语法tail 必要参数 [文件]功能用于显示指定文件末尾内容不指定文件时作为输入信息进行处理。常用查看日志文件。选项●-f循环读取●-n行数 显示行数例如重定向和管道[rootVM-0-6-centos ~]# tail myfile.txt hello linux hello me hello you hello linux hello me hello you hello linux hello me hello you [rootVM-0-6-centos ~]# tail -3 myfile.txt hello me hello you //空行也算一行 //如何显示文件的[5,10]行的内容 [rootVM-0-6-centos ~]# head -10 myfile.txt | tail -5 hello you hello linux hello me hello you hello linux3.15date指令指定格式显示时间date %Y:%m:%d用法date [OPTION]… [FORMAT]3.15.1在显示方面使用者可以设定想要显示的格式格式设定为一个加号后接数各标记其中常用的标记列表如下●%H小时00..23●%M分钟00..59●%S秒00..59●%X相当于%H:%M:%S●%d日01..31●%m月份01..12●%Y完整年份0000..9999●%F相当于%Y-%m-%d3.15.2在设定时间方面●date -s //设置当前时间只有root权限才能设置其他只能查看●date -s 20080523 //设置成20080523这样会把具体时间设置为空00:00:00●date -s 01:01:01 //设置具体时间不会对日期做更改●date -s 01:01:01 2008-05-23 //这样可以设置全部时间●date -s 01:01:01 20080523 //这样可以设置全部时间●date -s 2008-05-23 01:01:01 //这样可以设置全部时间●date -s 20080523 01:01:01 //这样可以设置全部时间3.15.3时间戳●时间-时间戳date %s●时间戳-时间date -d 1508749502●Unix时间戳英文为Unix epochUnix timePOSIX time或Unix timestamp是从1970年1月1日UTC/GMT的午夜开始所经过的秒数不考虑闰数//显示常规时间 [rootVM-0-6-centos ~]# date Sat Jul 11 18:06:28 CST 2026 [rootVM-0-6-centos ~]# date %Y/%m/%d 2026/07/11 [rootVM-0-6-centos ~]# date %Y/%m/%d-%H:%M:%S 2026/07/11-18:07:25 //显示时间戳 [rootVM-0-6-centos ~]# date %s 1783764501 //时间戳转化成可视时间 [rootVM-0-6-centos ~]# date %Y/%m/%d-%H:%M:%S -d 0 1970/01/01-08:00:00 [rootVM-0-6-centos ~]# date %Y/%m/%d-%H:%M:%S -d 100000 1970/01/02-11:46:40 [rootVM-0-6-centos ~]# date %Y/%m/%d-%H:%M:%S -d 1000000000 2001/09/09-09:46:403.16cal指令cal命令可以用来显示公历阳历。公历是现在国际通用的历法又称列历通称阳历。“阳历”又名“太阳历”系以地球绕太阳一周为一年为西方各国所通用故又称“西历”。命名格式cal 参数 [年份]功能用于查看日历等时间信息若只有一个参数则表示年份1-9999如有两个参数则表示月份和年份常用选项●-3 显示系统前一个月当前月下一个月的月历●-j 显示在当年中的第几天一年日期按天算从1月1日算起默认显示当前月再一年中的天数●-y 显示当前年份的日历//常规案例 [rootVM-0-6-centos ~]# cal July 2026 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 [rootVM-0-6-centos ~]# cal 1949 1949 January February March Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 1 2 3 4 5 1 2 3 4 5 2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12 9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19 16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26 23 24 25 26 27 28 29 27 28 27 28 29 30 31 30 31 April May June Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 1 2 3 4 5 6 7 1 2 3 4 3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11 10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18 17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25 24 25 26 27 28 29 30 29 30 31 26 27 28 29 30 July August September Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 1 2 3 4 5 6 1 2 3 3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 10 10 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 17 17 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 24 24 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 30 31 October November December Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 1 2 3 4 5 1 2 3 2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10 9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17 16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24 23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31 30 31 [rootVM-0-6-centos ~]# cal -3 June 2026 July 2026 August 2026 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 1 2 3 4 1 7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8 14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15 21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22 28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29 30 313.17find指令●Linux下find命令在目录结构中搜索文件并执行指定的操作。●Linux下find命令提供了相当多的查找条件功能很强大。由于find具有强大的功能所以它的到选项也很多。●即使系统中含有网络文件系统NFSfind命令在该文件系统中同样有效只要具有相应的权限。●在运行一个非常消耗资源的find命令时很多人都倾向于把它放在后台执行因为遍历一个大的文件系统可能会花费很长时间30G字节以上的文件系统。●语法find pathname -options●功能用于在文件树中查找文件并作出相应处理可能访问磁盘常用选项●-name 按照文件名查找文件●其它选项需要再查此命令较为复杂//从当前目录下搜索执行名称的文件 [rootVM-0-6-centos ~]# find -name myfile.txt ./myfile.txt //在指定目录下~家目录搜索执行名称的文件 [rootVM-0-6-centos ~]# find ~ -name myfile.txt /root/myfile.txt3.18which指令功能搜索系统指定的命令例如[rootVM-0-6-centos ~]# which ls alias lsls --colorauto /usr/bin/ls [rootVM-0-6-centos ~]# which pwd /usr/bin/pwd3.19whereis指令功能用于找到程序的源、二进制文件或手册例如[rootVM-0-6-centos ~]# whereis ls ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz [rootVM-0-6-centos ~]# whereis libc.so libc: /usr/lib64/libc.so /usr/share/man/man7/libc.7.gz //libcC标准库 //.soshared object动态共享库3.20alias指令功能设置命令的别名例如[rootVM-0-6-centos ~]# alias hellols -a -l [rootVM-0-6-centos ~]# which hello alias hellols -a -l /usr/bin/ls [rootVM-0-6-centos ~]# hello total 84 dr-xr-x---. 7 root root 4096 Jul 9 22:03 . dr-xr-xr-x. 19 root root 4096 Jul 11 20:26 .. -rw------- 1 root root 20131 Jul 11 20:26 .bash_history -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile -rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc drwxr-xr-x 4 root root 4096 Mar 22 02:55 .cache drwxr-xr-x 3 root root 4096 Mar 7 2019 .config -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc -rw------- 1 root root 69 Jul 4 23:17 .lesshst -rw-r--r-- 1 root root 162 Jul 9 22:09 myfile.txt -rw-r--r-- 1 root root 44 Mar 20 22:54 .npmrc drwxr-xr-x 2 root root 4096 Jul 2 21:41 .orca_term drwxr-xr-x 2 root root 4096 Mar 20 22:54 .pip -rw-r--r-- 1 root root 73 Mar 20 22:54 .pydistutils.cfg drwx------ 2 root root 4096 Mar 20 23:34 .ssh -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc3.21grep指令语法grep [选项] 搜索字符串 文件功能在文件中搜索字符串将找到的行打印出来常用选项●-i忽略大小写的不同所以大小写视为相同●-n顺便输出行号●-v反向选择即显示出没有搜索字符串内容的那一行//文件内容 [rootVM-0-6-centos ~]# nano tmp.txt [rootVM-0-6-centos ~]# cat tmp.txt hEllo Linux hello Linux heLlo Linux Hello Linux hello Linux hellO Linux hello Linux hello Linux hello Linux hello Linux hello Linux //基本查找 [rootVM-0-6-centos ~]# grep hello Linux tmp.txt hello Linux hello Linux hello Linux hello Linux hello Linux hello Linux hello Linux //忽略大小写的不同所以大小写视为相同 [rootVM-0-6-centos ~]# grep -i hello Linux tmp.txt hEllo Linux hello Linux heLlo Linux Hello Linux hello Linux hellO Linux hello Linux hello Linux hello Linux hello Linux hello Linux //顺便输出行号 [rootVM-0-6-centos ~]# grep -n hello Linux tmp.txt 2:hello Linux 5:hello Linux 7:hello Linux 8:hello Linux 9:hello Linux 10:hello Linux 11:hello Linux //反向选择即没有搜索字符串内容的那一行 [rootVM-0-6-centos ~]# grep -v hello Linux tmp.txt hEllo Linux heLlo Linux Hello Linux hellO Linux [rootVM-0-6-centos ~]# grep -nv hello Linux tmp.txt 1:hEllo Linux 3:heLlo Linux 4:Hello Linux 6:hellO Linux 12: [rootVM-0-6-centos ~]# grep -nvi hello Linux tmp.txt 12:3.22top1 top -d 1 -n 5-d刷新的时间间隔-n刷新的次数q退出3.23zip/unzip指令语法zip dst.zip src可以是目录或文件功能将目录或文件压缩成zip格式常用选项●-r递归处理将指定目录下的所有文件和子目录一并处理例如将test文件压缩zip test.zip test.txt将test目录压缩zip -r test.zip test解压到root目录unzip test.zip -d /root关于sz/rz这个工具用于Windows机器和远端的Linux机器通过XShell传输文件安装完毕之后可以通过拖拽的文件将文件上传过去。安装指令sudo yum/apt install -y lrzlz也可以用指令传输文件将test.zip从Linux传输到Windowssz test.zip将test.zip从Windows传输到Linuxrz3.24tar指令打包/解包不打开它直接看内容语法tar [-cxtzjvf] dst.tgz src常用选项●-c建立一个压缩文件参数指令即create●-x解开一个压缩文件的参数指令●-t查看tarfile里面的文件●-z是否同时具有gzip的属性亦即是否需要用gzip压缩●-j是否同时具有bzip2的属性亦即是否需要用bzip2压缩●-v压缩的过程中显示文件这个常用但不建议用在背景执行过程●-f使用档名在f之后利己接档名不要再加参数●-C解压到指定目录例如示例一将整个/etc目录下的文件全部打包成/tmp/etc.tar[rootlinux ~]$ tar -cvf /tmp/etc.tar /etc 仅打包不压缩[rootlinux ~]$ tar -zcvf /tmp/etc.tar.gz /etc 打包后以gzip压缩[rootlinux ~]$ tar -jcvf /tmp/etc.tar.bz2 /etc 打包后以bzip2压缩特别注意在参数f之后的文件档名是自己取的习惯上都用.tar来作为辨识。若加z参数则以.tar.gz或.tgz来代表gzip压缩过的tar file若加j参数则以.tar.bz2来作为附档名示例二查阅上述/tmp/etc.tar.gz文件内有哪些文件[rootlinux ~]$ tar -tzvf /tmp/etc.tar.gz由于使用gzip压缩所以要查阅该tar file内的文件时就要加上z这个参数示例三将tmp/etc.tar.gz文件解压缩在/usr/local/src底下[rootlinux ~]$ cd /usr/local/src[rootlinux ~]$ tar -xzvf /tmp/etc.tar.gz也可以直接[rootlinux ~]$ tar -xzvf /tmp/etc.tar.gz -C /usr/local/src在预设情况下可以将压缩档任何地方解开。另外若进入/usr/local/src/etc会发现该目录下的文件属性与/etc可能会有所不同。示例四在/tmp下只想将tmp/etc.tar.gz内的etc/passwd解开[rootlinux ~]$ cd /tmp[rootlinux ~]$ tar -xzvf /tmp/etc.tar.gz etc/passwd也可以直接[rootlinux ~]$ tar -xzvf /tmp/etc.tar.gz/passwd -C /tmp可以通过tar -ztvf来查阅tar file内的文件名称若单只要一个文件就可以通过这个方式来下达。注意etc.tar.gz内的根目录/是被拿掉了3.25bc指令bc命令可以很方便的进行浮点运算3.26uname -r指令语法uname [选项]功能uname用来获取电脑和操作系统的相关信息补充说明uname可显示Linux主机所用的操作系统的版本、硬件的名称等基本信息。常用选项●-a或-all详细输出所有信息依次为内核名称主机名内核版本号内核版本硬件名处理器类型硬件平台类型操作系统名称●lsb_release -a查看操作系统版本3.27重要的几个热键●[Tab]按键---具有【命令补全】和【档案补齐】的功能。快速摁两下。●[Ctrl]c按键---让当前的程序【停掉】●[Ctrl]d按键---通常代表着【键盘输入结束End Of FileEOF或End Of Input】的意思它也可以用来取代exit。退出当前用户●[上下键]---查看历史命令●[Ctrl]r按键---搜索历史命令3.28关机语法shutdown [选项]常见选项●-h将系统的服务停掉后立即关机●-r在系统的服务停掉之后就重新启动●-t sec-t后加秒数即【过几秒后关机】的意思3.29扩展命令●安装和登录命令login、shutdown、halt、reboot、install、mount、umount、chsh、exit、last●文件处理命令file、mkdir、grep、dd、find、mv、ls、diff、cat、ln●系统管理相关命令df、top、free、quota、at、lp、adduser、deluser、groupadd、kill、crontab●网络操作命令ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、mail、nslookup●系统安全相关命令passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、who●其它命令tar、unzip、gunzip、unarj、mtools、man、unendcode、uudecode。3.30shell命令以及运行原理Linux严格意义上说的是一个操作系统我们称之为“核心kernel”但对用户来说不能直接使用kernel。而是通过kenel的“外壳”程序即shell去与kernel沟通。如何理解为什么不能直接使用kernel从技术角度Shell的最简单定义命令行解释器command interpreter主要包含●将使用者的命令翻译给核心kernel处理●同时将核心的处理结果翻译给使用者对于windows GUI操作windows不是直接操作windows内核而是通过图形接口点击从而完成相应的操作如进入D盘的操作通常是双击D盘盘符运行应用程序也可以如此。shell对于Linux有相应的作用主要是对我们的指令进行解析解析指令给Linux内核。反馈结果再通过内核运行出结果通过shell解析给用户。