文件目录的增删改查

1. 创建目录mkdir

  • mkdir [dir]: 正常创建目录[dir], 如果原本就存在报错
➜  linux_commands mkdir tt
➜  linux_commands mkdir tt
mkdir: tt: File exists
  • mkdir -p [dir1/dir2]: 递归创建目录dir1,dir2,如果原本就存在也不报错。下面用的ls命令是展示文件目录,后续会讲解。
➜  linux_commands mkdir -p ss/tt
➜  linux_commands ls
ss
➜  linux_commands ls ss
tt
  • mkdir -m [xxx] [dir]: 创建目录[dir]并设定权限, mode=777表示所有用户均可读、写、执行。
➜  linux_commands mkdir -m 777 uu
➜  linux_commands ls -l
total 0
drwxrwxrwx  2 qiushye  staff  64 Mar 14 23:11 uu

2. 查看目录下的文件信息ls

  • ls -a(/A/f) [dir]: 查看[dir]下的所有文件目录,(不加[dir]参数默认指当前目录,下面省略)其中-a列出所有目录并排序展示,-A不列出. 和..目录,-f和-a输出内容相同,但不对文件名进行排序。
➜  linux_commands ls -a
.  .. tt uu
➜  linux_commands ls -A
tt uu
➜  linux_commands ls -f
.  .. uu tt
  • ls -F: 对不同文件类型按不同后缀展示,如目录是'/'后缀,可执行文件是'*'后缀,系统链接是'@'后缀,普通文本没有后缀。
➜  linux_commands ls -F
cp*    test   test1@ tt/    uu/
  • ls -l:展示目录下所有文件的权限,用户名,组名,文件大小,修改时间和文件名等,ls -l可以加各种附加参数,下面具体介绍
➜  linux_commands ls -l
total 96
-rwxr-xr-x  1 qiushye  staff  42272 Mar 15 17:56 cp
-rw-r--r--  1 qiushye  staff      1 Mar 15 17:50 test
lrwxr-xr-x  1 qiushye  staff      4 Mar 15 17:51 test1 -> test
drwxr-xr-x  2 qiushye  staff     64 Mar 15 17:43 tt
drwxrwxrwx  2 qiushye  staff     64 Mar 14 23:11 uu

1) -h  ls -lh: 用较友好的方式显示文件大小

➜  linux_commands ls -lh
total 96
-rwxr-xr-x  1 qiushye  staff    41K Mar 15 17:56 cp
-rw-r--r--  1 qiushye  staff     1B Mar 15 17:50 test
lrwxr-xr-x  1 qiushye  staff     4B Mar 15 17:51 test1 -> test
drwxr-xr-x  2 qiushye  staff    64B Mar 15 17:43 tt
drwxrwxrwx  2 qiushye  staff    64B Mar 14 23:11 uu

2) -O  ls -lO: 展示文件的标签,大部分的标签是'-',也有'hidden'标签

➜  linux_commands ls -lO
total 96
-rwxr-xr-x  1 qiushye  staff  - 42272 Mar 15 17:56 cp
-rw-r--r--  1 qiushye  staff  -     1 Mar 15 17:50 test
lrwxr-xr-x  1 qiushye  staff  -     4 Mar 15 17:51 test1 -> test
drwxr-xr-x  2 qiushye  staff  -    64 Mar 15 17:43 tt
drwxrwxrwx  2 qiushye  staff  -    64 Mar 14 23:11 uu

3) -T  ls -lT: 展示修改时间的年月日时分秒

➜  linux_commands ls -lT
total 96
-rwxr-xr-x  1 qiushye  staff  42272 Mar 15 17:56:14 2020 cp
-rw-r--r--  1 qiushye  staff      1 Mar 15 17:50:29 2020 test
lrwxr-xr-x  1 qiushye  staff      4 Mar 15 17:51:10 2020 test1 -> test
drwxr-xr-x  2 qiushye  staff     64 Mar 15 17:43:42 2020 tt
drwxrwxrwx  2 qiushye  staff     64 Mar 14 23:11:17 2020 uu

4) -t  ls -lt: 对文件按修改时间排序, 默认降序,如果需要升序排列加上-r参数(反序排列)

➜  linux_commands ls -lt
total 96
-rwxr-xr-x  1 qiushye  staff  42272 Mar 15 17:56 cp
lrwxr-xr-x  1 qiushye  staff      4 Mar 15 17:51 test1 -> test
-rw-r--r--  1 qiushye  staff      1 Mar 15 17:50 test
drwxr-xr-x  2 qiushye  staff     64 Mar 15 17:43 tt
drwxrwxrwx  2 qiushye  staff     64 Mar 14 23:11 uu

➜  linux_commands ls -ltr
total 96
drwxrwxrwx  2 qiushye  staff     64 Mar 14 23:11 uu
drwxr-xr-x  2 qiushye  staff     64 Mar 15 17:43 tt
-rw-r--r--  1 qiushye  staff      1 Mar 15 17:50 test
lrwxr-xr-x  1 qiushye  staff      4 Mar 15 17:51 test1 -> test
-rwxr-xr-x  1 qiushye  staff  42272 Mar 15 17:56 cp

5) -u  ls -lu: 列出最后一次访问对时间,和最后修改时间不同

➜  linux_commands ls -lu
total 96
-rwxr-xr-x  1 qiushye  staff  42272 Mar 15 17:56 cp
-rw-r--r--  1 qiushye  staff      1 Mar 15 17:50 test
lrwxr-xr-x  1 qiushye  staff      4 Mar 15 17:51 test1 -> test
drwxr-xr-x  2 qiushye  staff     64 Mar 15 17:43 tt
drwxrwxrwx  2 qiushye  staff     64 Mar 14 23:11 uu

6) -S  ls -lS: 按文件大小排序展示, 默认降序,如需升序加-r参数

➜  linux_commands ls -lS
total 96
-rwxr-xr-x  1 qiushye  staff  42272 Mar 15 17:56 cp
drwxr-xr-x  2 qiushye  staff     64 Mar 15 17:43 tt
drwxrwxrwx  2 qiushye  staff     64 Mar 14 23:11 uu
lrwxr-xr-x  1 qiushye  staff      4 Mar 15 17:51 test1 -> test
-rw-r--r--  1 qiushye  staff      1 Mar 15 17:50 test
➜  linux_commands ls -lSr
total 96
-rw-r--r--  1 qiushye  staff      1 Mar 15 17:50 test
lrwxr-xr-x  1 qiushye  staff      4 Mar 15 17:51 test1 -> test
drwxrwxrwx  2 qiushye  staff     64 Mar 14 23:11 uu
drwxr-xr-x  2 qiushye  staff     64 Mar 15 17:43 tt
-rwxr-xr-x  1 qiushye  staff  42272 Mar 15 17:56 cp
  • ls -n: 和ls -l类似,但用数字展示用户和组信息,而不是用名称
➜  linux_commands ls -n
total 96
-rwxr-xr-x  1 501  20  42272 Mar 15 17:56 cp
-rw-r--r--  1 501  20      1 Mar 15 17:50 test
lrwxr-xr-x  1 501  20      4 Mar 15 17:51 test1 -> test
drwxr-xr-x  2 501  20     64 Mar 15 17:43 tt
drwxrwxrwx  2 501  20     64 Mar 14 23:11 uu

3. 跳转目录cd

  • cd [dir]: 跳转到[dir]目录下,如果不存在则报错
  • cd .. : 跳转到上级目录, 注: cd ...表示跳到上上级目录,有些终端可能不支持
  • cd - : 跳转至上次调用cd时所在到目录, 并打印出新位置的path
➜  linux_commands cd tt
➜  tt ls
ss    test1 test2 test3
➜  tt cd aa
cd: no such file or directory: aa
➜  tt
➜  tt cd ..
➜  linux_commands
➜  linux_commands cd tt/ss
➜  ss
➜  ss cd -
~/linux_commands
➜  linux_commands

附加:
➜  linux_commands cd -
~/linux_commands/tt/ss
➜  ss cd ...
➜  linux_commands cd -
~/linux_commands/tt/ss
➜  ss cd tt
➜  tt cd ....
➜  linux_commands

4. 移除文件rm

  • rm -d [file]: 尝试删除文件和非空目录
➜  linux_commands ls
test tt   uu
➜  linux_commands ls tt
ss
➜  linux_commands rm -d *
zsh: sure you want to delete all 3 files in /Users/qiushye/linux_commands [yn]? y
rm: tt: Directory not empty
➜  linux_commands ls
tt
  • rm -i [file]: 删除前进行确认,输入y表示同意,n表示不同意,如果文件不存在会提示。
➜  linux_commands rm -i test
remove test? n
➜  linux_commands ls
test tt   uu
➜  linux_commands rm -i test
remove test? y
➜  linux_commands ls
tt uu
➜  linux_commands rm -i test
rm: test: No such file or directory
  • rm -f [file]: 删除前不做确认,也无视文件释放允许。如果文件不存在不报错。
➜  linux_commands ls
test tt   uu
➜  linux_commands chmod 111 test
➜  linux_commands rm test
override --x--x--x  qiushye/staff for test? n
➜  linux_commands rm -f test
➜  linux_commands ls
tt uu
➜  linux_commands rm -f test
➜  linux_commands
  • rm -r(=-R) [file/dir]: 删除文件或目录,如果加上-i参数会对每个文件进行删除确认
➜  linux_commands rm -r *
zsh: sure you want to delete all 3 files in /Users/qiushye/linux_commands [yn]? n
➜  linux_commands rm -ri *
zsh: sure you want to delete all 3 files in /Users/qiushye/linux_commands [yn]? y
remove test?
examine files in directory tt? y
examine files in directory tt/ss? y
remove tt/ss? y
remove tt? y
examine files in directory uu? y
remove uu? y
➜  linux_commands ls
test

经常会将-r和-f参数一起使用,但有时候可能出造成不可挽回的事件,请慎用! 可以对rm命令进行改造来避免,具体请自查。

5. 移动或重命名文件mv

  • mv -i [file1] [file2]: 如果移动[file1]到[file2]会导致覆盖已存在的文件或目录,即名称冲突,则给出确认请求
➜  linux_commands ls
test  test1
➜  linux_commands mv -i test test1
overwrite test1? (y/n [n]) n
not overwritten
➜  linux_commands mv -i test1 test
overwrite test? (y/n [n]) n
not overwritten
  • mv -n [file1] [file2]: 如果移动会导致覆盖已存在的文件,即名称冲突,则移动失败且不提示
➜  linux_commands mv -n test test1
➜  linux_commands ls
ss    test  test1 tt
➜  linux_commands mv -n test test2
➜  linux_commands ls
ss    test1 test2 tt
  • mv -f [file1] [file2]: 强制移动文件,即使覆盖也不做确认, mv不带参数默认-f
➜  linux_commands ls
ss    test1 test2 tt
➜  linux_commands mv -f test1 test2
➜  linux_commands ls
ss    test2 tt
➜  linux_commands mv -f ss tt
➜  linux_commands ls
test2 tt
  • 如果源参数和目的参数都是文件或都是目录(不带/后缀),则mv是重命名操作,否则就是移动文件操作
➜  linux_commands ls
test2 tt
➜  linux_commands mv tt test2
mv: rename tt to test2: Not a directory
➜  linux_commands mv test2 tt
➜  linux_commands ls
tt
➜  linux_commands ls tt
ss    test2

➜  linux_commands mkdir uu
➜  linux_commands ls
tt uu
➜  linux_commands mv tt uu/
➜  linux_commands ls uu
tt
➜  linux_commands

6. 复制文件cp

  • cp -i [file1] [file2]: 如果移动[file1]到[file2]会导致覆盖已存在的文件或目录,即名称冲突,则给出确认请求,文件名可不一致
  • cp -n [file1] [file2]: 如果移动[file1]到[file2]会导致覆盖已存在的文件或目录,则复制失败且不提示
  • cp -f [file1] [file2]: 如果移动[file1]到[file2]会导致覆盖已存在的文件或目录,则强制复制, 不带参数默认是-f
linux_commands cp -i test1 test2
overwrite test2? (y/n [n]) n
not overwritten
➜  linux_commands cp -n test1 test2
➜  linux_commands ls
test1 test2 tt    uu
➜  linux_commands cp -f test1 test2
➜  linux_commands ls
test1 test2 tt    uu
➜  linux_commands diff test1 test2
➜  linux_commands
  • cp -R [dir1/file] [dir2]: 将目录[dir1]或文件[file]复制到[dir2]目录下
➜  linux_commands mkdir uu
➜  linux_commands cp tt uu
cp: tt is a directory (not copied).
➜  linux_commands cp -R tt uu
➜  linux_commands ls uu
tt
  • cp -p: 复制文件保留修改时间、用户ID等信息。
  • cp -a == cp -pPR

7. 查找文件find

  • find [root_path] -name [file_info]: 在根目录[root_path]根据文件名信息[file_info]查找文件,文件信息可以是通配符
➜  linux_commands find . -name "tt"
./uu/tt
./uu/tt/ss/tt
./uu/tt/ss/tt/ss/tt
./tt
./tt/ss/tt
./tt/ss/tt/ss/tt

➜  linux_commands find . -name "*ss"
./uu/tt/ss
./uu/tt/ss/tt/ss
./tt/ss
./tt/ss/tt/ss
  • -or: 多模式查找
➜  linux_commands find . -name "*ss" -or -name "*uu"
./uu
./uu/tt/ss
./uu/tt/ss/tt/ss
./tt/ss
./tt/ss/tt/ss
  • find [root_path] -type d: 按文件类型查找,具体参数含义如下所示,
    • b       block special
    • c       character special
    • d       directory          
    • f       regular file             
    • l       symbolic link             
    • p       FIFO             
    • s       socket
➜  linux_commands find . -type d
.
./uu
./uu/tt
./uu/tt/ss
./uu/tt/ss/tt
./uu/tt/ss/tt/ss
./uu/tt/ss/tt/ss/tt
./tt
./tt/ss
./tt/ss/tt
./tt/ss/tt/ss
./tt/ss/tt/ss/tt
➜  linux_commands find . -type f
./uu/tes
./uu/tt/test1
./uu/tt/test2
./.DS_Store
./test1
./test3
./test2
./tt/test1
./tt/test2
  • find [root_path] -path [path_info]: 按文件目录查找,[path_info]可以含通配符
➜  linux_commands find . -path "*/uu/tt/ss/*"
./uu/tt/ss/tt
./uu/tt/ss/tt/ss
./uu/tt/ss/tt/ss/tt
  • find [root_path] -name [name_info] -not -path [path_info]: 排除指定目录匹配对应文件
➜  linux_commands find . -name "*ss" -not -path "*uu/tt*"
./tt/ss
./tt/ss/tt/ss
  • find [root_path] -size [size_info]: 按文件大小查找, size_info前带+表示大于该值,加-表示小于该值
➜  linux_commands find . -size +1k
./.DS_Store
➜  linux_commands find . -size -1k -size +100
.
./uu
./uu/tt
./tt
  • find [root_path] -mtime n: 查找n天内修改过的文件,用-前缀表示所在时间段以内,用+前缀表示所在时间段以外
    • -amin n   查找系统中最后n分钟访问的文件
    • -atime n  查找系统中最后n*24小时访问的文件
    • -cmin n   查找系统中最后n分钟修改文件状态的文件
    • -ctime n  查找系统中最后n*24小时修改文件状态的文件
    • -mmin n   查找系统中最后N分钟修改过的文件
    • -mtime n  查找系统中最后n*24小时修改过的文件
➜  linux_commands find . -mtime -1
./uu
./uu/tes
./tt
./tt/rf
➜  linux_commands find . -mtime +4
./.DS_Store
./tt/test1
./tt/ss
./tt/ss/tt
./tt/ss/tt/ss
./tt/ss/tt/ss/tt
➜  linux_commands find . -mtime +1 -mtime -4
.
./uu/tt
./uu/tt/test1
./uu/tt/ss
./uu/tt/ss/tt
./uu/tt/ss/tt/ss
./uu/tt/ss/tt/ss/tt
./uu/tt/test3
./uu/tt/test2
./test1
./test3
./test2
  • -exec [exec_info]: 可以对查找到到文件执行命令[exec_info]
➜  linux_commands find . -mtime +4 -type f -exec wc -l {} \;
       0 ./.DS_Store
       1 ./tt/test1
  • -delete: 删除查找到的文件
➜  linux_commands find . -mtime +4 -type f
./.DS_Store
./tt/test1
➜  linux_commands find . -mtime +4 -type f -delete
➜  linux_commands find . -mtime +4 -type f
➜  linux_commands

8. 定位文件locate

  • locate [pattern]: 在数据库中查找符合[pattern]的所有文件
➜  linux_commands locate uu/**/tt
/Users/qiushye/linux_commands/uu/tt
/Users/qiushye/linux_commands/uu/tt/ss
/Users/qiushye/linux_commands/uu/tt/ss/tt
/Users/qiushye/linux_commands/uu/tt/ss/tt/ss
/Users/qiushye/linux_commands/uu/tt/ss/tt/ss/tt
/Users/qiushye/linux_commands/uu/tt/test1
/Users/qiushye/linux_commands/uu/tt/test2
/Users/qiushye/linux_commands/uu/tt/test3
/Users/qiushye/linux_commands/uu/tt/ss/tt/ss
/Users/qiushye/linux_commands/uu/tt/ss/tt/ss/tt
  •  locate [file_path]: 根据部分的相对文件路径查找所有文件
➜  linux_commands locate tt/test1
/Users/qiushye/.none/uu/tt/test1
/Users/qiushye/linux_commands/uu/tt/test1
/Users/qiushye/tt/test1

9. 打印文件内容cat, head, tail

  • cat -b [file]: 对非空行编号展示
  • cat -n [file]: 对所有行编号展示
➜  linux_commands cat -b test1
     1	hello world!


     2	oh my god!
     3	你是
➜  linux_commands cat -n test1
     1	hello world!
     2
     3
     4	oh my god!
     5	你是
  • cat -s [file]: 合并多个连续空行
➜  linux_commands cat -sn test1
     1	hello world!
     2
     3	oh my god!
     4	你是
➜  linux_commands cat -sb test1
     1	hello world!

     2	oh my god!
     3	你是
  • cat [file1] [file2] > [file3]: 将[file1]和[file2]的内容以覆盖形式写入到[file3]
➜  linux_commands cat test1
hello world!


oh my god!
你是
➜  linux_commands cat test2

ss
➜  linux_commands cat test1 test2 > test3
➜  linux_commands cat test3
hello world!


oh my god!
你是

ss
  • cat [file1] - [file2] - [file3]: 依次输出[file1], [file2], [file3],用^D命令跳到下一个文件输出
➜  linux_commands cat test4
this is test4
➜  linux_commands cat test5
this is test5
➜  linux_commands cat test1 - test4 - test5
hello world!


oh my god!
你是
this is test4
this is test5
  • head -n [file]: 输出[file]文件内容的前n行,如果不加-n参数则默认前10行
  • tail -n [file]: 输出[file]文件内容的后n行,如果不加-n参数则默认后10行

10. 修改文件权限chmod

  • chmod [mode] [file]: 将[file]的权限设置为[mode],如将test1权限设置为777表示用户(用u表示)、群组(用g表示)和其他人(用o表示)都对test1文件有读、写、执行权限。

    mode是一个3位的8进制数表示,分别对应u,g,o的权限,而每位的数值其实又可以拆分成3位的二进制,依次表示是否有读(r)、写(w)、执行(x)权限,比如7等同于(111),则表示具有读写执行权限,而5等同于(101),则表示具有读和执行权限。mode的每个8进制数相互独立,也就是说用户、群组、其他人对于文件的权限相互独立。具体可见下图。

➜  linux_commands ls -l test1
-rwx------  1 qiushye  staff  83 Mar 27 13:03 test1
➜  linux_commands chmod 777 test1
➜  linux_commands ls -l test1
-rwxrwxrwx  1 qiushye  staff  83 Mar 27 13:03 test1
  • chmod -R [mode] [dir]: 对目录[dir]内的文件权限设置为[mode],如果目录下有链接,则不会更改权限(虽然文档说加-L参数会更改,但测试发现并没有更改。)
  • chmod -h [mode] [link]: 对系统链接[link]的权限设置为[mode]
➜  linux_commands ll tt
total 8
lrwxr-xr-x  1 qiushye  staff    25B Mar 22 20:40 rf -> /Users/qiushye/refund.csv
drwx------  3 qiushye  staff    96B Mar 17 12:27 ss
-rwx------  1 qiushye  staff     2B Mar 18 13:18 test2
lrwx-w---x  1 qiushye  staff     5B Mar 18 13:18 test3 -> test1
➜  linux_commands chmod -R 766 tt
➜  linux_commands ll tt
total 8
lrwxr-xr-x  1 qiushye  staff    25B Mar 22 20:40 rf -> /Users/qiushye/refund.csv
drwxrw-rw-  3 qiushye  staff    96B Mar 17 12:27 ss
-rwxrw-rw-  1 qiushye  staff     2B Mar 18 13:18 test2
lrwx-w---x  1 qiushye  staff     5B Mar 18 13:18 test3 -> test1

➜  linux_commands chmod -h 766 tt/rf
➜  linux_commands ll tt/rf
lrwxrw-rw-  1 qiushye  staff    25B Mar 22 20:40 tt/rf -> /Users/qiushye/refund.csv

 

  • chmod [a|u|g|o] [+/-][r|w|x] [file]: 对文件[file]添加或删除(所有人|用户|组|其他人)的读、写或执行权限
➜  linux_commands ls -l
total 40
-rwxrwxrwx  1 qiushye  staff   83 Mar 27 13:03 test1
-rw-r--r--  1 qiushye  staff    4 Mar 25 12:18 test2
-rw-r--r--  1 qiushye  staff   37 Mar 25 13:39 test3
-rw-r--r--  1 qiushye  staff   14 Mar 25 13:41 test4
-rw-r--r--  1 qiushye  staff   14 Mar 25 13:41 test5
drwxrw-rw-  6 qiushye  staff  192 Mar 22 21:24 tt
drwxr-xr-x  3 qiushye  staff   96 Mar 22 21:23 uu
➜  linux_commands chmod a-w test1
➜  linux_commands ls -l test1
-r-xr-xr-x  1 qiushye  staff  83 Mar 27 13:03 test1
➜  linux_commands chmod g+w test2
➜  linux_commands ls -l test2
-rw-rw-r--  1 qiushye  staff  4 Mar 25 12:18 test2

  • 对目录设置权限,查看目录需要r, x权限
➜  linux_commands chmod 077 tt
➜  linux_commands ls tt
ls: tt: Permission denied
➜  linux_commands chmod u+rw tt
➜  linux_commands ls tt
➜  linux_commands chmod u+x tt
➜  linux_commands ls tt
rf    ss    test2 test3
  • chmod o=u [file]: 使其他人和用户对[file]的权限一致
➜  linux_commands ls -l test4
-rw-r--r--  1 qiushye  staff  14 Mar 25 13:41 test4
➜  linux_commands chmod o=u test4
➜  linux_commands ls -l test4
-rw-r--rw-  1 qiushye  staff  14 Mar 25 13:41 test4
  • chmod o=  [file]: 移除其他人(o)对[file]的所有权限, o可以换成g, a, u
➜  linux_commands chmod o= test4
➜  linux_commands ls -l test4
-rw-r-----  1 qiushye  staff  14 Mar 25 13:41 test4
  • chmod -R g-w,o+x [dir]: 对目录下的所有文件或目录去除群组(g)的写权限,添加其他人对执行权限, g-w和o+x可以替换成其他表达式;-R参数对系统链接权限无影响。
➜  linux_commands ls -l tt
total 8
lrwxrw-rw-  1 qiushye  staff  25 Mar 22 20:40 rf -> /Users/qiushye/refund.csv
drwxrw-rw-  3 qiushye  staff  96 Mar 17 12:27 ss
-rwxrw-rw-  1 qiushye  staff   2 Mar 18 13:18 test2
lrwx-w---x  1 qiushye  staff   5 Mar 18 13:18 test3 -> test1
➜  linux_commands chmod -R g-w,o+x tt
➜  linux_commands ls -l tt
total 8
lrwxrw-rw-  1 qiushye  staff  25 Mar 22 20:40 rf -> /Users/qiushye/refund.csv
drwxr--rwx  3 qiushye  staff  96 Mar 17 12:27 ss
-rwxr--rwx  1 qiushye  staff   2 Mar 18 13:18 test2
lrwx-w---x  1 qiushye  staff   5 Mar 18 13:18 test3 -> test1

11. 修改文件拥有者chown

  • chown [user] [file]: 将[file]的拥有者改为[user], 一般要加sudo权限,下同
➜  linux_commands chmod 700 test2 (设置为当前用户才有权限)
➜  linux_commands ls -l test2
-rwx------  1 qiushye  staff  4 Mar 25 12:18 test2
➜  linux_commands chown eric test2
chown: test2: Operation not permitted
➜  linux_commands sudo chown eric test2
➜  linux_commands ls -l test2
-rwx------  1 eric  staff  4 Mar 25 12:18 test2
➜  linux_commands cat test2
cat: test2: Permission denied
  • chown -R [user] [dir]: 递归将目录[dir]下文件的拥有者改为[user]
➜  linux_commands ls -l tt
total 8
lrwxrw-rw-  1 qiushye  staff  25 Mar 22 20:40 rf -> /Users/qiushye/refund.csv
drwxr--rwx  3 qiushye  staff  96 Mar 17 12:27 ss
-rwxr--rwx  1 qiushye  staff   2 Mar 18 13:18 test2
lrwx-w---x  1 qiushye  staff   5 Mar 18 13:18 test3 -> test1
➜  linux_commands sudo chown -R eric tt
➜  linux_commands ls -l tt
total 8
lrwxrw-rw-  1 eric  staff  25 Mar 22 20:40 rf -> /Users/qiushye/refund.csv
drwxr--rwx  3 eric  staff  96 Mar 17 12:27 ss
-rwxr--rwx  1 eric  staff   2 Mar 18 13:18 test2

ls: tt/test3: Permission denied (其他用户只有执行权限,没有读权限,所以denied)
lrwx-w---x  1 eric  staff   5 Mar 18 13:18 test3
  • chown user:group [file]: 将文件[file]的拥有者改为群组group下的用户user
➜  linux_commands ls -l test4
----------  1 qiushye  staff   14 Mar 25 13:41 test4
➜  linux_commands sudo chown eric:localaccounts test4
➜  linux_commands ls -l test4
----------  1 eric  localaccounts  14 Mar 25 13:41 test4

12. 比较文件diff

  • diff [file1] [file2]: 按行比较文件[file1]和file3]
➜  linux_commands cat test1
hello world!


oh my god!
你是
sss
fff
gagds
➜  linux_commands cat test3
hello world!


oh my god!
你是

ss
➜  linux_commands diff test1 test3
2c2    (这里表示test1的第2行与test3的第2行比较,c表示compare,由于是空格不同所以看起来没差异)
<
---
>
6,8c6,7    (这里表示test1的第6-8行和test2的第6-7行比较)
< sss
< fff
< gagds
---
>
> ss
  • diff -w [file1] [file2]: 忽略空格的文件对比
➜  linux_commands diff -w test1 test3
6,8c6,7
< sss
< fff
< gagds
---
>
> ss
  • diff -y [file1] [file2]: 将文件并排对比
➜  linux_commands diff -y test1 test3
hello world!							hello world!
							      |

oh my god!							oh my god!
你是								你是
sss							      |
fff							      |	ss
gagds							      <
  • diff -u [file1] [file2]: 以统一格式(同git diff)展示对比结果
➜  linux_commands diff -u test1 test3
--- test1	2020-04-04 22:24:07.000000000 +0800
+++ test3	2020-04-04 22:25:51.000000000 +0800
@@ -1,8 +1,7 @@
 hello world!
-
+

 oh my god!
 你是
-sss
-fff
-gagds
+
+ss
  • diff -y [file1] [file2] | cat >> [diff_file]: 将文件[file1]和[file2]的对比结果写入文件[diff_file]中
➜  linux_commands diff -y test1 test3 | cat >> "diff.txt"
➜  linux_commands cat diff.txt
hello world!							hello world!
							      |

oh my god!							oh my god!
你是								你是
sss							      |
fff							      |	ss
gagds							      <
  • diff -r [dir1] [dir2]: 展示目录[dir1]和[dir2]的对比结果
  • diff -rq [dir1] [dir2]: 只展示目录[dir1]和[dir2]的文件名对比结果
➜  linux_commands diff -r tt ut
Only in tt: rf
Only in tt: ss
diff -r tt/test2 ut/test2
1c1,2
< s
---
>
> ss

➜  linux_commands diff -rq tt ut
Only in tt: rf
Only in tt: ss
Files tt/test2 and ut/test2 differ
diff: tt/test3: No such file or directory

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值