文件目录管理命令

文件目录管理操作

cd , ls

1、查看文件内容

​ cat/less/more/head/tail

1)、cat

[root@localhost ~]# cat /etc/fstab 

查看操作系统版本

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

查看内核版本
[root@localhost ~]# uname -r 
3.10.0-693.el7.x86_64

查看CPU型号
[root@localhost ~]# cat /proc/cpuinfo 
model name	: Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz

2)、less, more 用于分页查看文件

[root@localhost ~]# less /usr/share/dict/words 

​		回车:下一行, 空格: 下一页,   上下键 

​		q: 退出 

[root@localhost ~]# more /usr/share/dict/words 

3)、head, tail

head:  用于查看文件前n行内容,默认10行

[root@localhost ~]# head -n 3 /etc/passwd
[root@localhost ~]# head /etc/passwd



tail:用于查看文件的后n行内容,默认10行

[root@localhost ~]# tail -n 2 /etc/passwd
[root@localhost ~]# tail /etc/passwd


2、查看文件类型

 [root@localhost ~]# file /etc/fstab 
/etc/fstab: ASCII text
[root@localhost ~]# 
[root@localhost ~]# file /etc/passwd
/etc/passwd: ASCII text
[root@localhost ~]# 
[root@localhost ~]# which ls 
alias ls='ls --color=auto'
	/usr/bin/ls
[root@localhost ~]# 
[root@localhost ~]# file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=3d705971a4c4544545cb78fd890d27bf792af6d4, stripped

3、创建空白文件

# touch  文件名称
[root@localhost ~]# touch /tmp/file01
[root@localhost ~]# touch file01

大括号展开

[root@localhost ~]# touch /tmp/{1..100}.mp3 

[root@localhost ~]# touch /tmp/{1,3,5,7}.jpg

[root@localhost ~]# touch /tmp/{docker,kvm,openstack}

命令引用

$(命令)     引用命令产生的结果
[root@localhost ~]# date
Tue Dec 15 18:13:05 CST 2020
[root@localhost ~]# 
[root@localhost ~]# date +%F
2020-12-15
[root@localhost ~]# date +%T
18:15:36
[root@localhost ~]# date +%F-%T
2020-12-15-18:16:32
[root@localhost ~]# date +%Y
2020



[root@localhost ~]# touch /tmp/$(date +%T)
[root@localhost ~]# touch /tmp/$(date +%T)

4、创建目录

# mkdir [选项] 目录名称
[root@localhost ~]# mkdir /opt/python
[root@localhost ~]# mkdir -p /opt/linux/shell

5、删除文件、目录

# rm  [选项]  文件名称
	默认只能删除文件 
[root@localhost ~]# rm /tmp/1.jpg 

-r选项    用于删除目录 

[root@localhost ~]# rm -r /opt/python/

-f选项    强制删除 

[root@localhost ~]# rm -f /tmp/5.jpg 
[root@localhost ~]# rm -rf /tmp/ssh-461Lxw2DcOuA/

6、复制文件、目录

# cp  [选项]  源文件  目的文件
[root@localhost ~]# cp /etc/fstab /tmp/

[root@localhost ~]# cp /etc/passwd  /tmp/passwd_new

[root@localhost ~]# cp /etc/hosts   /tmp/hosts_$(date +%F)



-r选项    复制目录 

[root@localhost ~]# cp -r /opt/linux/    /tmp/

[root@localhost ~]# cp -r /opt/linux/*  /tmp/

7、移动文件、目录

# mv 源文件  目的文件 

文件重命名
    同目录下移动文件,相当于重命名 

[root@localhost ~]# ls /opt/linux/
1.sh  2.sh  3.sh  shell
 
[root@localhost ~]# mv /opt/linux/3.sh  /opt/linux/3_new.sh

[root@localhost ~]# ls /opt/linux/
1.sh  2.sh  3_new.sh  shell

8、统计文件大小

[root@localhost ~]# du -h /etc/fstab
4.0K	/etc/fstab

[root@localhost ~]# du -sh /etc/
40M	/etc/

[root@localhost ~]# du -ah /etc/  

9、统计文件字符数

[root@localhost ~]# wc /etc/hosts 
  2  10 158 /etc/hosts

[root@localhost ~]# wc -l /etc/hosts
2 /etc/hosts

[root@localhost ~]# wc -l /etc/passwd
43 /etc/passwd

10、管道符 |

​ 将上一条命令的结果转交给下一条命令处理

[root@localhost ~]# head -n 4 /etc/passwd | tail -n 1

[root@localhost ~]# du -ah /etc/ | less

[root@localhost ~]# du -ah /etc/ | wc -l

[root@localhost ~]# ifconfig ens33 | head -n 2 | tail -n 1

[root@localhost ~]# ifconfig ens33 | head -n 2 | tail -n 1 | awk '{print $2}'

11、文件打包压缩

1) gzip 压缩文件

.gz后缀

[root@localhost ~]# gzip /opt/testdir/1.txt 
[root@localhost ~]# ls /opt/testdir/
1.txt.gz  2.txt
[root@localhost ~]# file /opt/testdir/1.txt.gz 
/opt/testdir/1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Tue Dec 15 13:39:58 2020



[root@localhost ~]# gzip -d /opt/testdir/1.txt.gz 

2) bzip2 压缩文件

.bz2 

[root@localhost ~]# bzip2 /opt/testdir/2.txt 
[root@localhost ~]# ls /opt/testdir/
1.txt  2.txt.bz2
[root@localhost ~]# file /opt/testdir/2.txt.bz2 
/opt/testdir/2.txt.bz2: bzip2 compressed data, block size = 900k
[root@localhost ~]# 
[root@localhost ~]# bzip2 -d /opt/testdir/2.txt.bz2 

3) 文件打包/归档

1. 创建打包文件/归档文件
# tar cf 归档文件名称.tar   源文件
	c:  create  创建
	f:  file   用于指定归档文件名称
	

将/etc目录下所有文件打包存放到/tmp, 名称etc.tar 

[root@localhost ~]# tar cf /tmp/etc.tar /etc/ 
2、调用gzip进行压缩     .tar.gz 

# tar czf  归档文件名称    源文件 
	z: 调用gzip

[root@localhost ~]# tar czf /tmp/etc01.tar.gz   /etc/
 
[root@localhost ~]# file /tmp/etc01.tar.gz 
/tmp/etc01.tar.gz: gzip compressed data, from Unix, last modified: Tue Dec 15 13:59:08 2020
3、调用bzip2进行压缩      .tar.bz2 

# tar cjf  归档文件名称   源文件 
	j: 调用bzip2
	
[root@localhost ~]# tar cjf /tmp/etc02.tar.bz2  /etc/ 

[root@localhost ~]# file /tmp/etc02.tar.bz2 
/tmp/etc02.tar.bz2: bzip2 compressed data, block size = 900k
4、解包 

# tar xf  归档文件名称  [ -C 目录名称 ]
	x: 解包

	默认解压缩到当前目录, -C选项解压目录 
	
[root@localhost ~]# tar xf /tmp/etc01.tar.gz 

[root@localhost ~]# tar xf /tmp/etc02.tar.bz2 -C /opt/ 
注意事项:
    打包文件时,如果目录层次比较多,建议以相对路径的方式写源文件

[root@localhost ~]# cd /opt/linux/db/
[root@localhost db]# tar czf /tmp/data.tar.gz   MySQL/
posted @ 2023-03-25 10:27  nhxuan  阅读(37)  评论(0)    收藏  举报