linux文件和目录管理

处理目录和文件的几个常用命令

  • ls:展示目录下的内容
  • cd: 切换到某个目录下
  • pwd:显示目前的目录
  • mkdir:创建一个新的目录
  • rmdir:删除一个空的目录
  • cp:复制文件或者目录
  • rm:移除文件或者目录

1 ls命令

主要分为以下几类

  • 文件显示范围
  • 文件信息显示
  • 文件的排序命令
  • 递归查询文件

1.1 文件显示范围

-a 列出所有的档案和目录项目,包括.和..以及隐藏文件和目录

[root@snrsdevapp02 migration]# ls -a 
 .  ..  old_shard_info.cfg  shard_info.cfg  shard_set_info.txt  target_shard_info.cfg

   -A 显示除了隐藏文件和..及.之外的文件

[root@snrsdevapp02 migration]# ls -A
old_shard_info.cfg  shard_info.cfg  shard_set_info.txt  target_shard_info.cfg
  • 文件信息显示
    -C 多列显示输出结果,为默认显示
    -m 以逗号分隔显示
    -l 单行显示一个文件,并列出文件的详细信息
-c 形式显示
[root@snrsdevapp02 zedis]# ls -C
6480.conf      data      fusion_evaluate.sh  redis-benchmark  redis-check-dump  redis.conf       redis.conf-6579  redis-sentinel  zedis-1.1.1.tgz  zsh
check_load.sh  dump.rdb  quit                redis-check-aof  redis-cli         redis.conf-6479  redis.conf-6679  redis-server    zhuangerwei
[root@snrsdevapp02 zedis]# ls
6480.conf      data      fusion_evaluate.sh  redis-benchmark  redis-check-dump  redis.conf       redis.conf-6579  redis-sentinel  zedis-1.1.1.tgz  zsh
check_load.sh  dump.rdb  quit                redis-check-aof  redis-cli         redis.conf-6479  redis.conf-6679  redis-server    zhuangerwei
-m形式显示
[root@snrsdevapp02 zedis]# ls -m
6480.conf, check_load.sh, data, dump.rdb, fusion_evaluate.sh, quit, redis-benchmark, redis-check-aof, redis-check-dump, redis-cli, redis.conf, redis.conf-6479, redis.conf-6579, redis.conf-6679,
redis-sentinel, redis-server, zedis-1.1.1.tgz, zhuangerwei, zsh
-l形式显示
[root@snrsdevapp02 zedis]# ls -l
total 8877248
-rwxr-xr-x 1 root root      36344 Dec 26 10:29 6480.conf
-rwxrwxrwx 1 root root       3072 Apr  2 20:04 check_load.sh
drwxr-xr-x 2 root root       4096 May 14 20:08 data
-rw-r--r-- 1 root root 9061636146 Mar  6 19:50 dump.rdb
-rw-r--r-- 1 root root       7085 Apr 18 21:30 fusion_evaluate.sh
-rw-r--r-- 1 root root      21887 Dec 26 14:20 quit
-rwxr-xr-x 1 root root    4595960 Jan 22 10:54 redis-benchmark
-rwxr-xr-x 1 root root      22225 Dec 26 10:29 redis-check-aof
-rwxr-xr-x 1 root root      45443 Dec 26 10:29 redis-check-dump
-rwxr-xr-x 1 root root    4690205 Dec 26 10:29 redis-cli
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf
-rwxr-xr-x 1 root root      36337 Dec 26 14:34 redis.conf-6479
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf-6579
-rwxr-xr-x 1 root root      36331 Jan 10 19:15 redis.conf-6679
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-sentinel
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-server
-rwxr-xr-x 1 root root    6444889 Dec 26 10:29 zedis-1.1.1.tgz
-r-x------ 1 root root          0 May  9 17:36 zhuangerwei
drwxr-xr-x 5 root root       4096 Dec 26 10:29 zsh

在选择了文件显示范围和文件显示方式后,再设置文件显示的各种属性

1.2 文件的附属属性显示

   -F 在每个输出项后追加文件的类型标识符,具体含义:“*”表示具有可执行权限的普通文件,“/”表示目录,“@”表示符号链接,“|”表示命令管道FIFO,“=”表示sockets套接字
   -d 仅仅列出目录本身信息

[root@snrsdevapp02 zedis]# ls -ld
drwxrwxrwx 4 root root 4096 May 16 14:41 .
[root@snrsdevapp02 zedis]# ls -ld /opt/zedis/
drwxrwxrwx 4 root root 4096 May 16 14:41 /opt/zedis/

   -lh h必须配合-l使用,在单列显示的模式下,用人性化模式显示文件大小个各种信息(文件大小可读性更强)

[root@snrsdevapp02 zedis]# ls -lh
total 8.5G
-rwxr-xr-x 1 root root  36K Dec 26 10:29 6480.conf
-rwxrwxrwx 1 root root 3.0K Apr  2 20:04 check_load.sh
drwxr-xr-x 2 root root 4.0K May 14 20:08 data
-rw-r--r-- 1 root root 8.5G Mar  6 19:50 dump.rdb
-rw-r--r-- 1 root root 7.0K Apr 18 21:30 fusion_evaluate.sh
-rw-r--r-- 1 root root  22K Dec 26 14:20 quit
-rwxr-xr-x 1 root root 4.4M Jan 22 10:54 redis-benchmark
-rwxr-xr-x 1 root root  22K Dec 26 10:29 redis-check-aof
-rwxr-xr-x 1 root root  45K Dec 26 10:29 redis-check-dump
-rwxr-xr-x 1 root root 4.5M Dec 26 10:29 redis-cli
-rwxr-xr-x 1 root root  36K Dec 26 10:29 redis.conf
-rwxr-xr-x 1 root root  36K Dec 26 14:34 redis.conf-6479
-rwxr-xr-x 1 root root  36K Dec 26 10:29 redis.conf-6579
-rwxr-xr-x 1 root root  36K Jan 10 19:15 redis.conf-6679
-rwxr-xr-x 1 root root 6.1M Dec 26 10:29 redis-sentinel
-rwxr-xr-x 1 root root 6.1M Dec 26 10:29 redis-server
-rwxr-xr-x 1 root root 6.2M Dec 26 10:29 zedis-1.1.1.tgz
-r-x------ 1 root root    0 May  9 17:36 zhuangerwei
drwxr-xr-x 5 root root 4.0K Dec 26 10:29 zsh

1.3 文件的排序命令

-t 按照修改时间进行从新到旧的排序
-s 按照文件大小进行显示 -S按照文件大小进行显示并按从大到小进行排序
-r 以文件名反序列排序,或者加在其他命令后面进行反序排序

时间排序
[root@snrsdevapp02 zedis]# ls -lt
total 8877248
drwxr-xr-x 2 root root       4096 May 14 20:08 data
-r-x------ 1 root root          0 May  9 17:36 zhuangerwei
-rw-r--r-- 1 root root       7085 Apr 18 21:30 fusion_evaluate.sh
-rwxrwxrwx 1 root root       3072 Apr  2 20:04 check_load.sh
-rw-r--r-- 1 root root 9061636146 Mar  6 19:50 dump.rdb
-rwxr-xr-x 1 root root    4595960 Jan 22 10:54 redis-benchmark
-rwxr-xr-x 1 root root      36331 Jan 10 19:15 redis.conf-6679
-rwxr-xr-x 1 root root      36337 Dec 26 14:34 redis.conf-6479
-rw-r--r-- 1 root root      21887 Dec 26 14:20 quit
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf
-rwxr-xr-x 1 root root      36344 Dec 26 10:29 6480.conf
-rwxr-xr-x 1 root root      22225 Dec 26 10:29 redis-check-aof
-rwxr-xr-x 1 root root    6444889 Dec 26 10:29 zedis-1.1.1.tgz
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-sentinel
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-server
-rwxr-xr-x 1 root root    4690205 Dec 26 10:29 redis-cli
-rwxr-xr-x 1 root root      45443 Dec 26 10:29 redis-check-dump
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf-6579
drwxr-xr-x 5 root root       4096 Dec 26 10:29 zsh

时间倒序
[root@snrsdevapp02 zedis]# ls -ltr
total 8877248
drwxr-xr-x 5 root root       4096 Dec 26 10:29 zsh
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf-6579
-rwxr-xr-x 1 root root      45443 Dec 26 10:29 redis-check-dump
-rwxr-xr-x 1 root root    4690205 Dec 26 10:29 redis-cli
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-server
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-sentinel
-rwxr-xr-x 1 root root    6444889 Dec 26 10:29 zedis-1.1.1.tgz
-rwxr-xr-x 1 root root      22225 Dec 26 10:29 redis-check-aof
-rwxr-xr-x 1 root root      36344 Dec 26 10:29 6480.conf
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf
-rw-r--r-- 1 root root      21887 Dec 26 14:20 quit
-rwxr-xr-x 1 root root      36337 Dec 26 14:34 redis.conf-6479
-rwxr-xr-x 1 root root      36331 Jan 10 19:15 redis.conf-6679
-rwxr-xr-x 1 root root    4595960 Jan 22 10:54 redis-benchmark
-rw-r--r-- 1 root root 9061636146 Mar  6 19:50 dump.rdb
-rwxrwxrwx 1 root root       3072 Apr  2 20:04 check_load.sh
-rw-r--r-- 1 root root       7085 Apr 18 21:30 fusion_evaluate.sh
-r-x------ 1 root root          0 May  9 17:36 zhuangerwei
drwxr-xr-x 2 root root       4096 May 14 20:08 data

文件名倒序
[root@snrsdevapp02 zedis]# ls -lr
total 8877248
drwxr-xr-x 5 root root       4096 Dec 26 10:29 zsh
-r-x------ 1 root root          0 May  9 17:36 zhuangerwei
-rwxr-xr-x 1 root root    6444889 Dec 26 10:29 zedis-1.1.1.tgz
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-server
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-sentinel
-rwxr-xr-x 1 root root      36331 Jan 10 19:15 redis.conf-6679
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf-6579
-rwxr-xr-x 1 root root      36337 Dec 26 14:34 redis.conf-6479
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf
-rwxr-xr-x 1 root root    4690205 Dec 26 10:29 redis-cli
-rwxr-xr-x 1 root root      45443 Dec 26 10:29 redis-check-dump
-rwxr-xr-x 1 root root      22225 Dec 26 10:29 redis-check-aof
-rwxr-xr-x 1 root root    4595960 Jan 22 10:54 redis-benchmark
-rw-r--r-- 1 root root      21887 Dec 26 14:20 quit
-rw-r--r-- 1 root root       7085 Apr 18 21:30 fusion_evaluate.sh
-rw-r--r-- 1 root root 9061636146 Mar  6 19:50 dump.rdb
drwxr-xr-x 2 root root       4096 May 14 20:08 data
-rwxrwxrwx 1 root root       3072 Apr  2 20:04 check_load.sh
-rwxr-xr-x 1 root root      36344 Dec 26 10:29 6480.conf

文件由大到小
[root@snrsdevapp02 zedis]# ls -lS
total 8877248
-rw-r--r-- 1 root root 9061636146 Mar  6 19:50 dump.rdb
-rwxr-xr-x 1 root root    6444889 Dec 26 10:29 zedis-1.1.1.tgz
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-sentinel
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-server
-rwxr-xr-x 1 root root    4690205 Dec 26 10:29 redis-cli
-rwxr-xr-x 1 root root    4595960 Jan 22 10:54 redis-benchmark
-rwxr-xr-x 1 root root      45443 Dec 26 10:29 redis-check-dump
-rwxr-xr-x 1 root root      36344 Dec 26 10:29 6480.conf
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf
-rwxr-xr-x 1 root root      36337 Dec 26 14:34 redis.conf-6479
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf-6579
-rwxr-xr-x 1 root root      36331 Jan 10 19:15 redis.conf-6679
-rwxr-xr-x 1 root root      22225 Dec 26 10:29 redis-check-aof
-rw-r--r-- 1 root root      21887 Dec 26 14:20 quit
-rw-r--r-- 1 root root       7085 Apr 18 21:30 fusion_evaluate.sh
drwxr-xr-x 2 root root       4096 May 14 20:08 data
drwxr-xr-x 5 root root       4096 Dec 26 10:29 zsh
-rwxrwxrwx 1 root root       3072 Apr  2 20:04 check_load.sh
-r-x------ 1 root root          0 May  9 17:36 zhuangerwei

文件由小到大
[root@snrsdevapp02 zedis]# ls -lSr
total 8877248
-r-x------ 1 root root          0 May  9 17:36 zhuangerwei
-rwxrwxrwx 1 root root       3072 Apr  2 20:04 check_load.sh
drwxr-xr-x 5 root root       4096 Dec 26 10:29 zsh
drwxr-xr-x 2 root root       4096 May 14 20:08 data
-rw-r--r-- 1 root root       7085 Apr 18 21:30 fusion_evaluate.sh
-rw-r--r-- 1 root root      21887 Dec 26 14:20 quit
-rwxr-xr-x 1 root root      22225 Dec 26 10:29 redis-check-aof
-rwxr-xr-x 1 root root      36331 Jan 10 19:15 redis.conf-6679
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf-6579
-rwxr-xr-x 1 root root      36337 Dec 26 14:34 redis.conf-6479
-rwxr-xr-x 1 root root      36337 Dec 26 10:29 redis.conf
-rwxr-xr-x 1 root root      36344 Dec 26 10:29 6480.conf
-rwxr-xr-x 1 root root      45443 Dec 26 10:29 redis-check-dump
-rwxr-xr-x 1 root root    4595960 Jan 22 10:54 redis-benchmark
-rwxr-xr-x 1 root root    4690205 Dec 26 10:29 redis-cli
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-server
-rwxr-xr-x 1 root root    6307489 Dec 26 10:29 redis-sentinel
-rwxr-xr-x 1 root root    6444889 Dec 26 10:29 zedis-1.1.1.tgz
-rw-r--r-- 1 root root 9061636146 Mar  6 19:50 dump.rdb

1.4 递归查询文件

-R 这个命令可以递归查询出此目录下的所有目录

[root@snrsdevapp02 zedis]# ls -R
.:
6480.conf      dump.rdb            redis-benchmark   redis-cli        redis.conf-6579  redis-server     zsh
check_load.sh  fusion_evaluate.sh  redis-check-aof   redis.conf       redis.conf-6679  zedis-1.1.1.tgz
data           quit                redis-check-dump  redis.conf-6479  redis-sentinel   zhuangerwei

./data:
dump6480.rdb  dump.rdb  passport_prd_master_2.rdb

./zsh:
6380.log  6381  6381.log  data  pack  redis-10.37.152.215-6380.conf  redis-6380.conf  redis-6381.conf  redis.conf

./zsh/6381:
6381.log  dump-6381.rdb

./zsh/data:
6380.log  dump-6380.rdb  temp-1510467718.1494.rdb

./zsh/pack:
Ardb_1.1.1.zip  bin  etc  lib  log  rocksdata

./zsh/pack/bin:
ardb-repair  ardb-server  ardb-test

./zsh/pack/etc:
ardb.conf

./zsh/pack/lib:
libstdc++.so.6.0.18

./zsh/pack/log:

./zsh/pack/rocksdata:

1.5 特定的命令

--full-time显示完整时间
--color不同的类型显示不同颜色

2 CD命令

cd命令的参数不多,大致为以下几种
cd 进入用户主目录
cd ~ 进入用户主目录
cd - 返回OLDPWD的目录,即进入目录前的pwd
cd .. 返回上一级
cd ../.. 返回上两级

进入用户主目录
[root@snrsdevapp02 zedis]# cd 
[root@snrsdevapp02 ~]# ls
089f3b46de0aa6d8f72445d5506a15e8  backup
26370.log                         cabd75363d13e9f246d27ad6bc621e2f
26371.log                         e3dc2691365cd48ec7dca471fe5913b5
26372.log                         install.log
6370.log                          install.log.syslog
6371.log                          ngredisshard.lua
6380.log                          redis-4.0.2
6381.log                          redis-4.0.2.tar.gz
6af8f84e599cb9aa41308ddfd7b8c0c1  repl
96896008950dea47b2b56d8afa59dc0c  rocksdb
anaconda-ks.cfg                   ??Ƭ????????????v1.1.zip
[root@snrsdevapp02 ~]# pwd
/root
[root@snrsdevapp02 ~]# cd ~
[root@snrsdevapp02 ~]# ls
089f3b46de0aa6d8f72445d5506a15e8  backup
26370.log                         cabd75363d13e9f246d27ad6bc621e2f
26371.log                         e3dc2691365cd48ec7dca471fe5913b5
26372.log                         install.log
6370.log                          install.log.syslog
6371.log                          ngredisshard.lua
6380.log                          redis-4.0.2
6381.log                          redis-4.0.2.tar.gz
6af8f84e599cb9aa41308ddfd7b8c0c1  repl
96896008950dea47b2b56d8afa59dc0c  rocksdb
anaconda-ks.cfg                   ??Ƭ????????????v1.1.zip

返回进入目录前所在pwd
[root@snrsdevapp02 ~]# cd /
[root@snrsdevapp02 /]# cd /opt/zedis/
[root@snrsdevapp02 zedis]# cd -
/
[root@snrsdevapp02 /]# 

返回上一级和上两级目录
[root@snrsdevapp02 /]# cd /opt/zedis/
[root@snrsdevapp02 zedis]# cd ../
[root@snrsdevapp02 opt]# pwd
/opt
[root@snrsdevapp02 opt]# cd /opt/zedis/
[root@snrsdevapp02 zedis]# cd ../..
[root@snrsdevapp02 /]# pwd
/

3 pwd命令

主要注意的是完整路径和连接路径
pwd命令本身会输出连接档
pwd -P是强制输出全路径

4 mkdir 创建目录

  • -m 可以配置文件的权限,配合之前的数字权限配置比较方便
  • -p 可以一次性递归创建出相应的路径
[root@snrsdevapp02 zedis]# mkdir test
不加权限设置创建出来的权限为755
[root@snrsdevapp02 zedis]# ll -t
total 8877252
drwxr-xr-x 2 root root       4096 May 16 16:39 test

创建并设置权限为700
[root@snrsdevapp02 zedis]# mkdir -m 700 test 
[root@snrsdevapp02 zedis]# ll -t
total 8877252
drwx------ 2 root root       4096 May 16 16:42 test

创建递归的路径
[root@snrsdevapp02 zedis]# mkdir -p test/test1/test2
[root@snrsdevapp02 zedis]# ls -lR test
test:
total 4
drwxr-xr-x 3 root root 4096 May 16 16:43 test1

test/test1:
total 4
drwxr-xr-x 2 root root 4096 May 16 16:43 test2

test/test1/test2:
total 0

5 删除文件或者目录

  • rm:删除文件,也可用来删除目录
  • rmdir: 删除空的目录

5.1 rm命令

默认为询问模式
-f 强制删除不需要询问
-r递归删除,非常危险的选项

强制删除
[root@snrsdevapp02 zedis]# ll -t
-rw-r--r-- 1 root root          5 May 16 17:06 abc
drwxr-xr-x 2 root root       4096 May 14 20:08 data
[root@snrsdevapp02 zedis]# rm -f abc
[root@snrsdevapp02 zedis]# ll -t
drwxr-xr-x 2 root root       4096 May 14 20:08 data

递归删除
[root@snrsdevapp02 zedis]# mkdir -p test/test1/test2
[root@snrsdevapp02 zedis]# ll -lR test
test:
total 4
drwxr-xr-x 3 root root 4096 May 16 17:09 test1

test/test1:
total 4
drwxr-xr-x 2 root root 4096 May 16 17:09 test2

test/test1/test2:
total 0
[root@snrsdevapp02 zedis]# rm -rf test/
[root@snrsdevapp02 zedis]# ll -t
total 8877248
drwxr-xr-x 2 root root       4096 May 14 20:08 data

5.2 rmdir删除空目录

-p 递归删除路径上所有的非空目录

[root@snrsdevapp02 zedis]# mkdir -p test/test1/test2
[root@snrsdevapp02 zedis]# ls -lR test
test:
total 4
drwxr-xr-x 3 root root 4096 May 16 16:58 test1

test/test1:
total 4
drwxr-xr-x 2 root root 4096 May 16 16:58 test2

test/test1/test2:
total 0
[root@snrsdevapp02 zedis]# rmdir -p test/test1/test2/
[root@snrsdevapp02 zedis]# ls
6480.conf           redis-check-aof   redis-sentinel
check_load.sh       redis-check-dump  redis-server
data                redis-cli         zedis-1.1.1.tgz
dump.rdb            redis.conf        zhuangerwei
fusion_evaluate.sh  redis.conf-6479   zsh
quit                redis.conf-6579
redis-benchmark     redis.conf-6679

你可能感兴趣的:(linux文件和目录管理)