Linux-文件和目录

查看路径
pwd显示当前目录的路径

[root@localhost 203312032134]# pwd
/home/203312032134
[root@localhost 203312032134]#

which
查看命令的可执行文件所在路径, Linux 下,每一条命令其实都对应一个可执行程序,在终端中输入命令,按回车的时候,就是执行了对应的那个程序, which 命令本身对应的程序也存在于 Linux 中。

总的来说一个命令就是一个可执行程序。

[root@localhost 203312032134]# which cd
/usr/bin/cd
[root@localhost 203312032134]# which ps
/usr/bin/ps

浏览和切换目录

ls列出文件和目录,它是 Linux 最常用的命令之一。

【常用参数】
-a 显示所有文件和目录包括隐藏的
-l 显示详细列表
-h 适合人类阅读的
-t 按文件最近一次修改时间排序
-i 显示文件的 inode ( inode 是文件内容的标识)

演示效果:

[root@localhost 203312032134]# ls -a
.  ..  1.zip  a.txt  b.txt  c.txt  d.txt
[root@localhost 203312032134]# ls -l
total 4
-rw-r--r--. 1 root root 574 Dec  3 20:48 1.zip
-rw-r--r--. 1 root root   0 Dec  3 20:35 a.txt
-rw-r--r--. 1 root root   0 Dec  3 20:35 b.txt
-rw-r--r--. 1 root root   0 Dec  3 20:35 c.txt
-rw-r--r--. 1 root root   0 Dec  3 20:38 d.txt
[root@localhost 203312032134]# ls -h
1.zip  a.txt  b.txt  c.txt  d.txt
[root@localhost 203312032134]# ls -t
1.zip  d.txt  c.txt  b.txt  a.txt
[root@localhost 203312032134]# ls -i
5642 1.zip  5639 a.txt  5640 b.txt  5641 c.txt  5643 d.txt
[root@localhost 203312032134]#

cd
cd 是英语 change directory 的缩写,表示切换目录。

cd / --> 跳转到根目录
cd ~ --> 跳转到家目录
cd .. --> 跳转到上级目录
cd ./home --> 跳转到当前目录的home目录下
cd /home/lion --> 跳转到根目录下的home目录下的lion目录
cd --> 不添加任何参数,也是回到家目录

[注意] 输入cd /ho + 单次 tab 键会自动补全路径 + 两次 tab 键会列出所有可能的目录列表。

du
列举目录大小信息。

【常用参数】
-h 适合人类阅读的;
-a 同时列举出目录下文件的大小信息;
-s 只显示总计大小,不显示具体信息。

[root@localhost 203312032134]# du -h
4.0K    .
[root@localhost 203312032134]# du -a
4       ./1.zip
0       ./a.txt
0       ./b.txt
0       ./c.txt
0       ./d.txt
4       .
[root@localhost 203312032134]# du -s
4       .
[root@localhost 203312032134]#

浏览和创建文件

cat一次性显示文件所有内容,更适合查看小的文件。

【常用参数】
-n 显示行号。

less 分页显示文件内容,更适合查看大的文件。

【快捷操作】
空格键:前进一页(一个屏幕);
b 键:后退一页;
回车键:前进一行;
y 键:后退一行;
上下键:回退或前进一行;
d 键:前进半页;
u 键:后退半页;
q 键:停止读取文件,中止 less 命令;
= 键:显示当前页面的内容是文件中的第几行到第几行以及一些其它关于本页内容的详细信息;
h 键:显示帮助文档;
/ 键:进入搜索模式后,按 n 键跳到一个符合项目,按 N 键跳到上一个符合项目,同时也可以输入正则表达式匹配。

head
显示文件的开头几行(默认是10行)

【参数】
-n 指定行数

tail
显示文件的结尾几行(默认是10行)

【参数】
-n 指定行数 tail a.txt -n 2
-f 会每过1秒检查下文件是否有更新内容,也可以用 -s 参数指定间隔时间 tail -f -s 4 a.txt

touch
创建一个文件

[root@localhost 203312032134]# ls
1.zip  a.txt  b.txt  c.txt  d.txt
[root@localhost 203312032134]# touch e.txt
[root@localhost 203312032134]# ls
1.zip  a.txt  b.txt  c.txt  d.txt  e.txt

mkdir
创建一个目录

【常用参数】
-p 递归的创建目录结构

[root@localhost 203312032134]# mkdir -p a/b/c/d
[root@localhost 203312032134]# cd a/b/c
[root@localhost c]# cd d
[root@localhost d]# pwd
/home/203312032134/a/b/c/d
[root@localhost d]# 

文件的复制和移动

cp
拷贝文件和目录

【常用参数】
-r 递归的拷贝,常用来拷贝一整个目录

cp file file_copy --> file 是目标文件,file_copy 是拷贝出来的文件
cp file one --> 把 file 文件拷贝到 one 目录下,并且文件名依然为 file
cp file one/file_copy --> 把 file 文件拷贝到 one 目录下,文件名为file_copy
cp *.txt folder --> 把当前目录下所有 txt 文件拷贝到 folder 目录下

mv
移动(重命名)文件或目录,与cp命令用法相似。

mv file one --> 将 file 文件移动到 one 目录下
mv new_folder one --> 将 new_folder 文件夹移动到one目录下
mv *.txt folder --> 把当前目录下所有 txt 文件移动到 folder 目录下
mv file new_file --> file 文件重命名为 new_file

文件的删除和链接

rm
删除文件和目录,由于 Linux 下没有回收站,一旦删除非常难恢复,因此需要谨慎操作

rm new_file--> 删除 new_file 文件rm f1 f2 f3  --> 同时删除 f1 f2 f3 3个文件

【常用参数】
-i 向用户确认是否删除;
-f 文件强制删除;
-r 递归删除文件夹,著名的删除操作 rm -rf 。

软链接

软链接就类似 windows 下快捷方式。

ln -s file1 file2 将file2设置为file1的软连接

[root@localhost 203312032134]# ln -s a.txt a1.txt
[root@localhost 203312032134]# ls -l
total 12
-rw-r--r--. 1 root root  574 Dec  3 20:48 1.zip
drwxr-xr-x. 3 root root   15 Dec  4 20:29 a
lrwxrwxrwx. 1 root root    5 Dec  4 20:39 a1.txt -> a.txt
-rw-r--r--. 1 root root 6166 Dec  4 20:21 a.txt

其实 file2 只是 file1 的一个快捷方式,它指向的是 file1 ,所以显示的是 file1 的内容,但其实 file2 的 inode 与 file1 并不相同。如果我们删除了 file2 的话, file1是不会受影响的,但如果删除 file1 的话, file2 就会变成死链接,因为指向的文件不见了。

你可能感兴趣的:(Linux,Linux系统教程,linux,服务器,运维)