linux编程向:chap2-文件及目录管理


2.1 创建和删除

# 删除日志
$ rm *log
$ find ./ -name "*log" -exec rm {};

# 查看当前目录下文件个数
$ find ./ | wc -l

2.2 目录切换

这部分都比较简单跳过


2.3 列出目录项

$ ls # 显示当前目录下的文件
$ ls -lrt # 按时间顺序,以列表的方式显示目录项
-rw-r--r-- 1 root root      3204 Jan 28 13:19 README.md
-rw-r--r-- 1 root root      1063 Jan 28 13:19 LICENSE
drwxr-xr-x 2 root root      4096 Jan 28 13:19 doc
-rw-r--r-- 1 root root 325978726 Jan 28 13:30 deeplab-drn.pth
drwxr-xr-x 3 root root      4096 Jan 28 15:10 utils
-rw-r--r-- 1 root root       189 Jan 28 15:14 train_voc.sh
-rw-r--r-- 1 root root       665 Jan 28 16:26 mypath.py
drwxr-xr-x 3 root root      4096 Jan 29 10:12 run
drwxr-xr-x 6 root root      4096 Jan 29 10:25 modeling

$ ls | cat -n # 给每个文件前面增加一个id编号(看上去更整洁)

2.4 查找目录及文件

$ find ./ -name "train*" | xargs file # 搜寻文件或目录
./train-190129.log:                               UTF-8 Unicode text, with very long lines
./train_voc.sh:                                   ASCII text
./train-190131.log:                               C source, UTF-8 Unicode text, with very long lines
./train_coco.sh:                                  ASCII text
./train.py:                                       Python script, ASCII text executable
./train-190304.log:                               UTF-8 Unicode text, with very long lines
./coco/annotations/train_ids_2017.pth:            8086 relocatable (Microsoft)
./coco/images/train2017:                          directory
./.ipynb_checkpoints/train-checkpoint.py:         empty
./.ipynb_checkpoints/train-190129-checkpoint.log: UTF-8 Unicode text, with very long lines, with CR, LF line terminators
./.ipynb_checkpoints/train-190304-checkpoint.log: ASCII text, with very long lines, with CR, LF line terminators
./.ipynb_checkpoints/train-190131-checkpoint.log: C source, UTF-8 Unicode text, with very long lines, with CR, LF line terminators

$ find ./ -name "*.log" # 查找目标文件夹中是否有log文件
./train-190129.log
./train-190131.log
./train-190304.log
./.ipynb_checkpoints/train-190129-checkpoint.log
./.ipynb_checkpoints/train-190304-checkpoint.log
./.ipynb_checkpoints/train-190131-checkpoint.log

$ find ./ -name "*.o" -exec rm {} \; #递归当前目录及子目录删除所有.o文件

find是实时查找,如果需要更快的查询,可以选择locate;locate会为文件系统建立索引数据库,如果有文件更新,需要定期执行更新命令来更新索引库;与find不同,locate并不是实时查找。需要更新数据库,来获得最新的文件索引信息。


2.5 查看文件

关键字:cat vi head tail more

# 句法:
$ cat -n #显示文件内容的同时显示行号
# 举例子:
$ cat -n train-190129.log

# 句法:
$ ls -al|more #按页显示列表内容
# 举例子:
$ ls -al|more train-190129.log

# 句法:
$ head -n #只看前面n行
# 举例子:
$ head -10 train-190129.log

# 句法:
$ tail -n #显示文件倒数第n行
# 举例子:
$ tail -10 train-190129.log


# 句法:
$ diff file1 file2 #查看两个文件的差别
# 举例子:
$ diff train_coco.sh train_voc.sh
< python3 train.py --backbone resnet --lr 0.01 \
< --workers 4 --epochs 1000 --batch-size 4 \
< --gpu-ids 0 --checkname deeplab-resnet \
< --eval-interval 1 --dataset coco
---
> python3 train.py --backbone resnet --lr 0.007 \
> --workers 4 --epochs 50 --batch-size 16 \
> --gpu-ids 0 --checkname deeplab-resnet --eval-interval 1 --dataset pascal

# 句法:
$ tail -f file1 #动态显示文本最新信息
# 举例子:
$ tail -f train-190129.log

2.6 查找文件内容

使用egrep查询文件内容

# 句法:
$ egrep file # 查询文件内容,返回到屏幕上
# 举例子:
$ egrep 'Train' train-190131.log

# 句法:
$ egrep file > co.out # 查询文件内容,返回到文件中去
# 举例子:
$ egrep 'Train' train-190131.log > co.out

2.7 文件与目录权限修改

$ chown runoob:runoobgroup file1 # 将文件 file1 的拥有者设为 runoob 群体的使用者 runoobgroup
$ chmod

 

你可能感兴趣的:(命令行相关)