Linux 一切皆文件。个人理解 在linux下的命令操作都算是对文件操作
那么文件管理命令类命令可以分为下面几类
目录操作:
特殊目录解释: . 代表此层目录;.. 代表上一层目录;- 代表前一个目录;~ 代表当前用户的主文件夹 也可以称为家目录;
cd (change directory)切换目录
例:
[root@localhost ~]# cd ~fool
表示 切换到fool这个用户的家目录下 即 /home/fool
[root@localhost fool]# cd ~
切回到自己的主文件夹,/root目录下
[root@localhost ~]# cd
不加参数,表示切到自己的主目录下 /root
[root@localhost ~]# cd ..
表示且到目前的上层目录 即 /root 目录
[root@localhost /]# cd –
/root
表示回到刚才的目录 /root
[root@localhost ~]# cd /usr/share/zoneinfo/
使用绝对路径
[root@localhost zoneinfo]# cd ../zsh
使用相对路径(zsh 与zoneinfo 都是/usr/share/ 目录下)
pwd (print working directory)显示当前目录;
[root@localhost mail]# pwd
/var/mail
[root@localhost mail]# pwd -P
/var/spool/mail
/var
[root@localhost mail]# ls -ld /var/mail
lrwxrwxrwx. 1 root root 10 9月 16 10:09 /var/mail -> spool/mail
注意 –P参数为大写的P 大写P 大写P 好的,三遍
mkdir (make directory)新建一个新的目录, -p 表示递归创建目录
[root@localhost ~]# mkdir -p study1/study2/study3
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg study1
[root@localhost ~]# ls study1/
study2
[root@localhost ~]# ls study1/study2/
study3
-m 表示配置文件的权限
[root@localhost ~]# mkdir -m 700 study
[root@localhost ~]# ls -l
总用量 8
-rw——-. 1 root root 1851 9月 16 10:24 anaconda-ks.cfg
-rw——-. 1 root root 1899 9月 16 10:35 initial-setup-ks.cfg
drwx——. 2 root root 6 9月 21 22:36 study
drwxr-xr-x. 3 root root 19 9月 21 22:33 study1
注意:默认创建的文件属性为755
rmdir 删除一个”空”的目录
这个命令… 有个参数-p 递归删除目录 必须都是空目录;不好玩
介绍个好玩的命令 删除目录不管有没有内容都可以删除,
rm –rf 可以试试 温馨提示 数据无价 提前备份
查看文件目录
Ls (list)查看目录与文件 这个太常用了
-a 列出全部文件,包括隐藏文件(开头为.的文件);
-A列出全部文件,包括隐藏文件(不包括 . .. 这两目录)
-d 列出目录本身
-l 列出长数据串,包括文件的属性与权限等数据
[root@localhost ~]# ls -d /usr/bin/
/usr/bin/
[root@localhost ~]# ls -ld /usr/bin/
dr-xr-xr-x. 2 root root 45056 9月 16 10:19 /usr/bin/
复制、删除、移动 操作
cp (copy) 复制文件或目录
-a 保留源文件属性复制;
-i 若目标文件已存在,覆盖时先询问用户;
-r 递归持续复制;
[root@localhost ~]# ls -ld study1/study2/
drwxr-xr-x. 3 root root 19 9月 21 22:33 study1/study2/
[root@localhost ~]# cp -a study1/study2/ study
[root@localhost ~]# ls -ld study/study2
drwxr-xr-x. 3 root root 19 9月 21 22:33 study/study2
rm 移除文件或目录
-f:force,忽略不存在的文件,不出现警告信息;
-i:交互模式,删除前会询问用户是否操作;
-r:递归删除。常用在目录删除;
[root@localhost /]# rm -i /usr/123.txt
rm:是否删除普通文件 “/usr/123.txt”?y
[root@localhost /]# ls /usr
bin etc games include lib lib64 libexec local sbin share src tmp
[root@localhost /]# rm -f /usr/123.txt
[root@localhost /]# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
[root@localhost usr]# rm -r test1/test2/test3/
rm:是否删除目录 “test1/test2/test3/”?y
mv 一段文件与目录,或更名
-f:force 如果目标文件已经存在,不会询问而直接覆盖;
-i:若目标文件存在,就询问是否覆盖;
-u:若目标文件存在,且源文件较新才更新;
[root@localhost usr]# mv -f test1/test2/123.txt movefile
[root@localhost usr]# ls movefile/
123.txt
[root@localhost usr]# cat movefile/123.txt
hello world ~!!!
[root@localhost usr]# mv -u test1/test2/123.txt movefile
mv:是否覆盖”movefile/123.txt”? y
[root@localhost usr]# cat movefile/123.txt
hello
halo
haha
[root@localhost usr]# mv -i test1/test2/123.txt movefile
mv:是否覆盖”movefile/123.txt”?
获取路径的文件名与目录名称
basename 获取最后的文件名
[root@localhost study]# basename /usr/movefile/123.txt
123.txt
dirname 获取目录名
[root@localhost study]# dirname /usr/movefile/123.txt
/usr/movefile
文件内容查看
cat :concatenate
-n 打印并显示行号,
[root@localhost study]# cat -n /usr/movefile/123.txt
1 hello
2
3 halo
4 haha
tac 反向查看
[root@localhost study]# tac /usr/movefile/123.txt
haha
halo
hello
‘
more 翻页查看 空格:代表向下翻一页;enter 向下滚动一行;/字符 查找该字符; :f 离开显示处文件名以及目前显示的行数; q 离开离开more,不在显示该文件内容; b& ctrl+b 往回翻页 只对文件有效。
head 取出前面几行 默认前10行 head –n 文件
tail 取出后面几行 tail [-n number] 文件
touch [OPTION]… FILE… 修改文件时间或创建新文件
-c: 指定的文件路径不存在时不予创建;
-a: 仅修改access time;
-m:仅修改modify time;
-t STAMP [[CC]YY]MMDDhhmm[.ss]
which 寻找“执行文件”
Which –a command :将所有由path目录中找到的命令均列出,而不只是列出第一个命令名称
Whereis 寻找特定文件
-b 二进制格式文件
-m 只找在说明文件路径下的文件