Linux实用操作命令

2019-06-18  本文已影响0人  五花肉_pork

处理文件和文件夹操作相关命令

1.创建文件:touch
touch命令最常被使用在这个场景:

在Linux系统中,一个文件有三种时间:

时间 含义 解释
modification time(mtime) 文件的“内容数据”被更改,该时间更新 修改文件的内容
status time(ctime) 文件的“状态”被更改,该时间更新 修改文件的属性或权限
access time(atime) 文件的内容被取用,该时间更新 例如使用cat读取文件

PS:通过ls -l命令查看文件,显示的是文件mtime。
通过touch命令创建文件:

LxiindeMacBook-Pro:Desktop Lxiin$ touch test.txt
LxiindeMacBook-Pro:Desktop Lxiin$ ls -l test.txt 
-rw-r--r--  1 kingsoft  staff  0  6 16 11:35 test.txt

2.创建目录:mkdir
mkdir的几个参数:

LxiindeMacBook-Pro:test Lxiin$ mkdir test1
LxiindeMacBook-Pro:test Lxiin$ mkdir -m 777 test2
LxiindeMacBook-Pro:test Lxiin$ ls -l
total 0
drwxr-xr-x  2 Lxiin  staff  64  6 16 11:44 test1
drwxrwxrwx  2 Lxiin  staff  64  6 16 11:44 test2
LxiindeMacBook-Pro:test Lxiin$ mkdir test
LxiindeMacBook-Pro:test Lxiin$ mkdir test/test1/test2
mkdir: test/test1: No such file or directory
LxiindeMacBook-Pro:test Lxiin$ mkdir -p test/test1/test2
LxiindeMacBook-Pro:test Lxiin$ tree
.
└── test
    └── test1
        └── test2

3 directories, 0 files

3.删除文件/文件夹:rm/rmdir

LxiindeMacBook-Pro:test1 Lxiin$ rm -i test1.txt 
remove test1.txt? y  
LxiindeMacBook-Pro:test1 Lxiin$ mkdir test2
LxiindeMacBook-Pro:test1 Lxiin$ rm -r test2/
LxiindeMacBook-Pro:test1 Lxiin$ touch test.txt
LxiindeMacBook-Pro:test1 Lxiin$ rm -f test.txt 
LxiindeMacBook-Pro:test1 Lxiin$ touch test2/test.txt
LxiindeMacBook-Pro:test1 Lxiin$ rmdir test2/
rmdir: test2/: Directory not empty

4.查看目录文件的内容:ls

5.切换和查看当前的工作目录:cd/pwd

LxiindeMacBook-Pro:test Lxiin$ cd 
LxiindeMacBook-Pro:~ Lxiin$ cd ~/Desktop/test
LxiindeMacBook-Pro:test Lxiin$ pwd
/Users/Lxiin/Desktop/test/

6.查看文件内容:cat/tac/head/tail/more/less/rev/od
cat:查看文件的内容

LxiindeMacBook-Pro:test1 Lxiin$ cat test.txt 
abcd
1234

tac:将每个指定文件按行倒置并写到标准输出

LxiindeMacBook-Pro:test1 Lxiin$ tac test.txt 
1234
abcd

rev:显示最后一个字符到第一个字符

LxiindeMacBook-Pro:test1 Lxiin$ rev test.txt 
dcba
4321

7.拷贝:cp
用法:cp ([参数]) [目标文件] [目标目录]

LxiindeMacBook-Pro:test1 Lxiin$ tree
.
├── test.txt
└── test2

1 directory, 1 file
LxiindeMacBook-Pro:test1 Lxiin$ cp test.txt test2/
LxiindeMacBook-Pro:test1 Lxiin$ tree
.
├── test.txt
└── test2
    └── test.txt

1 directory, 2 files

8.移动:mv
mv命令可用来进行文件、文件夹的改名,和移动文件和文件夹。

LxiindeMacBook-Pro:test Lxiin$ ls
test1
LxiindeMacBook-Pro:test Lxiin$ mv test1/ test2
LxiindeMacBook-Pro:test Lxiin$ ls
test2
LxiindeMacBook-Pro:test Lxiin$
LxiindeMacBook-Pro:test Lxiin$ ls
test.txt    test1
LxiindeMacBook-Pro:test Lxiin$ mv test.txt test1
LxiindeMacBook-Pro:test Lxiin$ tree
.
└── test1
    └── test.txt

1 directory, 1 file

9.文件重命名:rename
rename相比于mv重命名文件,优势在于可以批量操作:将txt后缀的文档批量修改为rtf后缀的文档

LxiindeMacBook-Pro:test1 Lxiin$ rename .txt .rtf *.txt

10.查找文件:find
使用方法:find [PATH] [option] [action]

查找当前目录下过去24小时有改动的文件(其中0代表过去的24小时,换成3则是3天前的24小时内):

LxiindeMacBook-Pro:test1 Lxiin$ find . -mtime 0
.
./test1.txt
./test.txt

查找当前目录下Lxiin创建的文件:

LxiindeMacBook-Pro:test1 Lxiin$ find . -user Lxiin
.
./test1.txt
./test.txt
LxiindeMacBook-Pro:test1 Lxiin$ ls -l
total 0
-rw-r--r--  1 Lxiin  staff  0  6 17 20:32 test.txt
-rw-r--r--  1 Lxiin  staff  0  6 17 20:37 test1.txt

查找当前目录下名称为‘test.txt’的文件:

LxiindeMacBook-Pro:test1 Lxiin$ find . -name test.txt
./test.txt

查找当前目录下名称带有'.txt'后缀的文件:

LxiindeMacBook-Pro:test1 Lxiin$ find . -name '*.txt'
./test1.txt
./test.txt

11.创建链接:ln
使用方法:ln [-sf] [源文件] [目标文件]

将‘test.txt’文件创建一个符号链接:

LxiindeMacBook-Pro:test1 Lxiin$ ln -s test.txt test
LxiindeMacBook-Pro:test1 Lxiin$ ls -ls
total 0
0 lrwxr-xr-x  1 kingsoft  staff  8  6 18 23:14 test -> test.txt
0 -rw-r--r--  1 kingsoft  staff  0  6 17 20:32 test.txt

12.压缩和解压缩:gzip/gunzip
压缩‘test.txt‘文件:

LxiindeMacBook-Pro:test1 Lxiin$ gzip test.txt 
LxiindeMacBook-Pro:test1 Lxiin$ ls
test.txt.gz

解压’test.txt.gz‘文件:

LxiindeMacBook-Pro:test1 Lxiin$ gunzip test.txt.gz 
LxiindeMacBook-Pro:test1 Lxiin$ ls
test.txt
上一篇 下一篇

猜你喜欢

热点阅读