首页 > 系统相关 >Linux文件目录管理操作

Linux文件目录管理操作

时间:2023-02-06 20:44:06浏览次数:35  
标签:tmp opt 文件目录 tar etc Linux 操作 root localhost

文件目录管理操作

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 ~]# file /etc/passwd
/etc/passwd: ASCII text

[root@localhost ~]# which ls 
alias ls='ls --color=auto'
	/usr/bin/ls

[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 ~]# 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 ~]# 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/

标签:tmp,opt,文件目录,tar,etc,Linux,操作,root,localhost
From: https://www.cnblogs.com/nhxuan/p/17096653.html

相关文章

  • json串的使用与操作
    在controller中从后台数据库取出的json信息,怎样获取和怎么遍历?1.param中的信息用用result[0]还是result?想了很久,后来发现是你付处理信息数输出一个对象还是对个对象的问......
  • Git操作—— git add --all、git add.、git add * 的区别(转)
    原文:https://blog.csdn.net/W664160450/article/details/123715481?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-12......
  • Linux下磁盘扩容
    我们使用虚拟机时,不时会遇到空间不够的问题。虽然虚拟机扩容比较方便,但扩容后并不能直接使用,仍然需要在系统中调整,在windows中可以使用diskgenius等工具快速完成这个步骤,......
  • 在IIS上同站点部署多个程序操作步骤
    1、打开IIS管理器;  2、右击选中“网站”,选择“添加网站”;输入网站名称、路径、IP地址等信息;  3、确定后并访问网站;部署后可能会因为框架不支持导致报错,比如.NET......
  • 【Appium】python利用Template生成对象模板_appium_元素定位/操作
    UI自动化中用PageObject设计模式就会发现page元素定位代码基本重复,复制黏贴,修改,所以就想到运用模板方式,批量生成page,同理也能批量生成handle。有模板,利用配置文件ini获取......
  • root用户远程连接RocklyLinux9
    由于RocklyLinux9默认是拒绝Root用户22端口远程访问,所以需要进行调整1、登录服务器2、进入目录vi/etc/ssh/sshd_config3、修改PermitRootLoginprohibit-password为P......
  • Redis常用命令之操作List类型
    场景Centos中Redis的下载编译与安装(超详细):霸道的程序猿获取编程相关电子书、教程推送与免费下载。实现List类型是一个链表结构的集合,其主要功能有push、pop、获取元素等......
  • Redis常用命令之操作Set(集合)
    号霸道的程序猿获取编程相关电子书、教程推送与免费下载。实现set集合是string类型的无序集合,set是通过hashtable实现的,对集合我们可以取交集、并集、差集SADD命令语法SAD......
  • linux平台makefile文件的编写基础篇
    目的:基本掌握了make的用法,能在Linux系统上编程。环境:Linux系统,或者有一台Linux服务器,通过终端连接。一句话:有Linux编译环境。准备:准备三个文件:fil......
  • Android 总结4种线程中操作UI界面的方法
    我们经常会在后台线程中去做一些耗时的操作,比如去网络取数据。但是当数据取回来,需要显示到页面上的时候,会遇到一些小麻烦,因为我们都知道,android的UI页......