Linux常用命令的介绍和使用
这篇文章主要为大家分享Linux常用命令的介绍和使用。文中还介绍了linux使用命令对目录和文件的基本操作,希望大家通过这篇文章能有所收获。
一、Linux命令基础
1、Linux命令的通用格式
命令字 [选项] [参数]
命令字:命令字是唯一的,严格区分大小写;
选项:用于调节命令的具体功能,可以有一个或者多个选项,选项分为短格式和长格式,"-"表示短格式,例如"ls -a";"--"表示长格式,例如"ls --help";
- 参数:参数是命令处理的对象,例如文件、目录名等;
2、Linux命令作用
用于实现某一类功能的指令或程序,命令的执行依赖于解释器程序(例如:/bin/bash)。
3、Linux命令的分类
1)内部命令
集成在bash中,安装Linux系统自带,不需要对应文件,执行速度快,属于shell解释器的一部分。
2)外部命令
独立于shell解释器之外的程序,安装程序或者服务生成命令,需要对应到系统文件中,执行速度慢。
4、编辑Linux命令行的辅助操作
1)Tab键
自动补全命令
[root@centos01 ~]# whi which while whiptail
2)反斜杠"\"
强制换行
[root@centos01 ~]# user\> add bob [root@centos01 ~]# tail -2 /etc/passwd test:x:1000:1000:test:/home/test:/bin/bash bob:x:1001:1001::/home/bob:/bin/bash
3)快捷键Ctrl+U
清空至行首
[root@centos01 ~]# useradd [root@centos01 ~]# eradd
4)快捷键Ctrl+K
清空至行尾
[root@centos01 ~]# useradd[root@centos01 ~]# use
5)快捷键Ctrl+L
清空屏幕内容
6)快捷键Ctrl+C
取消本次命令编辑
[root@centos01 ~]# tail -2 /etc/passwd^C[root@centos01 ~]#
5、获取命令帮助
1)内部命令help
查看Bash内部命令的帮助信息
[root@centos01 ~]# help pwdpwd: pwd [-LP] 打印当前工作目录的名字。 选项: -L 打印 $PWD 变量的值,如果它命名了当前的 工作目录 -P 打印当前的物理路径,不带有任何的符号链接 默认情况下,`pwd' 的行为和带 `-L' 选项一致 退出状态: 除非使用了无效选项或者当前目录不可读,否则 返回状态为0
2)命令的"--help"选项
适用于大多数外部命令。
[root@centos01 ~]# vim --helpVIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug 2 2017 00:45:39)用法: vim [参数] [文件 ..] 编辑指定的文件 或: vim [参数] - 从标准输入(stdin)读取文本 或: vim [参数] -t tag 编辑 tag 定义处的文件 或: vim [参数] -q [errorfile] 编辑第一个出错处的文件参数: -- 在这以后只有文件名 -v Vi 模式 (同 "vi") -e Ex 模式 (同 "ex") -E Improved Ex mode -s 安静(批处理)模式 (只能与 "ex" 一起使用) -d Diff 模式 (同 "vimdiff") -y 容易模式 (同 "evim",无模式) -R 只读模式 (同 "view") -Z 限制模式 (同 "rvim") -m 不可修改(写入文件) -M 文本不可修改 -b 二进制模式 -l Lisp 模式 -C 兼容传统的 Vi: 'compatible' -N 不完全兼容传统的 Vi: 'nocompatible' -V[N][fname] Be verbose [level N] [log messages to fname] -D 调试模式 -n 不使用交换文件,只使用内存 -r 列出交换文件并退出 -r (跟文件名) 恢复崩溃的会话 -L 同 -r -A 以 Arabic 模式启动 -H 以 Hebrew 模式启动 -F 以 Farsi 模式启动 -T 设定终端类型为 -u 使用 替代任何 .vimrc --noplugin 不加载 plugin 脚本 -P[N] 打开 N 个标签页 (默认值: 每个文件一个) -o[N] 打开 N 个窗口 (默认值: 每个文件一个) -O[N] 同 -o 但垂直分割 + 启动后跳到文件末尾 + 启动后跳到第 行 --cmd 加载任何 vimrc 文件前执行 -c 加载第一个文件后执行 -S 加载第一个文件后执行文件 -s 从文件 读入正常模式的命令 -w 将所有输入的命令追加到文件 -W 将所有输入的命令写入到文件 -x 编辑加密的文件 --startuptime Write startup timing messages to -i 使用 取代 .viminfo -h 或 --help 打印帮助(本信息)并退出 --version 打印版本信息并退出
3)使用man命令阅读手册页
使用"⬆"、"⬇"方向键滚动文本,使用page Up和page Down键翻页,按Q或q键退出阅读环境、按"/"键查找内容。
[root@centos01 ~]# man pwdPWD(1) FSF PWD(1)NAME pwd - 显示出当前/活动目录的名称总览 (SYNOPSIS) pwd [OPTION]描述 (DESCRIPTION) 显示出 完整的 当前 活动目录 名称. --help 显示 帮助 信息, 然后 退出 --version 显示 版本 信息, 然后 退出报告 BUGS 发现的 bug 寄往 .另见 (SEE ALSO) Pwd 的 完整文档 以 Texinfo 手册 的 形式 维护. 如果 正确 安装了 info 和 pwd 文件, 用 命令 info pwd 可以 访问 完整 的 手册.版权 (COPYRIGHT) Copyright © 1999 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FIT‐ NESS FOR A PARTICULAR PURPOSE.[中文版维护人] 徐明 [中文版最新更新] 2003/05/13《中国Linux论坛man手册页翻译计划》 http://cmpp.linuxforum.net
6、ls(list)命令详解
ls命令选项
-l:以长格式显示文件和目录的列表;
-a:显示所有子目录和文件的信息;
-A:与-a选项的作用基本类似,但有两个特殊隐藏目录不会显示,"."和"..";
-d:显示目录本身的属性,而不是显示目录中的内容;
-h:以更人性化的方式显示出目录或文件的大小,此选项需要结合-l选项一起使用;
-R:以递归的方式显示指定目录及其子目录中的所有内容;
- --color:显示颜色;
应用举例:
[root@centos01 ~]# ls -l /总用量 102428-rw-r--r-- 1 root root 104857600 1月 9 21:42 1.isodrwxr-xr-x 3 root root 18 1月 9 21:22 benetlrwxrwxrwx. 1 root root 7 10月 23 22:29 bin -> usr/bindr-xr-xr-x. 5 root root 4096 10月 23 22:37 bootdrwxr-xr-x 18 root root 3140 1月 9 21:00 dev[root@centos01 ~]# ls -ld / dr-xr-xr-x. 19 root root 269 1月 9 21:31 /[root@centos01 ~]# ls -ld /test/yun/benet/1.txt -rw-r--r-- 1 root root 0 1月 9 21:40 /test/yun/benet/1.txt[root@centos01 benet]# dd if=/dev/zero of=./1.iso bs=10M count=10 记录了10+0 的读入记录了10+0 的写出104857600字节(105 MB)已复制,1.13446 秒,92.4 MB/秒[root@centos01 benet]# ls -lh ./1.iso -rw-r--r-- 1 root root 100M 1月 9 21:43 ./1.iso[root@centos01 ~]# ls -lhR /test/ /test/:总用量 0drwxr-xr-x 3 root root 19 1月 9 21:31 yun/test/yun:总用量 0drwxr-xr-x 2 root root 84 1月 9 21:43 benet/test/yun/benet:总用量 100M-rw-r--r-- 1 root root 100M 1月 9 21:43 1.iso-rw-r--r-- 1 root root 0 1月 9 21:40 1.txt-rw-r--r-- 1 root root 0 1月 9 21:40 2.txt-rw-r--r-- 1 root root 0 1月 9 21:40 3.txt-rw-r--r-- 1 root root 0 1月 9 21:40 4.txt-rw-r--r-- 1 root root 0 1月 9 21:40 5.txt
7、du(disk usage)命令详解
用于统计指定目录(或)文件所占磁盘空间的大小,du命令常用的几个选项如下:
-a:统计磁盘空间占用时包括所有的文件,而不仅仅只统计目录;
-h:以更人性化的方式(默认以KB计数,但不显示单位)显示出统计结果;
- -s:只统计所占用空间总的(Summary)大小,而不是统计每个子目录,文件的大小;
应用举例:
[root@centos01 ~]# df -Th 文件系统 类型 容量 已用 可用 已用% 挂载点/dev/sda3 xfs 76G 3.8G 72G 5% /devtmpfs devtmpfs 474M 0 474M 0% /devtmpfs tmpfs 489M 0 489M 0% /dev/shmtmpfs tmpfs 489M 7.0M 482M 2% /runtmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup/dev/sda1 xfs 197M 136M 61M 70% /boottmpfs tmpfs 98M 0 98M 0% /run/user/0[root@centos01 ~]# du -sh /var/log/ 4.9M /var/log/[root@centos01 ~]# du -sh /test/ 100M /test/[root@centos01 ~]# du -ah /test/yun/benet/1.iso100M /test/yun/benet/1.iso
二、目录和文件的基本操作
1、touch命令
touch命令用于创建空文件,用于测试,当目标文件已存在时,将更新该文件的时间标记。
应用举例:
[root@centos01 ~]# touch /test/1.iso [root@centos01 ~]# cd /test/ [root@centos01 test]# ls 1.iso[root@centos01 ~]# touch /test/{1..10}.txt [root@centos01 ~]# [root@centos01 ~]# cd /test/[root@centos01 test]# ls 10.txt 1.iso 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
2、常用命令--mkdir(make directory)
mkdir命令用于创建新目录,语法格式如下:
mkdir [选项] 目录位置及名称...
- -p:-p选项表示一次性创建嵌套的多层目录;
应用举例:
[root@centos01 ~]# mkdir www [root@centos01 ~]# lsanaconda-ks.cfg initial-setup-ks.cfg www[root@centos01 ~]# mkdir -p /test/yun/benet [root@centos01 ~]# cd /test/yun/benet/[root@centos01 benet]# pwd/test/yun/benet
3、常用命令--cp(copy)
复制文件或目录,需要复制多个文件或目录时,目标位置必须是目录,而且目标目录必须以及存在。语法格式如下:
cp [选项]... 源文件或目录... 目标文件或目录...
cp常用的选项如下:
-f:覆盖目录同名文件或目录时不进行提醒,直接强制复制;
-i:覆盖目录同名文件或目录时提醒用户确认;
-p:复制时保持源文件的权限,属主及时间标记等属性不变;
- -r:复制目录时必须使用此选项,表示递归复制所有文件及子目录;
应用举例:
[root@centos01 ~]# cp -r benet/ www/ [root@centos01 ~]# cp -rpf /test/ ./yun/
4、常用命令--rm(remove)
rm命令用于删除指定的文件或目录,语法格式如下:
rm [选项] 要删除的文件或目录...
rm命令常用的选项如下:
-f:不提醒,直接强制删除;
-i:提醒用户确认;
- -r:递归删除整个目录树;
应用举例:
[root@centos01 test]# ls10.txt 1.iso 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt yun[root@centos01 test]# rm -rf yun [root@centos01 test]# rm -rf ./*.iso [root@centos01 test]# ls10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt[root@centos01 test]# rm -ri ./* rm:是否删除普通空文件 "./10.txt"?y rm:是否删除普通空文件 "./1.txt"?y............
rm -rf命令极度危险,慎重操作!!!
5、常用命令--mv(move)
mv命令用于将指定的文件或目录转移位置,如果目标位置与源位置相同,则相当于执行重命名操作。mv命令语法格式如下:
mv [选项]... 源文件或目录... 目标文件或目录
应用举例:
[root@centos01 ~]# mv 1.mp3 /benet/bdqn/accp/xsh/[root@centos01 xsh]# ls1.mp3[root@centos01 xsh]# mv 1.mp3 2.mp3 [root@centos01 xsh]# ls2.mp3
6、常用命令--which
which命令用来查找Linux命令程序所在的位置,注意:默认当找到第一个目标后不再继续查找;若在所有搜素路径中查找,加"-a"选项;无法查找内部命令所对应的程序文件,例如:which cd。语法格式如下:
which 命令|程序名
应用举例:
[root@centos01 ~]# which passwd /usr/bin/passwd[root@centos01 ~]# which -a groupadd /usr/sbin/groupadd
7、常用命令--find
find命令用来精细查找文件或目录。基本语法格式如下:
find [查找范围] [查找条件表达式]
find常用的查找条件如下:
-name:按名称查找;根据目标文件的名称进行查找,允许使用"*"及"?"通配符;
-size:按文件大小查找;一般使用"+"、"-"号设置超过或小于指定的大小作为查找条件。常用的容量单位包括kB(注意k是小写)、MB、GB;
-user:按文件属主查找;
- -type:按文件类型查找;类型指的是普通文件(f)、目录(d)、块设备文件(b)、字符设备文件(c)等。
同时使用多个查找条件时,各表达式之间可以使用逻辑运算符"-a"、"-o",分别表示而且(and)、或者(or)。
应用举例:
[root@centos01 ~]# find /test/yun/benet/ -name *.txt /test/yun/benet/1.txt/test/yun/benet/2.txt/test/yun/benet/3.txt/test/yun/benet/4.txt/test/yun/benet/5.txt[root@centos01 ~]# find /test/yun/benet/ -type d /test/yun/benet/[root@centos01 ~]# find /test/yun/benet/ -type f /test/yun/benet/1.txt/test/yun/benet/2.txt/test/yun/benet/3.txt/test/yun/benet/4.txt/test/yun/benet/5.txt/test/yun/benet/1.iso[root@centos01 ~]# find /test/yun/benet/ -size +100M /test/yun/benet/2.iso[root@centos01 ~]# find /test/yun/benet/ -size -100M /test/yun/benet//test/yun/benet/1.txt/test/yun/benet/2.txt/test/yun/benet/3.txt/test/yun/benet/4.txt/test/yun/benet/5.txt[root@centos01 ~]# find /test/yun/benet/ -size +100M -a -name *.iso /test/yun/benet/2.iso[root@centos01 ~]# find /test/yun/benet/ -size +100M -o -name *.iso /test/yun/benet/1.iso/test/yun/benet/2.iso
8、日期和日历的查看方式
显示日期的命令:
[root@centos01 ~]# date2020年 01月 10日 星期五 22:05:54 CST[root@centos01 ~]# date +%F2020-01-10[root@centos01 ~]# date +%Y/%m/%d2020/01/10[root@centos01 ~]# date +%H:%M22:07
显示日历的命令:
[root@centos01 ~]# cal 一月 2020 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30 31[root@centos01 ~]# cal 22 11 1999 十一月 1999 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728 29 30
关于Linux的常用命令就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。