Linux 文件相关操作

1.创建目录文件:

   [root@rhcsa ~]# mkdir test

   [root@rhcsa ~]# cd test
   [root@rhcsa test]# touch text1.txt text2.txt
   [root@rhcsa test]# vim text1.txt

编辑模式(a,i,o):

  1 Welcome to my Linux.
  2 You are the best.
  3 This is my first file to create on linux

末行模式(Esc):

:set nu

:% s/my/your/g

:w new_test1.txt

建立连接:

[root@rhcsa test]# cp ./* /root/test2
[root@rhcsa test]# mv test1.txt test111.txt
[root@rhcsa test]# ln -s test111.txt test111_symb.txt
[root@rhcsa test]# ln test111.txt test111_hard.txt
[root@rhcsa test]# rm text1.txt
[root@rhcsa test]# cd ..


2.输出内容并保存:

[root@rhcsa ~]# echo "This is my first time to use pipe" | tee pipe_data.txt
 

3.文件内容浏览:
[root@rhcsa ~]# cat -n/etc/password
[root@rhcsa ~]# more -10/etc/password
[root@rhcsa ~]# less -10/etc/password
[root@rhcsa ~]# head -5 /etc/password
[root@rhcsa ~]# tail -5 /etc/password
 

4 文件的复制和移动

#cp复制源文件存在
#复制/root/q文件到/root/passwd
[root@rhcsa ~]# cp   /root/q  /root/passwd
#复制目录文件,将/root/dir2复制到已存在的/root/dir3目录里面
[root@rhcsa ~]# cp  -r   /root/dir2   /root/dir3
#复制/root/file2 /root/dir2 /root/passwd  文件到已存在的/dir1目录里面
[root@rhcsa ~]# cp -r  /root/file2 /root/dir2 /root/passwd  /dir1

#mv移动后源文件不存在
#将/root/q文件移动到/root/passwd3,q文件会消失,/root下面会多一个passwd3文件等同于重命名
[root@rhcsa ~]# mv  /root/q   /root/passwd3
#将/root/passwd3移动到已存在的/root/dir4目录
[root@rhcsa ~]# mv /root/passwd3 /root/dir4
#将/root/dir2 移动到已存在的/root/dir4目录里面
[root@rhcsa ~]# mv  /root/dir2 /root/dir4
[root@rhcsa0 ~]# mv  /root/file2 /root/dir2 /root/passwd  /dir5 

5. 查找文件: 

[root@rhcsa ~]# ll /root/ -d
dr-xr-x---. 15 root root 4096  8月  4 14:50 /root/
文件类型  文件的权限  文件的所属者  文件的所属组  文件的大小  文件的mtime  文件的名字
find  路径   -type  [f|d|l|b|c|p|s]
find  路径   -perm  777
find  路径   -user/-uid   用户名/用户的编号
find  路径   -group/-gid   组名/组编号
find  路径    -size      -2k/2k/+2k
find  路径    -mtime/-atime/-ctime   -3/3/+3   
    mtime:如果修改了文件内容则该时间更新
    atime:如果访问过该文件则时间更新
    ctime:如果文件的元数据(文件的名字,文件的大小)变化则时间更新
find  路径   -name    文件名
find   路径   -iname   文件名
[root@rhcsa ~]# find /  -iname file

4.文件的打包和压缩

#压缩、解压:
    .zip          压缩指令:zip,unzip
    .gz            压缩指令:gzip,gunzip    
    .bz2        压缩指令:bzip2,bunzip2
    .xz            压缩指令:xz,unxz
    .lz            压缩指令:xz --format=lzma,xz --format=lzma --decompress
[root@rhcsa yasuo]# tree /yasuo/
/yasuo/
├── dir1
│   └── hostname
├── passwd
└── sshd
#zip的压缩和解压缩
[root@rhcsa yasuo]# zip /yasuo/passwd.zip  /yasuo/passwd
[root@rhcsa yasuo]# ll /yasuo/
总用量 12
drwxr-xr-x. 2 root root   22  8月  4 15:24 dir1
-rw-r--r--. 1 root root 2154  8月  4 15:24 passwd
-rw-r--r--. 1 root root 1050  8月  4 15:27 passwd.zip
[root@rhcsa yasuo]# unzip /yasuo/passwd.zip
Archive:  /yasuo/passwd.zip
  inflating: yasuo/passwd
[root@rhcsa yasuo]# ll  /yasuo/
总用量 20
drwxr-xr-x. 2 root root   22  8月  4 15:24 dir1
-rw-r--r--. 1 root root    5  8月  4 15:28 file
-rw-r--r--. 1 root root  175  8月  4 15:28 file.zip
-rw-r--r--. 1 root root 2154  8月  4 15:24 passwd
-rw-r--r--. 1 root root 1050  8月  4 15:27 passwd.zip
-rw-------. 1 root root 3667  8月  4 15:24 sshd
drwxr-xr-x. 2 root root   20  8月  4 15:45 yasuo
#gzip,bzip2,xz的压缩和解压缩
[root@rhcsa yasuo]# bzip2 /yasuo/file /yasuo/passwd
[root@rhcsa yasuo]# ll /yasuo/
总用量 12
drwxr-xr-x. 2 root root   22  8月  4 15:24 dir1
-rw-r--r--. 1 root root   42  8月  4 15:28 file.bz2
-rw-r--r--. 1 root root  922  8月  4 15:24 passwd.bz2
-rw-------. 1 root root 3667  8月  4 15:24 sshd
drwxr-xr-x. 2 root root   20  8月  4 15:45 yasuo
[root@rhcsa yasuo]# bzip2 -c /yasuo/sshd >  /yasuo/sshd.bz2
[root@rhcsa yasuo]# ll /yasuo/
总用量 16
drwxr-xr-x. 2 root root   22  8月  4 15:24 dir1
-rw-r--r--. 1 root root   42  8月  4 15:28 file.bz2
-rw-r--r--. 1 root root  922  8月  4 15:24 passwd.bz2
-rw-------. 1 root root 3667  8月  4 15:24 sshd
-rw-r--r--. 1 root root 1744  8月  4 15:53 sshd.bz2
drwxr-xr-x. 2 root root   20  8月  4 15:45 yasuo
[root@rhcsa yasuo]# bunzip2  /yasuo/passwd.bz2
[root@rhcsa yasuo]# ll /yasuo/
总用量 16
drwxr-xr-x. 2 root root   22  8月  4 15:24 dir1
-rw-r--r--. 1 root root    5  8月  4 15:28 file
-rw-r--r--. 1 root root 2154  8月  4 15:24 passwd
-rw-------. 1 root root 3667  8月  4 15:24 sshd
-rw-r--r--. 1 root root 1744  8月  4 15:53 sshd.bz2
[root@rhcsa yasuo]# bunzip2  -c /yasuo/sshd.bz2  > /yasuo/sshd1
[root@rhcsa yasuo]# ll /yasuo/

#tar打包压缩文件
[root@rhcsa yasuo]# tar -czf  dps.tar.gz /yasuo/dir1 /yasuo/passwd  /yasuo/sshd
[root@rhcsa yasuo]# tar -cjf  dps.tar.bz2 /yasuo/dir1 /yasuo/passwd  /yasuo/sshd
[root@rhcsa yasuo]# tar -cJf  dps.tar.xz /yasuo/dir1 /yasuo/passwd  /yasuo/sshd
tar: 从成员名中删除开头的“/”
tar: 从硬连接目标中删除开头的“/”
[root@rhcsa yasuo]# ll /yasuo/
总用量 16
drwxr-xr-x. 2 root root   22  8月  4 15:24 dir1
-rw-r--r--. 1 root root 2737  8月  4 16:00 dps.tar.bz2
-rw-r--r--. 1 root root 2707  8月  4 16:02 dps.tar.gz
-rw-r--r--. 1 root root 2154  8月  4 15:24 passwd
-rw-------. 1 root root 3667  8月  4 15:24 sshd
#将打包压缩文件解压解包
[root@rhcsa yasuo]# tar -xf  /yasuo/dps.tar.gz
[root@rhcsa yasuo]# ll /yasuo/
总用量 16
drwxr-xr-x. 2 root root   22  8月  4 15:24 dir1
-rw-r--r--. 1 root root 2737  8月  4 16:00 dps.tar.bz2
-rw-r--r--. 1 root root 2707  8月  4 16:02 dps.tar.gz
-rw-r--r--. 1 root root 2154  8月  4 15:24 passwd
-rw-------. 1 root root 3667  8月  4 15:24 sshd
drwxr-xr-x. 3 root root   44  8月  4 16:03 yasuo
[root@rhcsa yasuo]# tar -xf /yasuo/dps.tar.bz2
[root@rhcsa yasuo]# tar -xf /yasuo/dps.tar.xz
[root@rhcsa yasuo]# tar -xf  /yasuo/dps.tar.gz  -C /dir10
[root@rhcsa yasuo]# tree  /dir10
/dir10
└── yasuo
    ├── dir1
    │   └── hostname
    ├── passwd
    └── sshd
#查看打包压缩文件的文件结构
[root@rhcsa yasuo]# tar   -tf  /yasuo/dps.tar.gz
yasuo/dir1/
yasuo/dir1/hostname
yasuo/passwd
yasuo/sshd 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值