Linux基础笔记:文件操作、man手册与vim使用

命令行管理文件

mkdir命令

1、mkdir 创建目录

 mkdir+目录名
 ls :查看目录名
 [yuxb@centos7 ~]$ mkdir lab
 [yuxb@centos7 ~]$ ls
 lab           time.log    公共  视频  文档  音乐
 html.zip                    operator.log  typescript  模板  图片  下载  桌面
 # 创建多个目录
 mkdir lab1 lab2
 ​
 # 加个-p 创建递归目录
 mkdir -p dir1/dir2/dir3

2. tree 查看目录结构(需安装)

 安装:
 yum install -y tree
 ​
 输出:
 [yuxb@centos7 ~]$ tree dir1
 dir1
 └── dir2
     └── dir3

cp命令

cp 复制文件或目录

 # 把etc目录里的hosts复制到此目录下面
 cp /etc/hosts .
 [yuxb@centos7 lab]$ cp /etc/hosts .
 [yuxb@centos7 lab]$ ls
 hosts
 ​
 # 把复制过来的文件重新命名
 cp /etc/hosts ./hosts-1
 [yuxb@centos7 lab]$ cp /etc/hosts ./hosts-1
 [yuxb@centos7 lab]$ ls
 hosts  hosts-1
 ​
 # 加个-i 询问是否覆盖
 [yuxb@centos7 lab]$ cp -i /etc/hosts ./hosts-1
 cp:是否覆盖"./hosts-1"?
 ​
 # -r 选项复制目录
 [yuxb@centos7 lab]$ cp -r /etc/yum .
 [yuxb@centos7 lab]$ ls
 host.conf  hosts  hosts-1  passwd  yum
 [yuxb@centos7 lab]$ ls yum
 fssnap.d  pluginconf.d  protected.d  vars  version-groups.conf
 ​
 # 当前目录下有同名称目录,则将源目录放到相同目录下面,而不是覆盖当前目录
 [yuxb@centos7 lab]$ cp -r /etc/yum ./yum
 [yuxb@centos7 lab]$ ls
 host.conf  hosts  hosts-1  passwd  yum  yum-1
 [yuxb@centos7 lab]$ ls yum
 fssnap.d  pluginconf.d  protected.d  vars  version-groups.conf  yum
 ​
 ​

mv命令

mv 移动或重命名

 # 移动文件到另一个目录里面
 [yuxb@centos7 lab]$ mv host.conf /home/yuxb/lab1
 [yuxb@centos7 lab]$ ls
 etc  home  hosts  hosts-1  passwd  yum  yum-1
 ​
 # 移动多个文件 目标位置只能是目录
 [yuxb@CentOS7 lab]$ mv passwd hosts /home/laoma/lab1/
 [yuxb@CentOS7 lab]$ ls
 etc  home  host.conf  yum  yum-1
 [yuxb@CentOS7 lab]$ ls ../lab1
 hosts  hosts-1  passwd
 ​
 ​

rm命令

rm删除文件/目录

 # 删除文件
 [yuxb@centos7 lab1]$ ls
 host.conf  hosts  hosts-1  passwd
 [yuxb@centos7 lab1]$ rm hosts
 [yuxb@centos7 lab1]$ rm hosts-1 passwd
 [yuxb@centos7 lab1]$ ls
 host.conf
 ​
 # 删除空目录
 [yuxb@centos7 lab1]$ mkdir dir01
 [yuxb@centos7 lab1]$ rmdir dir01
 [yuxb@centos7 lab1]$ ls
 host.conf
 ​
 # -r选项递归删除目录
 [yuxb@CentOS7 lab1]$ rm -r yum
 [yuxb@CentOS7 lab1]$ ls
 etc  home  yum-1
 ​
 ​

软连接

 # 创建软连接
 [yuxb@centos7 ~]$ ln -s /var/tmp/ mytmp
 [yuxb@centos7 ~]$ ls -l mytmp
 lrwxrwxrwx 1 yuxb yuxb 9 7月  21 10:51 mytmp -> /var/tmp/
 ​
 # 查看
 [yuxb@centos7 ~]$ ls mytmp/
 abrt
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-bolt.service-TIM4to
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-chronyd.service-zikR53
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-colord.service-vlWAsF
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-cups.service-fU59AV
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-fwupd.service-v5EQnS
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-nginx.service-JcAI5p
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-rtkit-daemon.service-VGR6uK
 [yuxb@centos7 ~]$ ls /var/tmp/
 abrt
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-bolt.service-TIM4to
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-chronyd.service-zikR53
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-colord.service-vlWAsF
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-cups.service-fU59AV
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-fwupd.service-v5EQnS
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-nginx.service-JcAI5p
 systemd-private-4430fcc0c6284ad590791554fd20a5fc-rtkit-daemon.service-VGR6uK
 ​

硬连接

 # 创建硬链接
 [yuxb@centos7 lab]$ cp /etc/hosts ./hosts-1
 [yuxb@centos7 lab]$ ln hosts-1 hosts-2
 [yuxb@centos7 lab]$ ls -l hosts-1 hosts-2
 -rw-r--r-- 2 yuxb yuxb 158 7月  21 10:54 hosts-1
 -rw-r--r-- 2 yuxb yuxb 158 7月  21 10:54 hosts-2
 ​
 # 两个文件的inode是相同的
 [yuxb@centos7 lab]$ ls -i1 hosts-1 hosts-2
 4759 hosts-1
 4759 hosts-2
 ​
 # 更新host-1的内容 host-2也会变动
 [yuxb@centos7 lab]$ cat hosts-1
 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
 [yuxb@centos7 lab]$ cat hosts-2
 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
 [yuxb@centos7 lab]$ echo hello world >> hosts-1
 [yuxb@centos7 lab]$ cat hosts-1
 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
 hello world
 [yuxb@centos7 lab]$ cat hosts-2
 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
 hello world
 ​
 ​

shell扩展匹配文件

* 匹配任意字符(0个或多个)

 可以匹配多个字符
 例如已经创建了touch file-1 file-2 file-a file-a1 file-a2  file-ab file-b
 可以输入ls file*查看或者ls fi*查看等等 

?匹配一个任意字符

 只能匹配单个字符
 在上面创建好的情况下
 想要查找file-ab、file-a1、file-a2输入ls file-??查看
 一个?一个字符位置

~ 表示当前用户主目录

 echo ~可以显示当前用户的家目录路径
 cd ~ 切换到主目录

[] 匹配字符集合或范围

 [abc]      # 匹配 'a' 或 'b'  'c'
 [0-9]      # 匹配任意一位数字
 [^a-z]     # 匹配非小写字母
 ​
 匹配 file1.txt 和 file2.txt
 ls file[12].txt
 匹配 fileA.txt(排除 1 和 2)
 ls file[^12].txt
 ​

{} 花括号扩展(生成多个字符串)

 1、生成字符串列表
 echo {a,b,c}
 输出:a b c
 ​
 echo file{1,2,3}.txt
 输出:file1.txt file2.txt file3.txt
 ​
 2、批量创建文件
 touch file{1..3}.txt
 批量删除文件
 rm file{1..3}.txt
 ​

Man手册

man 简介

man,manual简称。man使用不同section区分手册类别。

SECTION内容类型
1用户命令(可执行命令和shell程序)
2系统调用(从用户空间调用的内核例程)
3库函数(由程序库提供)
4特殊文件(如设备文件)
5文件格式(用于许多配置文件和结构)
6游戏(过去的有趣程序章节)
7惯例、标准和其他(协议、文件系统)
8系统管理和特权命令(维护任务)
9Linux内核API(内核调用)

man命令

man --help

查看 man 命令自身的帮助信息

 用法:man [选项] [命令名]
 ​
 常用选项:
   -k    按关键字搜索手册页(等价于 apropos)
   -f    显示命令属于哪一节(等价于 whatis)
   -a    顺序显示所有匹配的手册页
   -w    显示匹配手册页的路径但不打开
   -c    强制使用终端方式显示
   -P    指定使用哪个程序显示(如 -P "less -X")
 ​
   --help  显示帮助信息
   --version 显示版本信息

man ls

查看 ls 命令的详细说明,包括作用、语法、参数、示例等

man cp

查看 cp 的手册页。

常用操作快捷键(在 man 页面中)

键盘操作说明
↑ ↓PageUp / PageDown上下滚动
q退出
/关键词搜索关键词
n下一个匹配结果
h显示帮助

批量修改用户密码

举例:

 echo laoma:redhat >> user.txt
 echo laowang:redhat >> user.txt
 cat user.txt
 ​
 useradd laoma
 useradd laowang
 ​
 cat user.txt  | chpasswd 

which 和 whereis 命令

which 命令

查找并显示 某个命令的可执行文件路径

 which ls
 输出:/bin/ls
 ​
 表示系统执行 ls 命令时,其实运行的是 /bin/ls 这个程序。

whereis 命令

查找命令的 可执行文件、源代码路径和 man 手册路径

 whereis ls
 输出:ls: /bin/ls /usr/share/man/man1/ls.1.gz
 ​

vim 编辑器

vim 编辑器介绍

每一个系统管理员都有一个偏爱的文本编辑器。有的管理员偏爱gedit,有的偏爱nano,甚至有的偏爱emacs。即使已经有偏爱的编辑器,了解vim还是有必要的。因为vim可以安装在任何系统。

vim 模式

  • command 模式:该模式下键盘中的字母有特殊含义。例如G,跳转到最后一行;gg,跳转到第一行。

  • edit 模式:在command 模式下按i,进入该模式。在该模式可以输入内容。在该模式下按Esc返回command模式。

  • extended command 模式:在command 模式下按:,进入该模式。在该模式下,可以保存文件(:w),退出编辑(:q),强制退出编辑(:q!),报错并退出(:wq)。

  • visual edit 模式:在command 模式下按 v(单个字符选中)、V(整行选中)或者ctrl+v(按矩形选中),进入该模式。在该模式下,用于选中文件,随后可以复制或删除选中的内容。

    [yuxb@centos7 ~]$ cp /etc/profile . [yuxb@centos7 ~]$ vim profile [yuxb@centos7 ~]$ mv profile profile.sh

命令行模式快捷键

1、模式切换
快捷键功能说明
i进入插入模式(Insert)
a在当前字符后插入
I在行首插入
A在行末插入
v进入可视模式(Visual)
:进入底行命令模式(Ex 模式)
Esc返回命令模式(Command)
2、光标移动
快捷键功能说明
h / l左 / 右移动一个字符
j / k下 / 上移动一行
w跳到下一个单词开头
b跳到前一个单词开头
0跳到行首
^跳到行首第一个非空字符
$跳到行尾
G跳到最后一行
gg跳到第一行
nG:n跳到第 n
3、复制 / 删除 / 粘贴
快捷键功能说明
yy复制当前行
nyy复制 n
dd删除当前行(剪切)
ndd删除 n
x删除当前字符
p在光标后粘贴
P在光标前粘贴
4、撤销 / 重做
快捷键功能说明
u撤销上一步操作
U撤销当前行的所有更改
Ctrl + r重做撤销的操作
5、查找 / 替换
快捷键功能说明
/关键字向下查找
?关键字向上查找
n查找下一个匹配
N查找上一个匹配
:%s/旧/新/g全文替换旧为新
:n,m s/旧/新/g替换第 n 到 m 行中的内容
6、保存 / 退出
快捷键功能说明
:w保存文件
:q退出(未保存时会报错)
:wq保存并退出
:q!强制退出不保存
:w filename另存为文件名
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值