04—Linux文件和目录(上)

linux快捷键

  • ctrl+c 取消当前运行的操作
  • ctrl+l 清屏
  • ctrl+a 将光标移动到行首
  • ctrl+e 将光标移动到行尾
  • ctrl+u 剪切光标到行首的内容
  • ctrl+y 粘贴

绝对路径和相对路径

  • 绝对路径:从根目录开始
  • 相对路径:不是从根目录开始,从当前路径开始的

1、pwd:显示当前所在目录

[root@linux-yunwei01 network-scripts]# pwd
/etc/sysconfig/network-scripts

2、cd :切换目录/进入到目录中(change directory)

  • cd ~ 回到当前用户的家目录
[root@linux-yunwei01 network-scripts]# cd ~
[root@linux-yunwei01 ~]# 
  • cd . 保持当前目录不变
[root@linux-yunwei01 ~]#  cd /etc/sysconfig/network-scripts/
[root@linux-yunwei01 network-scripts]# cd .
[root@linux-yunwei01 network-scripts]# 
  • cd .. 回到上级目录
[root@linux-yunwei01 ~]#  cd /etc/sysconfig/network-scripts/
[root@linux-yunwei01 network-scripts]# cd ..
[root@linux-yunwei01 sysconfig]# 
  • cd - 进入到上一次所在目录
[root@linux-yunwei01 network-scripts]# cd ..
[root@linux-yunwei01 sysconfig]# cd -
/etc/sysconfig/network-scripts
[root@linux-yunwei01 network-scripts]# 

3、mkdir创建目录(make directory)

  • 创建一层目录
[root@linux-yunwei01 ~]# mkdir lianxi/fuxi
  • 创建多层目录
    参数:-p 递归创建目录 可以连续创建多个目录
[root@linux-yunwei01 ~]# mkdir -p lianxi/fuxi/aa/bb/cc
[root@linux-yunwei01 ~]# cd lianxi/fuxi/aa/bb/cc
[root@linux-yunwei01 cc]# 

连续创建多个目录

[root@linux-yunwei01 ~]# mkdir -p lianxi/fuxi/aa/bb/11 lianxi/fuxi/aa/bb/22 lianxi/fuxi/aa/bb/33
[root@linux-yunwei01 ~]# ls -l lianxi/fuxi/aa/bb/
total 0
drwxr-xr-x 2 root root  6 Mar 28 19:25 11
drwxr-xr-x 2 root root  6 Mar 28 19:25 22
drwxr-xr-x 2 root root  6 Mar 28 19:25 33
drwxr-xr-x 3 root root 20 Mar 28 19:02 cc
[root@linux-yunwei01 ~]# ^C
[root@linux-yunwei01 ~]# 
[root@linux-yunwei01 ~]# ls -l lianxi/fuxi/aa/
total 0
drwxr-xr-x 6 root root 46 Mar 28 19:25 bb
[root@linux-yunwei01 ~]# mkdir -p lianxi/fuxi/aa/{01..05}
[root@linux-yunwei01 ~]# ls -l lianxi/fuxi/aa
total 0
drwxr-xr-x 2 root root  6 Mar 28 19:31 01
drwxr-xr-x 2 root root  6 Mar 28 19:31 02
drwxr-xr-x 2 root root  6 Mar 28 19:31 03
drwxr-xr-x 2 root root  6 Mar 28 19:31 04
drwxr-xr-x 2 root root  6 Mar 28 19:31 05
drwxr-xr-x 6 root root 46 Mar 28 19:25 bb
[root@linux-yunwei01 ~]# 

4、ls 显示目录内容

  • ls 查看当前目录下的内容
[root@linux-yunwei01 ~]# cd lianxi/fuxi/aa/bb/
[root@linux-yunwei01 bb]# ls
cc
[root@linux-yunwei01 bb]# 
  • -l 以列表方式显示文件的详细信息
[root@linux-yunwei01 bb]# ls
cc
[root@linux-yunwei01 bb]# ls -l
total 0
drwxr-xr-x 3 root root 20 Mar 28 19:02 cc
[root@linux-yunwei01 bb]# 

5、touch创建文件或更改文件时间戳

[root@linux-yunwei01 ~]# touch lianxi/fuxi/fuxi-01.txt
[root@linux-yunwei01 ~]# ls -l lianxi/fuxi/
total 0
drwxr-xr-x 8 root root 66 Mar 28 19:31 aa
-rw-r--r-- 1 root root  0 Mar 28 19:33 fuxi-01.txt
[root@linux-yunwei01 ~]# 
[root@linux-yunwei01 ~]# touch lianxi/fuxi/fuxi-{01..05}.txt
[root@linux-yunwei01 ~]# ls -l lianxi/fuxi/
total 0
drwxr-xr-x 8 root root 66 Mar 28 19:31 aa
-rw-r--r-- 1 root root  0 Mar 28 19:38 A.txt
-rw-r--r-- 1 root root  0 Mar 28 19:38 B.txt
-rw-r--r-- 1 root root  0 Mar 28 19:38 C.txt
-rw-r--r-- 1 root root  0 Mar 28 19:38 D.txt
-rw-r--r-- 1 root root  0 Mar 28 19:38 E.txt
-rw-r--r-- 1 root root  0 Mar 28 19:45 fuxi-01.txt
-rw-r--r-- 1 root root  0 Mar 28 19:45 fuxi-02.txt
-rw-r--r-- 1 root root  0 Mar 28 19:45 fuxi-03.txt
-rw-r--r-- 1 root root  0 Mar 28 19:45 fuxi-04.txt
-rw-r--r-- 1 root root  0 Mar 28 19:45 fuxi-05.txt
[root@linux-yunwei01 ~]# 

6、echo输出信息到屏幕(默认到屏幕)

[root@linux-yunwei01 ~]# echo lianxi/fuxi
lianxi/fuxi

7、cp 复制文件或目录

cp命令的语法:
cp 源文件 目标文件

[root@linux-yunwei01 ~]# ls lianxi/01/
aaaa.txt
[root@linux-yunwei01 ~]# cp lianxi/01/aaaa.txt  lianxi/
[root@linux-yunwei01 ~]# ll lianxi/
total 0
drwxr-xr-x 2 root root  22 Mar 28 17:46 01
drwxr-xr-x 2 root root   6 Mar 28 17:35 02
-rw-r--r-- 1 root root   0 Mar 28 20:03 aaaa.txt
drwxr-xr-x 3 root root 176 Mar 28 19:45 fuxi
[root@linux-yunwei01 ~]# 
  • 参数:
    -r 递归式复制目录,及目录下的所有子文件
    注:cp默认无法复制目录,但如果一定要复制目录,就用-r这个参数
[root@linux-yunwei01 ~]# cp -r lianxi/01/  lianxi/fuxi/
[root@linux-yunwei01 ~]# ll lianxi/fuxi/
total 0
drwxr-xr-x 2 root root 22 Mar 28 20:28 01
drwxr-xr-x 8 root root 66 Mar 28 19:31 aa
-rw-r--r-- 1 root root  0 Mar 28 19:38 A.txt
-rw-r--r-- 1 root root  0 Mar 28 19:38 B.txt
-rw-r--r-- 1 root root  0 Mar 28 19:38 C.txt
-rw-r--r-- 1 root root  0 Mar 28 19:38 D.txt
-rw-r--r-- 1 root root  0 Mar 28 19:38 E.txt
-rw-r--r-- 1 root root  0 Mar 28 19:45 fuxi-01.txt
-rw-r--r-- 1 root root  0 Mar 28 19:45 fuxi-02.txt
-rw-r--r-- 1 root root  0 Mar 28 19:45 fuxi-03.txt
-rw-r--r-- 1 root root  0 Mar 28 19:45 fuxi-04.txt
-rw-r--r-- 1 root root  0 Mar 28 19:45 fuxi-05.txt
[root@linux-yunwei01 ~]# ll lianxi/fuxi/01/
total 0
-rw-r--r-- 1 root root 0 Mar 28 20:28 aaaa.txt
[root@linux-yunwei01 ~]# 

注:cp还有备份的作用,如图:

[root@linux-yunwei01 ~]# ll lianxi/fuxi/01/
total 0
-rw-r--r-- 1 root root 0 Mar 28 20:28 aaaa.txt
[root@linux-yunwei01 ~]# ls -l lianxi/fuxi/01/
total 0
-rw-r--r-- 1 root root 0 Mar 28 20:28 aaaa.txt
[root@linux-yunwei01 ~]# cp lianxi/fuxi/01/aaaa.txt lianxi/fuxi/01/aaaa.txt.bak
[root@linux-yunwei01 ~]# ls -l lianxi/fuxi/01/
total 0
-rw-r--r-- 1 root root 0 Mar 28 20:28 aaaa.txt
-rw-r--r-- 1 root root 0 Mar 28 20:38 aaaa.txt.bak
[root@linux-yunwei01 ~]# 

8、{}生成序列

[root@linux-yunwei01 ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@linux-yunwei01 ~]# 
[root@linux-yunwei01 ~]# echo fuxi{01..10}
fuxi01 fuxi02 fuxi03 fuxi04 fuxi05 fuxi06 fuxi07 fuxi08 fuxi09 fuxi10

9、练习题

pwd cd mkdir ls 练习题: 根据描述进行操作(发出操作过程)

创建/data  和 /你的名字  (拼音的目录)  
进入到/data目录,然后再进入 /你名字的目录
进入到上一次所在目录
回老家

[root@oldboyedu59 ~]# mkdir   /data/   /lidao  
[root@oldboyedu59 ~]# ls -l /data   /lidao/
/data:
total 0

/lidao/:
total 0
[root@oldboyedu59 ~]# cd /data/
[root@oldboyedu59 data]# cd /lidao/
[root@oldboyedu59 lidao]# cd -
/data
[root@oldboyedu59 data]# cd
#mkdir与touch题目:
#创建 /oldboy/alex/lidao  和 /data/alex/lidao 两个目录 
#在/oldboy/alex/lidao 和 /data/alex/lidao 都创建一个叫oldboy.txt文件 


[root@oldboyedu59 ~]# mkdir -p   /oldboy/alex/lidao   /data/alex/lidao 
[root@oldboyedu59 ~]# touch /oldboy/alex/lidao/oldboy.txt  /data/alex/lidao/oldboy.txt
[root@oldboyedu59 ~]# ls -l  /oldboy/alex/lidao/    /data/alex/lidao/
/data/alex/lidao/:
total 0
-rw-r--r--. 1 root root 0 Mar 28 11:58 oldboy.txt

/oldboy/alex/lidao/:
total 0
-rw-r--r--. 1 root root 0 Mar 28 11:58 oldboy.txt
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容