Linux常用管理命令(中)

6. touch 创建文件命令

touch 命令用于创建新的空白文件

  • 命令格式:touch [-选项] 文件名

#在当前路径创建空文件
[root@localhost ~]# touch hello
[root@localhost ~]# ls
​
#在当前路径同时创建多个文件
[root@localhost ~]# touch t1 t2 t3 t4
[root@localhost ~]# ls
​
#在指定路径同时创建多个文件
[root@localhost ~]# touch /opt/test1 /opt/test2 /opt/test3
[root@localhost ~]# ls /opt
rh  student  test1  test2  test3  xx
​
#如果存在同名目录时,无法创建
[root@localhost ~]# mkdir test
mkdir: 无法创建目录"test": 文件已存在
​
#如果存在同名文件时,touch命令没有提示,但原有文件不会被覆盖
[root@localhost ~]# touch t1
​
#对于目录而言,只有单个目录的时候,“/”可有可无
[root@localhost ~]# ls /opt/
rh  student  test1  test2  test3  xx
[root@localhost ~]# ls /opt
rh  student  test1  test2  test3  xx
​
#对于目录而言,查看目录下的内容时,必须要有“/”
[root@localhost ~]# ls /opt/xx
oo
​
#对于文件而言,后边绝对不能有“/”
[root@localhost ~]# ls /opt/test1
/opt/test1
[root@localhost ~]# ls /opt/test1/
ls: 无法访问/opt/test1/: 不是目录

7. cp 复制命令

cp(英文全拼:copy file)用于复制文件或目录,cp命令在复制时也可修改目录或文件名字

  • 命令格式:cp [-选项] 源文件或目录 目标目录

  • 常用选项:

    • -p 保留源文件属性不变(如:修改时间、归属关系、权限)

    • -r 复制目录(包含该目录下所有的子目录和文件)

  • 使用 . 配合cp命令执行复制(.永远表示当前路径)

#复制当前目录文件到/opt目录(相对路径方式复制)
[root@localhost ~]# cp t1 /opt/
[root@localhost ~]# ls /opt
rh  student  t1  test1  test2  test3  xx
​
#复制文件到/opt目录(绝对路径方式复制)
[root@localhost ~]# cp /root/t2 /opt
[root@localhost ~]# ls /opt
rh  student  t1  t2  test1  test2  test3  xx
​
#同时复制多个文件
[root@localhost ~]# cp t3 t4 /opt/
[root@localhost ~]# ls /opt
​
#创建目录
[root@localhost ~]# mkdir abc
​
#使用-r对目录执行复制
[root@localhost ~]# cp -r abc /opt
[root@localhost ~]# ls /opt
​
#同时复制多个目录
[root@localhost ~]# mkdir abc1 abc2 abc3
[root@localhost ~]# cp -r abc1 abc2 abc3 /opt
[root@localhost ~]# ls /opt
​
#复制hello文件到/opt并改名为hello.txt
[root@localhost ~]# cp hello /opt/hello.txt
[root@localhost ~]# ls /opt
​
#复制xxxx目录到/opt并改名xxoo
[root@localhost ~]# mkdir xxxx
[root@localhost ~]# cp -r xxxx /opt/xxoo
[root@localhost ~]# ls /opt
​
#使用“.”配合cp命令执行复制
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
​
[root@localhost network-scripts]# cp /root/t1 .
[root@localhost network-scripts]# ls
​
#操持属性不变复制文件
[root@localhost ~]# cp -p anaconda-ks.cfg /opt
cp:是否覆盖"/opt/anaconda-ks.cfg"? y                         
[root@localhost ~]# ls -l /opt/anaconda-ks.cfg 
-rw-------. 1 root root 1800 3月  13 17:34 /opt/anaconda-ks.cfg
​
#对比以上两个文件的详细属性信息(最后一次修改时间)
[root@localhost ~]# ls -l anaconda-ks.cfg 
-rw-------. 1 root root 1800 3月  13 17:34 anaconda-ks.cfg
​
#这两个操作代表什么意思?
[root@localhost ~]# cp -r xxxx /mnt/oooo  #拷贝并改名
[root@localhost ~]# cp -r xxxx /mnt/oooo  #拷贝

8. mv 剪切命令

mv(英文全拼:move file)用于移动文件或目录到其他位置,也可用于修改目录或文件名

  • 命令格式:mv [-选项] 源文件... 目标路径

  • 使用 . 配合mv命令使用

#移动当前路径hello文件到/mnt目录
[root@localhost ~]# mv hello /mnt
[root@localhost ~]# ls /mnt
hello  home  oooo  test
​
#同时移动多个文件
[root@localhost ~]# mv t1 t2 t3 t4 /mnt
[root@localhost ~]# ls /mnt
hello  home  oooo  student1  t1  t2  t3  t4  test
​
#移动/opt目录下文件到/mnt
root@localhost ~]# mv /opt/test1 /opt/test2 /opt/test3 /mnt/
[root@localhost ~]# ls /mnt
hello  home  oooo  student1  t1  t2  t3  t4  test  test1  test2  test3
​
#移动目录
[root@localhost ~]# mv student1 /mnt
[root@localhost ~]# ls /mnt
hello  home  oooo  student1  test
​
#移动文件并改名
[root@localhost ~]# mv hello.txt /media/hello
[root@localhost ~]# ls /media/
hello
​
#移动目录并改名
[root@localhost ~]# mv test /media/testxx
[root@localhost ~]# ls /media/
hello  testxx

9. cat 查看文件内容命令

cat (英文全拼:concatenate)命令用于查看文本文件内容

  • 命令格式:cat [选项] 文件名

  • 常用选项

    • -n #查看文件时以行号的形式显示文件内容

#查看文件内容
[root@localhost ~]# cat anaconda-ks.cfg 
[root@localhost ~]# cat initial-setup-ks.cfg 
[root@localhost ~]# cat /etc/hosts
​
#查看网卡文件内容,网卡配置文件
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32 
...
NAME="ens32"   //网卡名
UUID="16085f4c-f690-4058-b29e-d55c73387026"
DEVICE="ens32"
ONBOOT="yes"
IPADDR="192.168.0.50"     //网卡IP地址
PREFIX="24"               //子网掩码
GATEWAY="192.168.0.254"   //网关
DNS1="114.114.114.114"    //DNS
​
#查看当前系统用户基本信息文件内容
[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
​
#查看当前系统主机名配置文件内容
[root@localhost ~]# cat /etc/hostname
localhost.localdomain
​
#查看当前系统版本信息文件内容
[root@localhost ~]# cat /etc/centos-release 
CentOS Linux release 7.6.1810 (Core) 
​
#查看当前系统开机自动挂载配置文件内容
[root@localhost ~]# cat /etc/fstab
​
#查看系统组基本信息文件内容
[root@localhost ~]# cat /etc/group
​
#使用“-n”以行号形式显示文件内容
[root@localhost ~]# cat -n /etc/passwd
[root@localhost ~]# cat -n /etc/hostname
[root@localhost ~]# cat -n /etc/fstab
[root@localhost ~]# cat -n /etc/group
[root@localhost ~]# cat -n /etc/services 
​

10. less命令

less工具是对文件的输出进行分页显示的工具,常用于查看内容量较大的文件

  • 命令格式:less [-选项] 文件

  • 常用选项:

    • -N #以行号形式显示文件内容

  • 使用技巧:

    • 键盘上下键逐行查看

    • pgdn :向下翻一页(Fn + 下键)

    • pgup :向上翻一页(Fn + 上键)

  • /字符串 :搜索指定字符串(n从上向下搜索,N从下向上搜索)

    • G:直接跳转到文件最后一行

    • gg:直接跳转到文件行首

    • :1000 #精准的定位到某一行

    • q :退出

[root@localhost ~]# less -N /etc/services

猜你喜欢

转载自blog.csdn.net/qq_54100121/article/details/129330394