Linux基本命令

Linux基本命令

Locam Lv1

文件目录类命令

pwd(print working directory ) 打印当前工作目录的绝对路径

  • 基本语法

    • pwd (功能描述:显示当前工作目录的绝对路径)
  • 案例实操

    • 显示当前工作目录的绝对路径
      1
      2
      [root@centos100 ~]# pwd
      /root

ls(list) 列出目录内容

  • 基本语法

    • ls [选项] [目录或是文件]
  • 选项说明

    选项 功能
    -a 全部的文件,连同隐藏档( 开头为 . 的文件) 一起列出来(常用)
    -l 长数据串列出,包含文件的属性与权限等等数据;(常用)
  • 显示说明

    每行列出的信息依次是: 文件类型与权限 链接数 文件属主 文件属组 文件大小用byte来表示 建立或最近修改的时间 名字 

  • 实操案例

    • 查看当前目录的所有内容信息
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      [root@centos100 ~]# ls -al
      总用量 80
      dr-xr-x---. 15 root root 4096 11月 7 19:08 .
      dr-xr-xr-x. 17 root root 224 11月 3 20:18 ..
      -rw-------. 1 root root 1533 11月 3 20:19 anaconda-ks.cfg
      -rw-------. 1 root root 1996 11月 7 18:54 .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 176 12月 29 2013 .bashrc
      drwx------. 15 root root 4096 11月 6 14:03 .cache
      drwxr-xr-x. 16 root root 4096 11月 6 17:00 .config
      -rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
      drwx------. 3 root root 25 11月 3 20:25 .dbus
      -rw-------. 1 root root 16 11月 3 20:28 .esd_auth
      -rw-------. 1 root root 1550 11月 7 19:04 .ICEauthority
      -rw-r--r--. 1 root root 1581 11月 3 20:25 initial-setup-ks.cfg
      drwx------. 3 root root 19 11月 3 20:28 .local
      drwx------. 5 root root 66 11月 3 20:29 .mozilla
      -rw-r--r--. 1 root root 6939 11月 6 15:35 smartd1.conf
      -rw-r--r--. 1 root root 6792 11月 6 15:48 smartd.conf
      -rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
      -rw-------. 1 root root 4518 11月 7 19:08 .viminfo
      -rw-------. 1 root root 165 11月 7 18:55 .Xauthority

cd(Change Directory)切换路径

  • 基本语法

    • cd [参数]
  • 参数说明

    参数 功能
    cd 绝对路径 切换路径
    cd 相对路径 切换路径
    cd ~或者cd 回到自己的家目录
    cd - 回到上一次所在目录
    cd .. 回到当前目录的上一级目录
    cd -P 跳转到实际物理路径,而非快捷方式路径
    cd / 回到系统根目录
  • 实操案例

    • 使用绝对路径切换到root目录
      1
      [root@centos100 ~]# cd /root/
    • 使用相对路径切换到“公共的”目录
      1
      [root@centos100 ~]# cd 公共的/
    • 表示回到自己的家目录,亦即是 /root 这个目录
      1
      [root@centos100 公共的]# cd ~
    • cd- 回到上一次所在目录
      1
      [root@centos100 ~]# cd -
    • 表示回到当前目录的上一级目录,亦即是 “/root/公共的”的上一级目录的意思;
      1
      [root@centos100 公共的]# cd ..

mkdir(Make directory) 建立目录

  • 基本语法

    • mkdir [选项] 要创建的目录
  • 选项说明

    选项 功能
    -p 创建多层目录
  • 实操案例

    • 创建一个目录
      1
      2
      3
      [root@centos100 ~]# mkdir xiyou

      [root@centos100 ~]# mkdir xiyou/yaojing
    • 创建一个多级目录
      1
      [root@centos100 ~]# mkdir -p xiyou/qujing/meihouwang

rmdir(Remove directory) 删除目录

  • 基本语法

    • rmdir 要删除的空目录
  • 实操案例

    • 删除一个空的文件夹
      1
      [root@centos100 ~]# rmdir xiyou/qujing/meihouwang

 touch 创建空文件

  • 基本语法

    • touch 文件名称
  • 实操案例

    1
    [root@centos100 ~]# touch xiyou/qujing/sunwukong.txt

cp 复制文件或目录

  • 基本语法

    • cp [选项] source dest (功能描述:复制source文件到dest)
  • 选项说明

    选项 功能
    -r 递归复制整个文件夹
  • 参数说明

    参数 功能
    source 源文件
    dest 目标文件
  • 实操案例

    • 复制文件
      1
      [root@centos100 ~]# cp xiyou/qujing/suwukong.txt xiyou/mingjie/
    • 递归复制整个文件夹
      1
      [root@centos100 ~]# cp -r a/b/ ./
  • 注意事项

     强制覆盖不提示的方法:\cp

rm移除文件或者目录

  • 基本语法

    • rm [选项] deleteFile
  • 选项说明

    选项 功能
    -r 递归删除目录所有内容
    -f 强制删除,不提示用户进行确认
    -v 显示指令的详细执行过程
  • 实操案例

    • 删除目录中的内容
      1
      [root@centos100 ~]# rm xiyou/mingjie/sunwukong.txt
    • 递归删除目录中所有内容
      1
      [root@centos100 ~]# rm -rf  qujing/

mv移动文件与目录或重命名

  • 基本语法
    • 重命名 
      1
      mv oldNameFile newNameFile
    • 移动文件 
      1
      mv /temp/movefile /targetFolder
  • 实操案例
    • 重命名
      1
      [root@centos100 ~]# mv xiyou/qujing/suwukong.txt xiyou/qujing/houge.txt
    • 移动文件
      1
      [root@centos100 ~]# mv xiyou/qujing/houge.txt ./

cat查看文件内容

  • 基本语法

    • cat [选项] 文件 查看文件内容,从第一行开始显示
  • 选项说明

    选项 功能描述
    - n 显示所有行的行号,包括空行
  • 经验技巧

    1
    一般查看比较小的文件,一屏幕能显示全的
  • 实操案例

    • 查看文件内容并显示行号
      1
      [atguigu@centos100 ~]$ cat -n houge.txt 

more 文件分屏查看器

  • 基本语法
    1
    2
    more 要查看的文件

    1
    more指令是一个基于VI编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容。more指令中内置了若干快捷键,详见操作说明。
  • 操作说明
    操作 功能说明
    空白键 (space) 代表向下翻一页;
    Enter 代表向下翻『一行』;
    q 代表立刻离开 more ,不再显示该文件内容。
    Ctrl+F 向下滚动一屏
    Ctrl+B 返回上一屏
    = 输出当前行的行号
    :f 输出文件名和当前行的行号
  • 实操案例
    • (1)采用more查看文件
      1
      [root@centos100 ~]# more smartd.conf

less 分屏显示文件内容

  • 基本语法
    1
    2
    3
    less指令用来分屏查看文件内容,它的功能与more指令类似,但是比more指令更加强大,支持各种显示终端。less指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示需要加载内容,对于显示大型文件具有较高的效率。

    less 要查看的文件
  • 操作说明
    操作 功能说明
    空白键 向下翻动一页;
    [pagedown] 向下翻动一页
    [pageup] 向上翻动一页;
    /字串 向下搜寻『字串』的功能;n:向下查找;N:向上查找;
    ?字串 向上搜寻『字串』的功能;n:向上查找;N:向下查找;
    q 离开 less 这个程序;
  • 实操案例
    • (1)采用less查看文件
      1
      [root@centos100 ~]# less smartd.conf

head显示文件头部内容

  • 基本语法
    1
    2
    3
    4
    head用于显示文件的开头部分内容,默认情况下head指令显示文件的前10行内容。

    head 文件 (功能描述:查看文件头10行内容)
    head -n 5 文件 (功能描述:查看文件头5行内容,5可以是任意行数)
  • 选项说明
    选项 功能
    -n <行数> 指定显示头部内容的行数
  • 实操案例
    • (1)查看文件的头2行
      1
      [root@centos100 ~]# head -n 2 smartd.conf

tail 输出文件尾部内容

  • 基本语法
    1
    2
    3
    4
    tail用于输出文件中尾部的内容,默认情况下tail指令显示文件的后10行内容。
    (1)tail 文件 (功能描述:查看文件后10行内容)
    (2)tail -n 5 文件 (功能描述:查看文件后5行内容,5可以是任意行数)
    (3)tail -f 文件 (功能描述:实时追踪该文档的所有更新)
  • 选项说明
    选项 功能
    -n<行数> 输出文件尾部n行内容
    -f 显示文件最新追加的内容,监视文件变化
  • 实操案例
    • (1)查看文件头1行内容
      1
      [root@centos100 ~]# tail -n 1 smartd.conf 
    • (2)实时追踪该档的所有更新
      1
      [root@centos100 ~]# tail -f houge.txt

echo 打印信息

  • 基本语法
    1
    2
    3
    echo输出内容到控制台  System.out.println();

    echo [选项] [输出内容]
  • 选项说明
    选项 功能
    -e 支持反斜线控制的字符转换
    控制字符 作用
    —- ———–
    \\ 输出\本身
    \n 换行符
    \t 制表符,也就是Tab键
  • 实操案例
    • (1) 打印文字信息
      1
      2
      3
      4
      [atguigu@centos100 ~]$ echo "hello\tworld"
      hello\tworld
      [atguigu@centos100 ~]$ echo -e "hello\tworld"
      hello world
    • (2) 打印环境变量
      1
      [atguigu@centos100 ~]$ echo $PATH

> 覆盖和>>追加

  • 基本语法
    1
    2
    3
    4
    (1)ll >文件       (功能描述:列表的内容写入文件a.txt中(**覆盖写**))
    (2)ll >>文件 (功能描述:列表的内容**追加**到文件aa.txt的末尾)
    (3)cat 文件1 > 文件2 (功能描述:将文件1的内容覆盖到文件2)
    (4)echo “内容” >> 文件
  • 实操案例
    • (1)将ls查看信息写入到文件中
      1
      [root@centos100 ~]# ls -l>houge.txt
    • (2)将ls查看信息追加到文件中
      1
      [root@centos100 ~]# ls -l>>houge.txt
    • (3)采用echo将hello单词追加到文件中
      1
      [root@centos100 ~]# echo hello>>houge.txt

ln创建链接和软连接

  • 基本语法
    1
    2
    3
    链接表示目标资源的另外的访问方式,表示一种路径
    软链接也称为符号链接,类似于windows里的快捷方式,有自己的数据块,主要存放了链接其他文件的路径。
    ln [-s] [原文件或目录] [链接名] (功能描述:给原文件创建一个链接)
  • 选项说明
    选项 功能
    -s 创建的链接为 软连接
  • 经验技巧
    1
    2
    删除软链接: rm -rf 软链接名,而不是rm -rf 软链接名/
    查询:通过ll就可以查看,列表属性第1位是l,尾部会有位置指向。
  • 实操案例
    • (1)创建软连接
      1
      2
      3
      4
      [root@centos100 ~]# mv houge.txt xiyou/qujing/
      [root@centos100 ~]# ln -s /root/xiyou/qujing/houge.txt /root/houzi
      [root@centos100 ~]# ll
      lrwxrwxrwx. 1 root root 20 6月 17 12:56 houzi -> xiyou/qujing/houge.txt
    • (2)删除软连接
      1
      [root@centos100 ~]# rm -rf houzi
    • (3)进入软连接实际物理路径
      1
      2
      [root@centos100 ~]# ln -s xiyou/qujing/ ./qujing
      [root@centos100 ~]# cd -P qujing/

history查看历史命令

  • 基本语法
    1
    history                    (功能描述:查看已经执行过历史命令)
  • 实操案例
    • (1)查看已经执行过的历史命令
      1
      [root@centos100 test1]# history
    • (2) /root/.bash_history文件中也是历史命令
      1
      less /root/.bash_history
  • Title: Linux基本命令
  • Author: Locam
  • Created at : 2024-02-20 22:27:17
  • Updated at : 2024-02-26 21:51:22
  • Link: https://locam.fun/yongjuns-hub/2024/02/20/Linux基本命令/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
Linux基本命令