加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 教程 > 正文

Linux基础命令

发布时间:2021-01-25 14:29:18 所属栏目:教程 来源:互联网
导读:基础命令 Linux系统的命令通常都是如下所示的格式: 命令名称[命名参数][命令对象] 获取登录信息 获取登录信息 - 「 w 」 / 「 who 」 / 「 last 」/ 「 lastb 」。 maoli@ubuntu:~$w 08:07:38up2 min ,1 user , load average:0.83,0.64,0.27 USER TTY FROM

基础命令

Linux系统的命令通常都是如下所示的格式:


  1. 命令名称 [命名参数] [命令对象] 

获取登录信息

获取登录信息 - 「w」 / 「who」 / 「last」/ 「lastb」。


  1. maoli@ubuntu:~$ w 
  2. 08:07:38 up 2 min,  1 user,  load average: 0.83, 0.64, 0.27 
  3. USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT 
  4. maoli    tty7     :0               08:06    2:38   2.29s  0.31s /sbin/upstart - 
  5. maoli@ubuntu:~$ who 
  6. maoli    tty7         2020-05-28 08:06 (:0) 
  7. maoli@ubuntu:~$ last 
  8. maoli    tty7         :0               Thu May 28 08:06    gone - no logout 
  9. reboot   system boot  4.15.0-99-generi Thu May 28 08:04   still running 
  10. maoli    tty7         :0               Fri May  1 15:20 - down   (01:26) 
  11. reboot   system boot  4.15.0-99-generi Fri May  1 15:20 - 16:47  (01:26) 
  12. maoli    tty7         :0               Fri May  1 10:12 - crash  (05:08) 
  13. wtmp begins Fri May  1 10:12:22 2020 
  14. maoli@ubuntu:~$ sudo lastb 
  15. [sudo] maoli 的密码:  
  16.  
  17. btmp begins Fri May  1 15:20:29 2020 

查看自己使用的Shell

查看自己使用的Shell - 「ps」。

Shell也被称为“壳”或“壳程序”,它是用户与操作系统内核交流的翻译官,简单的说就是人与计算机交互的界面和接口。目前很多Linux系统默认的Shell都是bash(Bourne Again SHell),因为它可以使用tab键进行命令和路径补全、可以保存历史命令、可以方便的配置环境变量以及执行批处理操作。


  1. maoli@ubuntu:~$ ps 
  2.   PID TTY          TIME CMD 
  3.  3621 pts/1    00:00:00 bash 
  4.  5082 pts/1    00:00:00 ps 

查看命令的说明和位置

查看命令的说明和位置 - 「whatis」 / 「which」 / 「whereis」。


  1. maoli@ubuntu:~$ whatis ps 
  2. ps (1)               - report a snapshot of the current processes. 
  3. maoli@ubuntu:~$ whatis python 
  4. python (1)           - an interpreted, interactive, object-oriented programmi... 
  5. maoli@ubuntu:~$ whereis ps 
  6. ps: /bin/ps /usr/share/man/man1/ps.1.gz 
  7. maoli@ubuntu:~$ whereis python 
  8. python: /usr/bin/python3.5m-config /usr/bin/python /usr/bin/python3.5 /usr/bin/python3.5m /usr/bin/python2.7 /usr/bin/python3.5-config /usr/lib/python3.5 /usr/lib/python2.7 /etc/python /etc/python3.5 /etc/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python2.7 /usr/include/python3.5 /usr/include/python3.5m /usr/share/python /usr/share/man/man1/python.1.gz 
  9. maoli@ubuntu:~$ which ps 
  10. /bin/ps 
  11. maoli@ubuntu:~$ which python 
  12. /usr/bin/python 

清除屏幕上显示的内容

清除屏幕上显示的内容 - 「clear」。

查看帮助文档查看帮助文档 - 「man」 / 「info」 / 「help」 / 「apropos」。


  1. maoli@ubuntu:~$ ps --help 
  2.  
  3. Usage: 
  4. ps [options] 
  5.  
  6. Try 'ps --help <simple|list|output|threads|misc|all>' 
  7.  or 'ps --help <s|l|o|t|m|a>' 
  8. for additional help text. 
  9.  
  10. For more details see ps(1). 
  11. maoli@ubuntu:~$ man ps 
  12.  
  13. PS(1)                                User Commands                                PS(1) 
  14. NAME 
  15.        ps - report a snapshot of the current processes. 
  16. SYNOPSIS 
  17.        ps [options] 
  18. DESCRIPTION 
  19. ... 

查看系统和主机名

查看系统和主机名 - 「uname」 / 「hostname」。


  1. maoli@ubuntu:~$ uname 
  2. Linux 
  3. maoli@ubuntu:~$ hostname 
  4. ubuntu 

时间和日期

时间和日期 - 「date」 / 「cal」。


  1. maoli@ubuntu:~$ date 
  2. 2020年 05月 28日 星期四 08:13:25 CST 
  3. maoli@ubuntu:~$ cal 
  4.      五月 2020          
  5. 日 一 二 三 四 五 六   
  6.                1  2   
  7. 3  4  5  6  7  8  9   
  8. 10 11 12 13 14 15 16   
  9. 17 18 19 20 21 22 23   
  10. 24 25 26 27 28 29 30   
  11. 31                     
  12. maoli@ubuntu:~$ cal 5 2020 
  13.      五月 2020          
  14. 日 一 二 三 四 五 六   
  15.                1  2   
  16. 3  4  5  6  7  8  9   
  17. 10 11 12 13 14 15 16   
  18. 17 18 19 20 21 22 23   
  19. 24 25 26 27 28 29 30   
  20. 31         

重启和关机

重启和关机 - 「reboot」 / 「shutdown」。


  1. maoli@ubuntu:~$  shutdown -h +5   #五分钟关机 
  2. Shutdown scheduled for 四 2020-05-28 08:19:24 CST, use 'shutdown -c' to cancel. [root ~]#  
  3. maoli@ubuntu:~$ shutdown -c 
  4. maoli@ubuntu:~$ shutdown -r 12:00 
  5. Shutdown scheduled for 四 2020-05-28 12:00:00 CST, use 'shutdown -c' to cancel 
  6. maoli@ubuntu:~$ shutdown -c 

说明:在执行shutdown命令时会向登录系统的用户发出警告,可以在命令后面跟上警告消息来替换默认的警告消息,也可以在-h参数后通过now来表示立刻关机。

退出登录

退出登录 - 「exit」 / 「logout」。

查看历史命令

查看历史命令 - 「history」。


  1. maoli@ubuntu:~$ history 
  2. ... 
  3.   625  date 
  4.   626  cal 
  5.   627  cal 5 2020 
  6.   628  shutdown -c 
  7.   629  shutdown -r 12:00 
  8.   630  shutdown -c 
  9.   631  history 
  10.  
  11. maoli@ubuntu:~$ !631   

说明:查看到历史命令之后,可以用!历史命令编号来重新执行该命令;通过history -c可以清除历史命令。

实用程序

文件和文件夹操作

创建/删除空目录

创建/删除空目录 - 「mkdir」 / 「rmdir」。


  1. [root ~]# mkdir runsen 
  2. [root ~]# mkdir -p&nbsp;abc/runsen 
  3. [root ~]# rmdir runsen 

创建/删除文件

创建/删除文件 - 「touch」 / 「rm」。


  1. maoli@ubuntu:~$ touch readme.txt 
  2. maoli@ubuntu:~$  rm readme.txt  
  3. rm: remove regular empty file ‘readme.txt ’? y 
  4. [root ~]# rm -rf ;xyz&amp;amp;nbsp;

touch命令用于创建空白文件或修改文件时间。在Linux系统中一个文件有三种时间:

  • 更改内容的时间 - mtime。
  • 更改权限的时间 - ctime。
  • 最后访问时间 - atime。

rm的几个重要参数:

  • -i:交互式删除,每个删除项都会进行询问。
  • -r:删除目录并递归的删除目录中的文件和目录。
  • -f:强制删除,忽略不存在的文件,没有任何提示。

切换和查看当前工作目录

切换和查看当前工作目录 - 「cd」 / 「pwd」。

说明:cd命令后面可以跟相对路径(以当前路径作为参照)或绝对路径(以/开头)来切换到指定的目录,也可以用cd ..来返回上一级目录。返回到上上一级目录应该给cd ../../命令。

查看目录内容

查看目录内容 - 「ls」。

  • -l:以长格式查看文件和目录。
  • -a:显示以点开头的文件和目录(隐藏文件)。
  • -R:遇到目录要进行递归展开(继续列出目录下面的文件和目录)。
  • -d:只列出目录,不列出其他内容。
  • -S / -t:按大小/时间排序。

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读