【Linux学习】基本指令其二

前言

【Linux学习】基本指令其一
第一篇中介绍Linux中最基本的命令, 现在继续说明后续指令。

一、man指令

Linux的命令有很多参数,可以通过查看联机手册获取帮助。
man 命令用于查看 Linux 系统中的手册页,提供了关于各种命令和程序的详细信息。
基本语法如下:

man [选项] 命令

手册页中使用方向键进行滚动,按q退出。
比如: 使用man ls命令后可以看到最下一行的提示。

Manual page ls(1) line 1 (press h for help or q to quit) # 按q 或者 ctrl + c退出

简单示例:

-> man ls   # 查看ls指令的手册

常用选项:

  • -k:根据关键词搜索手册页。
  • -f:显示指定命令的简短描述。
  • -a:显示所有匹配的手册页,而不仅仅是第一个。
  • -c:清除缓存的手册页。

搜索与copy相关的命令。

  man -k copy # 凡是命令描述中带有copy的均会被显示打印出来。

显示指定命令的简单描述

➜  ~ man -f cp	# cp命令下文说明
cp (1)               - copy files and directories
cp (1p)              - copy files

man 后面可以跟自己

-> man man

在这里插入图片描述

Linux 手册页有8章内容:
你提到的分类是 Linux 手册页的不同部分,具体说明如下:

  1. 普通命令:指用户在命令行中可以执行的命令,如 lscp 等。
  2. 系统调用:如 openwrite 等,内核提供的功能,可以通过 C 语言调用,相关头文件通常在手册中有说明。
  3. 库函数:如 printffread 等,函数在标准库中定义,提供更高级的功能。
  4. 特殊文件:通常指 /dev 目录下的设备文件,表示设备的接口。
  5. 文件格式:描述特定文件格式的手册,如 passwd 文件,解释其各字段的含义。
  6. 游戏相关:特定于游戏的命令和功能,由各个游戏自己定义。
  7. 附件和变量:如 environ 这样的全局变量的说明。
  8. 系统管理命令:需要超级用户权限的命令,如 ifconfig,用于系统配置和管理。

通过 man 命令,你可以访问这些不同的手册部分

man num ... # 只在第num章节找

比如

man 2 open   # 查看系统调用
man 3 printf # 查看库函数
man 5 passwd # 查看文件格式
man 8 ifconfig # 查看系统管理命令

二、echo指令 & cat指令

功能: 用于输出字符串变量的值到标准输出(通常是终端)

➜  ~ echo hello,world # 输出到终端上
hello,world
➜  ~ echo "hello world" # 以字符串的形式输出到终端
hello world
➜  ~ name="David" # 创建一个变量
➜  ~ echo "hello,$name" # 格式化打印
hello,David

如果我们将字符串写入文件呢?

cat指令作用: 查看目标文件中的内容
-b 对非空输出行编号
-n 对输出的所有行编号
-s 不输出多行空行

➜  ~ mkdir Test # 创建一个Test目录
➜  ~ cd Test # 进入到该目录中
➜  Test ls # 初始为空目录
➜  Test echo 'hello,Linux'>test.txt # 将’hello,world‘写入文件test.txt中
➜  Test cat test.txt # 打印文件内容
hello,Linux # 结果
➜  Test ls # 再次查看目录信息
test.txt # 当前目录创建了一个新文件test.txt

我们键盘输入的内容被打印到文件中而不是显示器中,这样的操作叫输出重定向,上面第二个输出方式就叫做输出重定向, 它使得输出被保存在文件中。 重定向过程中,如果当前目录没有该文件,则创建该文件并将字符串写入。
如果已经存在呢?

➜  Test cat test.txt
hello,Linux
➜  Test echo 'hello,C!'>test.txt
➜  Test ls
test.txt
➜  Test cat test.txt
hello,C! # 原test.txt的内容被覆写了。

如果要采取追加的方式呢?

➜  Test echo 'hello,Linux'>>test.txt # >> 追加重定向
➜  Test cat -n test.txt # -n 是cat指令的选项, 对所有输出行进行编号
     1	hello,C!
     2	hello,Linux
  • 使用 > 进行输出重定向时,如果文件已存在,内容将被覆盖。
  • 如果希望将输出追加到现有文件中,可以使用 >>

三、cp指令

基本语法:cp [选项] 源文件/目录 目标文件/目录
功能: 复制文件或目录
说明: cp指令用于复制文件或目录。
若同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中。若同时指定多个文件或目录,而最后的目的地并非一个已存在的目录,则会出现错误信息。

同一目录下拷贝
同一目录下拷贝文件,若后者是一份新文件则注意不要重名。

➜  Test ls
test.txt
➜  Test cp test.txt test2.txt # 当前目录下拷贝同时创建一个新文件
➜  Test ls
test2.txt  test.txt
➜  Test cat test.txt
hello,C!
hello,Linux
➜  Test cat test2.txt # 拷贝文件内容相同
hello,C!
hello,Linux

如果试图拷贝一份重名文件

➜  Test cp test.txt test.txt
cp: ‘test.txt’ and ‘test.txt’ are the same file # 错误信息

已有文件则直接拷贝内容,无创建过程:

➜  Test echo > test2.txt # 利用重定向将test2.txt文件清空
➜  Test ls test2.txt
test2.txt
➜  Test cat test2.txt # 验证清空该文件了

➜  Test cp test.txt test2.txt # 拷贝内容到已有文件上
➜  Test cat test2.txt # 打印结果
hello,C!
hello,Linux

不同目录下拷贝文件

➜  Test mkdir dir1 # 当前目录下, 新建一个dir1目录
➜  Test cp test.txt dir1/test.txt # 拷贝一个文件在dir1下, 不同目录允许重名文件
➜  Test cat dir1/test.txt # 打印观察
hello,C!
hello,Linux

常用选项

-f 或 –force 强行复制文件或目录, 不论目的文件或目录是否已经存在
-i 或 –interactive 覆盖文件之前先询问用户, 防止覆盖一些重要文件。
-r 递归复制目录及其内容,包括所有文件和子目录。
-功能与 -r 相同,递归处理目录及其所有内容,通常可以互换使用

四、mv指令

mv 命令是 “move” 的缩写,用于移动文件或目录,以及重命名文件或目录。
它是 Linux 系统中常用的命令,特别适用于备份操作。
语法

mv [选项] 源文件或目录 目标文件或目录
  1. 重命名文件或目录
➜  Test ls
dir1  dir2  test2.txt  test.txt
➜  Test mv test2.txt test.c # 当前目录下的test2.txt进行重命名
➜  Test ls
dir1  dir2  test.c  test.txt
➜  Test mv dir2 dir # 修改目录名
➜  Test ls
dir  dir1  test.c  test.txt
  1. 移动文件或目录到新目录
➜  Test tree ./
./
├── dir
│   └── test.txt
├── dir1
│   └── test.txt
├── test.c
└── test.txt

2 directories, 4 files
➜  Test mv test.c ./dir # 将当前目录的test.c文件移动到dir目录下。
➜  Test tree ./
./
├── dir
│   ├── test.c
│   └── test.txt
├── dir1
│   └── test.txt
└── test.txt

2 directories, 4 files

注意到当前目录,dir目录,dir1目录下均有同名的test.txt文件。

  • -i:在覆盖现有文件之前询问用户确认。
    mv -i source.txt destination.txt
    
➜  Test mv -i test.txt ./dir/test.txt # 可以发现两者是重名文件
mv: overwrite ‘./dir/test.txt’? n # 询问是否覆写? 这里拒绝
➜  Test tree ./ # 打印应该与先前一样。
./
├── dir
│   ├── test.c
│   └── test.txt
├── dir1
│   └── test.txt
└── test.txt

2 directories, 4 files

移动时,并重命名。

➜  Test mv test.txt ./dir/test2.txt
➜  Test tree ./
./
├── dir
│   ├── test2.txt
│   ├── test.c
│   └── test.txt
└── dir1
    └── test.txt

2 directories, 4 files
  • -f:强制移动文件,覆盖目标文件而不询问。
 mv -f source.txt destination.txt
➜  Test tree ./
./
├── dir
│   ├── test2.txt
│   ├── test.c
│   └── test.txt
└── dir1
    └── test.txt

2 directories, 4 files
➜  Test mv -f ./dir1/test.txt ./dir/test.txt # 不询问直接覆写文件。
➜  Test tree ./
./
├── dir
│   ├── test2.txt
│   ├── test.c
│   └── test.txt
└── dir1

2 directories, 3 files
  • -u:仅在源文件比目标文件新时移动。
    mv -u source.txt destination.txt
    

五、which指令

功能:查找指令所在的位置

while 指令

示例:

➜  Test which echo
/usr/bin/echo # echo指令路径, 下面同理
➜  Test which cat
/usr/bin/cat
➜  Test which cp
/usr/bin/cp
➜  Test which mv
/usr/bin/mv

六、clear指令

clear 指令用于清除终端屏幕上的内容,使其变得干净,通常用于提升可读性。执行 clear 命令后,光标会移动到屏幕的左上角。

clear

七、alias

alias是用于创建命令的别名。

➜  Test alias one="ls -l -a"
➜  Test ls -l -a
total 16
drwxrwxr-x  4 LiuGuanYi LiuGuanYi 4096 Oct  1 10:40 .
drwx------ 12 LiuGuanYi LiuGuanYi 4096 Oct  1 10:48 ..
drwxrwxr-x  2 LiuGuanYi LiuGuanYi 4096 Oct  1 10:42 dir
drwxrwxr-x  2 LiuGuanYi LiuGuanYi 4096 Oct  1 10:42 dir1
➜  Test one # 效果等价
total 16
drwxrwxr-x  4 LiuGuanYi LiuGuanYi 4096 Oct  1 10:40 .
drwx------ 12 LiuGuanYi LiuGuanYi 4096 Oct  1 10:48 ..
drwxrwxr-x  2 LiuGuanYi LiuGuanYi 4096 Oct  1 10:42 dir
drwxrwxr-x  2 LiuGuanYi LiuGuanYi 4096 Oct  1 10:42 dir1
alias # 查看别名的指令。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值