Linux——文件目录管理操作

2 篇文章 0 订阅
2 篇文章 0 订阅

一、查看文件内容

1、查看文件内容

cat/less/more/head/tail		

1)cat

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

#
# /etc/fstab
# Created by anaconda on Thu Mar 21 07:01:01 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=84953f78-f1ba-4dbd-ac60-aaabc2e4cb9c /                       ext4    defaults        1 1

查看操作系统版本
[root@wtl ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

查看CPU型号
[root@wtl ~]# cat /proc/cpuinfo 
model name	: Intel(R) Core(TM) i5-2430M CPU @ 2.40GHz

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

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

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

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

​		q: 退出 

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

3)head,tail

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

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



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

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

2、查看文件类型

[root@wtl ~]# file /etc/fstab 
/etc/fstab: ASCII text
[root@wtl ~]# file /etc/passwd
/etc/passwd: ASCII text
[root@wtl ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@wtl ~]# 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]=ceaf496f3aec08afced234f4f36330d3d13a657b, stripped

3、创建空白文件

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

连续创建多个文件

[root@wtl ~]# touch {1..10}.txt
[root@wtl ~]# touch {2,4,6,8,}.jpg
[root@wtl ~]# touch {docker,kvm,openstack}

命令引用

$(命令)		引用命令产生的结果
[root@wtl ~]# date
Wed Jan 13 19:32:05 CST 2021
[root@wtl ~]# date +%F
2021-01-13
[root@wtl ~]# date +%T
19:32:22
[root@wtl ~]# date +%F-%T
2021-01-13-19:32:33
[root@wtl ~]# date +%Y
2021

[root@wtl ~]# touch /tmp/$(date +%T)
[root@wtl ~]# touch /tmp/$(date +%T)
[root@wtl ~]# ls /tmp/
19:33:30
19:33:38

4、创建目录

#mkdir [选项] 文件名称
[root@tianlong ~]# mkdir /opt/python
[root@tianlong ~]# mkdir -p /opt/linux/shell

5、删除文件

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

-r选项    用于删除目录 

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

-f选项    强制删除 

[root@wtl ~]# rm -f /tmp/5.jpg 

6、复制文件、目录

#cp [选项]  源文件  目的文件
[root@wtl ~]# cp /etc/fstab /tmp/
[root@wtl ~]# cp /etc/passwd  /tmp/passwd_new
[root@wtl ~]# cp /etc/hosts   /tmp/hosts_$(date +%F)
-r选项    复制目录 
[root@wtl~]# cp -r /opt/linux/    /tmp/
[root@wtl ~]# 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/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丶Seman

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值