【Linux】文件目录类解析

x33g5p2x  于2022-03-08 转载在 Linux  
字(8.5k)|赞(0)|评价(0)|浏览(388)

文件目录类

pwd显示文件路径指令

ls查看文件指令

cd进入文件指令

mkdir创建目录指令

rmdir删除目录指令

touch创建空文件指令

cp复制指令

rm删除指令

mv移动或重命名指令

cat查看文件内容指令

more指令

less查看指令

echo输出指令

head显示头部指令

tail显示尾部指令

覆盖指令和>>追加指令

当前日历显示 cal

ln符号连接指令

删除软链接rm

history查看历史命令指令

文件目录类

pwd显示文件路径指令

**基本语法:pwd **(功能描述:显示当前工作目录的绝对路径,绝对路径是从跟目录开始的

示例:

** 第一个斜杆表示根目录**

ls查看文件指令

基本语法:ls   [选项]   [目录或文件]

常用选项

-a:显示当前目录所有文件和目录,包括隐藏文件

-l:以列表的方式显示信息

示例:查看目录下的所有信息

 cd进入文件指令

基本语法:cd  参数(功能描述:切换到指定目录)

理解:绝对路径和相对路径 

**cd ~或者 cd **:表示回到字节的家目录,如用户kongchao,使用cd ~会回到/home/kongchao,而root用户cd ~会回到/root

**cd .. ** 表示回到当前目录的上一级目录 (顶层目录是根目录,所以一直cd ..最多只会到根目录/)

示例:
案例1:使用绝对路径切换到root目录(cd /root)

案例二:使用相对路径到/root目录。比如现在位置在 /home/kongchao(cd ../../root这相当于回退两个目录到根目录最后直接访问root)

案例三:表示当前目录的上一级目录(cd ..)

案例四:回到家目录 (cd ~)

mkdir创建目录指令

mkdir指令用户创建目录
基本语法:mkdir  [选项]  要创建的目录

常用选项: -p 创建多级目录

示例:创建一个目录和创建多级目录

 rmdir删除目录指令

rmdir指令删除空目录

基本语法:rmdir [选项]   要删除的空目录(非空目录无法删除)

rm -rf 表示递归删除(慎用)

如:删除/home/Dog(Dog下无内容)和删除Dog2,Dog2下有smallDog文件(rm -rf /home/Dog2)

 touch创建空文件指令

touch指令创建空文件
基本语法 :**touch  文件名称 **

如在/home目录下创建空文件夹hello.txt,(touch /home/hello.txt)

cp复制指令

cp指令拷贝文件到指定目录
基本语法:cp [ 选项 ] source dest

常用选项 -r:递归复制整个文件夹

示例:将text1中的hello.txt复制到text2上

递归复制整个夹就不用加上之后的/hello.txt (cp  -r text1/  text2)已经到了/home目录,如果没到应该写成cp -r /home/text1  text2

使用细节

强制覆盖不提示:\cp -r /home/text1  text2

[root@kongchao02 home]# ls
kongchao  kongchao1  kongchao2  text1  text2
[root@kongchao02 home]# ls text1
hello1.txt  hello.txt
[root@kongchao02 home]# ls text2
[root@kongchao02 home]# cp -r text1/hello.txt  text2
[root@kongchao02 home]# ls text2
hello.txt

 rm删除指令

说明:rm指令移除文件或目录
基本语法:rm  [选项]   要删除的文件或目录

常用选项:

-r :递归删除整个文件夹

-f:强制删除不提示

-i:删除之前提示

示例:删除text1和text2

[root@kongchao02 home]# ls 
kongchao  kongchao1  kongchao2  text1  text2
[root@kongchao02 home]# ls text1
hello1.txt  hello.txt
[root@kongchao02 home]# rm -rf text1/
[root@kongchao02 home]# ls
kongchao  kongchao1  kongchao2  text2
[root@kongchao02 home]# rm -ri text2/
rm:是否进入目录"text2/"? y
rm:是否删除目录 "text2/hello.txt"?y
rm:是否删除目录 "text2/"?y
[root@kongchao02 home]# ls
kongchao  kongchao1  kongchao2

-i提示性删除更安全,但是也更繁琐,-f强制删除不提示,不安全谨慎使用

mv移动或重命名指令

mv是移动文件与目录或重命名

基本语法:
mv  oldName  newName (重命名) 

mv  /temp/movefile   /targetFolder(移动文件 

示例1**:重命名和移动位置**

[root@kongchao02 home]# mkdir text1
[root@kongchao02 home]# mkdir text1/text2
[root@kongchao02 home]# ls 
kongchao  kongchao1  kongchao2  text1
[root@kongchao02 home]# mv text1 newNameText1
[root@kongchao02 home]# ls
kongchao  kongchao1  kongchao2  newNameText1
[root@kongchao02 home]# ls newNameText1/
text2
[root@kongchao02 home]# mkdir text3
[root@kongchao02 home]# ls
kongchao  kongchao1  kongchao2  newNameText1  text3
[root@kongchao02 home]# mv newNameText1  text3
[root@kongchao02 home]# ls
kongchao  kongchao1  kongchao2  text3
[root@kongchao02 home]# ls text3
newNameText1
[root@kongchao02 home]# ls text3/newNameText1
text2

同一路径下为改名,不同路径为移动位置

示例2:移动并起新的名字

[root@kongchao02 home]# ls
kongchao  kongchao1  kongchao2  text3
[root@kongchao02 home]# mv text3/newNameText1  /home/text4
[root@kongchao02 home]# ls
kongchao  kongchao1  kongchao2  text3  text4

整个目录移动:如mv   /home/text3   /temp

 cat查看文件内容指令

cat查看文件内容
基本语法:cat   [选项]  要查看的文件

常用选项:-n  :显示行号

示例:查看hello.java

[root@kongchao02 home]# vim hello.java
[root@kongchao02 home]# ls
hello.java  kongchao  kongchao1  kongchao2  text3  text4
[root@kongchao02 home]# cat -n hello.java
     1	public class Hello{
     2	  public static void main (String[] args){
     3	  System.out.println("hello java");
     4	  
     5	}
     6	
     7	
     8	
     9	
    10	}

** 使用细节**:cat只能浏览文件,而不能修改文件(所以cat安全),为了浏览方便,一般会带上管道命令 | more,作用是把查询到的结果交给more处理,等全部显示完就会退出more
cat -n  /etc/profile |more(交互指令)

如按下空格显示更多

 more指令

more指令是一个基于vi编辑器的文本过滤器,他以全屏幕的方式按页显示文本文件的内容,more指令中内置了若干个快捷键(交互指令)
基本语法:more 要查看的文件

操作说明如图:

less查看指令

less指令用来分屏查看文件内容,它的功能与more指令类似,但是比more指令更加强大,支持各种显示终端。less指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示需要加载的内容,对于显示大型文件具有较高的效率。 
基本语法**:less   要查看的文件**

 echo输出指令

echo输出内容到控制台
基本语法:echo  [选项]   [输出内容]

示例:用echo指令输出环境变量和指定内容

[root@kongchao02 ~]# echo $HOSTNAME
kongchao02
[root@kongchao02 ~]# echo "hello kongchao"
hello kongchao
[root@kongchao02 ~]#

 head显示头部指令

head指令用于显示文件开头部分内容,默认情况下head指令显示文件的前10行内容
基本语法:**head  文件 ** (查看文件头10行内容)

head  -n  15 文件(查看文件头15行内容,可以是任意行数)

示例:查看/etc/profile下前6行

[root@kongchao02 ~]# head -n 6 /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
[root@kongchao02 ~]#

 tail显示尾部指令

tail用于输出文件中尾部的内容,  默认情况下tail显示文件的前10行内容。

基本语法:
tail  文件(查看文件尾部10行内容)

**tail  -n  5  文件   ** (查看文件尾部5行内容)

tail  -f  文件   (实时追踪该文档的所有更新,该文件变化会反馈出来)

[root@kongchao02 ~]# tail /etc/profile
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
[root@kongchao02 ~]# tail -n 5 /etc/profile
    fi
done

unset i
unset -f pathmunge
[root@kongchao02 ~]#

 >覆盖指令和>>追加指令

是覆盖原先的内容,>>是追加到原来的内容之后

基本语法:

**ls -l  > 文件  ** (列表的内容写入到文件中覆盖原先的)

**ls  -al  >> 文件 ** (列表的内容追加到文件的末尾)

cat  文件1  >  文件2(将文件1内容覆盖到文件2中)

echo  "内容"  >>  文件 (将内容追加到文件中)

示例1:将/home目录下的文件列表写入到/home/text.txt中,覆盖写入(ls -l  /home  > /text.txt)如果text.txt不存在则会自动创建

[root@kongchao02 home]# ls -l /home
总用量 24
-rw-r--r--.  1 root      root       107 3月   4 22:22 hello.java
drwx------. 15 kongchao  kongchao  4096 3月   1 20:37 kongchao
drwx------.  5 kongchao1 kongchao1 4096 2月  28 22:08 kongchao1
drwx------.  3 kongchao2 kongchao2 4096 2月  28 22:11 kongchao2
drwxr-xr-x.  2 root      root      4096 3月   3 21:44 text3
drwxr-xr-x.  3 root      root      4096 3月   3 21:27 text4
[root@kongchao02 home]# ls -l /home > text.txt
[root@kongchao02 home]# ls /home
hello.java  kongchao  kongchao1  kongchao2  text3  text4  text.txt
[root@kongchao02 home]# cat /home/text.txt 
总用量 24
-rw-r--r--.  1 root      root       107 3月   4 22:22 hello.java
drwx------. 15 kongchao  kongchao  4096 3月   1 20:37 kongchao
drwx------.  5 kongchao1 kongchao1 4096 2月  28 22:08 kongchao1
drwx------.  3 kongchao2 kongchao2 4096 2月  28 22:11 kongchao2
drwxr-xr-x.  2 root      root      4096 3月   3 21:44 text3
drwxr-xr-x.  3 root      root      4096 3月   3 21:27 text4
-rw-r--r--.  1 root      root         0 3月   4 22:29 text.txt
[root@kongchao02 home]#

示例2:将I am kc追加到hello.java中 

[root@kongchao02 home]# cat /home/hello.java 
public class Hello{
  public static void main (String[] args){
  System.out.println("hello java");  
  }
}
[root@kongchao02 home]# echo "I am kc" >> /home/hello.java
[root@kongchao02 home]# cat /home/hello.java 
public class Hello{
  public static void main (String[] args){
  System.out.println("hello java");  
  }
}
I am kc
[root@kongchao02 home]#

示例3:将文件覆盖 

[root@kongchao02 home]# cd /home/
[root@kongchao02 home]# cat text.txt 
总用量 24
-rw-r--r--.  1 root      root       107 3月   4 22:22 hello.java
drwx------. 15 kongchao  kongchao  4096 3月   1 20:37 kongchao
drwx------.  5 kongchao1 kongchao1 4096 2月  28 22:08 kongchao1
drwx------.  3 kongchao2 kongchao2 4096 2月  28 22:11 kongchao2
drwxr-xr-x.  2 root      root      4096 3月   3 21:44 text3
drwxr-xr-x.  3 root      root      4096 3月   3 21:27 text4
-rw-r--r--.  1 root      root         0 3月   4 22:29 text.txt
[root@kongchao02 home]# cat hello.java > text.txt 
[root@kongchao02 home]# cat text.txt 
public class Hello{
  public static void main (String[] args){
  System.out.println("hello java");  
  }
}
I am kc
[root@kongchao02 home]#

 当前日历显示 cal

[root@kongchao02 ~]# cal
      三月 2022     
日 一 二 三 四 五 六
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

[root@kongchao02 ~]#

ln符号连接指令

In软连接也称符号连接,类似于windows里的快捷方式,主要存放了链接其他文件的路径
基本语法:**ln -s  [原文件或目录]   [软链接名] **  (给原文件创建一个软链接)

**示例:**在/home目录下创建一个软链接myroot,链接到/root目录

[root@kongchao02 ~]# ln -s /root  /home/myroot
[root@kongchao02 ~]# ls /home
hello.txt  kongchao  kongchao1  kongchao2  myroot
[root@kongchao02 ~]# cd /home/myroot/
[root@kongchao02 myroot]# ls
anaconda-ks.cfg       公共  视频  文档  音乐
initial-setup-ks.cfg  模板  图片  下载  桌面
[root@kongchao02 myroot]# pwd
/home/myroot
[root@kongchao02 myroot]#

** 细节说明**:当我们使用pwd指令查看目录是,仍然看到的是软链接所在的目录,而不是指向的目录

 删除软链接rm

rm  软链接路径

[root@kongchao02 myroot]# rm /home/myroot
rm:是否删除符号链接 "/home/myroot"?y
[root@kongchao02 myroot]# ls /home
hello.txt  kongchao  kongchao1  kongchao2
[root@kongchao02 myroot]#

 history查看历史命令指令

history查看已经执行过历史命令,也可以执行历史命令
基本语法:history  [数字]  (查看已经执行过的多少条命令)

查看所有历史命令:history

查看10条历史命令:history  10

执行历史编号的指令:!编号

示例1:查看历史指令过的指令

[root@kongchao02 myroot]# history
    1  gcc -v
    2  ifconfig
    3  reboot
    4  ifconfig
    5  cat /etc/passwd
    6  useradd kongchao1
    7  passwd kongchao1
    8  su kongchao1
    9  clear
   10  ls
   11  vim hello.v
   12  vim /etc/passwd/
   13  useradd kongchao2
   14  passwd kongchao2
   15  clear
   16  cat /etc/passwd
   17  clear
   18  vim /etc/shadow
   19  cat /etc/shadow
   20  clear
   21  cat /etc/group
   22  clear
   23  init 5
   24  init 3
   25  init 5
   26  gcc- v
   27  gcc -v
   28  tree
   29  ls /home/
   30  ls
   31  su kongchao
   32  logout
   33  ifconfig
   34  vim clear
   35  man ls
   36  q
   37  help cd
   38  clear
   39  ls /
   40  cd /
   41  ls
   42  cd home
   43  s
   44  clear
   45  cd /home
   46  ls
   47  cd kongchao
   48  pwd
   49  clear
   50  ls -a
   51  ls -al
   52  ls a
   53  man lc
   54  manls
   55  man ls
   56  clear
   57  ls -al
   58  cd /home
   59  ls
   60  cd kongchao
   61  cd/
   62  cd /
   63  clear
   64  cd /home/kongchao
   65  cd ~
   66  pwd
   67  ls -s /root  /home/myroot
   68  clear
   69  ln -s /root  /home/myroot
   70  ls /home
   71  cd /home/myroot/
   72  ls
   73  pwd
   74  clear
   75  ls /home/myroot/
   76  clear
   77  ls /home
   78  rm /home/myroot/
   79  clear
   80  rm /home/myroot
   81  ls /home
   82  clear
   83  history

示例2:查看指令过的10条指令

[root@kongchao02 myroot]# history 10
   75  ls /home/myroot/
   76  clear
   77  ls /home
   78  rm /home/myroot/
   79  clear
   80  rm /home/myroot
   81  ls /home
   82  clear
   83  history
   84  history 10

示例3:指令对应标号的指令

[root@kongchao02 myroot]# history 10
   75  ls /home/myroot/
   76  clear
   77  ls /home
   78  rm /home/myroot/
   79  clear
   80  rm /home/myroot
   81  ls /home
   82  clear
   83  history
   84  history 10
[root@kongchao02 myroot]# !77
ls /home
hello.txt  kongchao  kongchao1  kongchao2
[root@kongchao02 myroot]#

相关文章