您的位置:首页 > 运维架构 > Linux

8.linux文件相关命令

2015-12-27 21:10 573 查看
万物皆文件是linux中一个比较重要的思想,文件操作命令是我们日常将会用到的。

cp:复制文件

cp 源 目标

-r/R 递归操作


-f 强制进行复制


-i 提示信息


-p 保留权限


-a 保留所有权限


-L 复制文件而不是链接


-P 保持链接

[root@localhost ~]# 
[root@localhost ~]# mkdir test
[root@localhost ~]# cd test/
[root@localhost test]# touch test
[root@localhost test]# cp -r ../test/  ../test2
[root@localhost test]# ls
test
[root@localhost test]# cd ..
[root@localhost ~]# ls
anaconda-ks.cfg  Desktop  install.log  install.log.syslog  test  test2
[root@localhost ~]# tree test2
test2
`-- test

0 directories, 1 file
使用L复制链接的源文件而不是链接本身,毕竟链接也是文件,万物皆文件嘛

[root@localhost test]# ln -s test s_test
[root@localhost test]# ll
total 8
lrwxrwxrwx 1 root root 4 Dec 27 05:19 s_test -> test
-rw-r--r-- 1 root root 0 Dec 27 05:17 test
[root@localhost test]# cp s_test ./no_test
[root@localhost test]# cp -L s_test ./y_test
[root@localhost test]# ll
total 16
-rw-r--r-- 1 root root 0 Dec 27 05:20 no_test
lrwxrwxrwx 1 root root 4 Dec 27 05:19 s_test -> test
-rw-r--r-- 1 root root 0 Dec 27 05:17 test
-rw-r--r-- 1 root root 0 Dec 27 05:20 y_test
[root@localhost test]#
mv 移动文件

mv 源 目标

-f 强制

-i 提示

install 复制文件并指定属性

创建两个目录

[root@localhost test]# install -d ./test2/{aa,bb}
[root@localhost test]# ll
total 24
-rw-r--r-- 1 root root    0 Dec 27 05:20 no_test
lrwxrwxrwx 1 root root    4 Dec 27 05:19 s_test -> test
-rw-r--r-- 1 root root    0 Dec 27 05:17 test
drwxr-xr-x 4 root root 4096 Dec 27 05:25 test2
-rw-r--r-- 1 root root    0 Dec 27 05:20 y_test
[root@localhost test]# ll ./test2
total 16
drwxr-xr-x 2 root root 4096 Dec 27 05:25 aa
drwxr-xr-x 2 root root 4096 Dec 27 05:25 bb


我们希望查看文件的内容,而不对文件进行编辑时使用cat

cat 链接并显示

[root@localhost test2]# cat /etc/issue /etc/fstab 
Red Hat Enterprise Linux Server release 5.8 (Tikanga)
Kernel \r on an \m

LABEL=/                 /                       ext3    defaults        1 1
<span style="font-family:Courier New;">。。。。。。</span>
-n 显示行号

-E 显示结束符(linux中的结束符为$,windows为$\n)

-T 显示制表符

-V 显示非打印符

-A 显示所有的特殊符号

这里说明一下,有时候我们命令执行到一半卡住了,可以使用组合键ctrl+c来结束执行,这个组合键也可以用作命令的不执行。

tac 将文档从后向前显示

cat会将文件一溜烟的全部显示,我们想一部分一部分分页查看可以使用more和less

more file

space 向后翻一页

B 向前翻一页

enter 一行行后翻

less 分屏显示

space 向后一页

B 向前一页

K 向前一行

Enter 一行行向后

head 显示前几行(不指定行数默认为10行)

[root@localhost test2]# head -n 2 /etc/inittab
#
# inittab       This file describes how the INIT process should set up
[root@localhost test2]# head -2 /etc/inittab
#
# inittab       This file describes how the INIT process should set up
tail 显示后几行(不指定行数默认为10行)

[root@localhost test2]# tail -2 /etc/inittab
# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon
他最重要的参数是-f,查看并等待进程追加,常用于日志的追踪

cut 将文本切割成几部分

[root@localhost test2]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
这段文本的意思是将文本按":"切割,并获得第一段

-d 指定字段的分割符,默认为空格

-f 指定要显示的字段

-f 1,3 指1和3行

-f 1-3 指1到3行

wc用于文本的统计

-l 显示行数

-w 显示词数

-c 显示字节数

-m 显示字符数

-L 最长字符数

tr是字符串处理命令,用于转换或删除字符

[root@localhost test2]# tr 'ab' 'AB' < passwd 
root:x:0:0:root:/root:/Bin/BAsh
[root@localhost test2]# tr 'a-z' 'A-Z' < passwd 
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
[root@localhost test2]# tr -d 'ab' < passwd
root:x:0:0:root:/root:/in/sh
原本我的笔记中是有sort和uniq但是我觉得用的少,所以这里没有写上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: