文件目錄指令

  • pwd 顯示當前工作目錄的絕對路徑
[root@localhost ~]# pwd
/root
  • ls [選項] [目錄或文件]

-a :顯示當前目錄所有的文件和目錄,包括隱藏的。

-l:以列表的形式顯示

[root@localhost ~]# ls
公共  視頻  文檔  音樂  anaconda-ks.cfg  initial-setup-ks.cfg
模板  圖片  下載  桌面  Hello.java
[root@localhost ~]# ls -l
總用量 12
drwx------. 2 root root    6 2月  18 08:39 公共
drwx------. 2 root root    6 2月  18 08:39 模板
drwx------. 2 root root    6 2月  18 08:39 視頻
drwx------. 2 root root    6 2月  18 08:39 圖片
drwx------. 2 root root    6 2月  18 08:39 文檔
drwx------. 2 root root    6 2月  18 08:39 下載
drwx------. 2 root root    6 2月  18 08:39 音樂
drwx------. 2 root root    6 2月  18 08:39 桌面
-rw-------. 1 root root 1386 2月  18 08:37 anaconda-ks.cfg
-rw-r--r--. 1 root root  102 2月  18 10:03 Hello.java
-rw-r--r--. 1 root root 1541 2月  18 08:39 initial-setup-ks.cfg
[root@localhost ~]# ls -al
總用量 56
dr-xr-x---. 15 root root 4096 2月  19 03:41 .
dr-xr-xr-x. 17 root root  224 2月  18 08:30 ..
drwx------.  2 root root    6 2月  18 08:39 公共
drwx------.  2 root root    6 2月  18 08:39 模板
drwx------.  2 root root    6 2月  18 08:39 視頻
drwx------.  2 root root    6 2月  18 08:39 圖片
drwx------.  2 root root    6 2月  18 08:39 文檔
drwx------.  2 root root    6 2月  18 08:39 下載
  • cd 切換目錄
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /home/alice/
[root@localhost alice]# pwd
/home/alice
[root@localhost alice]# 

cd ~或cd:回到用戶家目錄

cd …:回到上一級目錄

  • mkdir [選項] 要創建的目錄

-p:創建多級目錄

創建一個目錄:

[root@localhost home]# mkdir cat
[root@localhost home]# ls
alice  Bob  cat  dog

創建多個目錄:

[root@localhost home]# mkdir -p /home/animal/tiger
[root@localhost home]# ls
alice  animal  Bob  cat  dog
[root@localhost home]# cd animal
[root@localhost animal]# ls
tiger
  • rmdir [選項] 要刪除的空目錄
[root@localhost home]# rmdir cat
[root@localhost home]# ls
alice  animal  Bob  dog

rmdir刪除的是空目錄,如果裏面有內容是無法刪除的。

刪除非空目錄:

[root@localhost home]# rmdir animal/
rmdir: 刪除 'animal/' 失敗: 目錄非空
[root@localhost home]# rm -rf /home/animal/
[root@localhost home]# ls
alice  Bob  dog
  • touch 創建一個空文件
[root@localhost home]# touch hello.txt
[root@localhost home]# ls
alice  Bob  dog  hello.txt
  • cp 拷貝指令

cp [選項] source dest

-r:遞歸複製整個文件夾

拷貝單個文件:

[root@localhost home]# ls
alice  Bob  dog  hello.txt
[root@localhost home]# cp hello.txt dog/hello.txt
[root@localhost home]# cd dog
[root@localhost dog]# ls
hello.txt

遞歸拷貝整個文件夾:

[root@localhost home]# cp -r dog Bob
[root@localhost home]# cd Bob
[root@localhost Bob]# ls
dog
[root@localhost Bob]# cd dog/
[root@localhost dog]# ls
a.txt  b.txt  c.txt  hello.txt
  • rm 指移除文件或目錄

rm [選項] 要刪除的文件或目錄

-r:遞歸刪除整個文件夾

-f:強制刪除不提示

[root@localhost home]# ls
alice  Bob
[root@localhost home]# rm -rf Bob
[root@localhost home]# ls
alice
  • mv 移動文件與目錄重命名

mv oldNameFile newNameFile

將dog.txt改成cat.txt:

[root@localhost home]# touch dog.txt
[root@localhost home]# mv dog.txt cat.txt
[root@localhost home]# ls
alice  cat.txt

將/home/cat.txt移動到/root目錄下:

[root@localhost home]# mv cat.txt /root
[root@localhost home]# cd /root
[root@localhost ~]# ls
公共  視頻  文檔  音樂  anaconda-ks.cfg  Hello.java
模板  圖片  下載  桌面  cat.txt          initial-setup-ks.cfg
  • cat 查看文件內容,以只讀的方式打開文件

cat [選項] 要查看到文件

-n:顯示行號

[root@localhost ~]# cat Hello.java 
public class Hello{
        public static void main(String[] args){
                System.out.print('hello world');
        }
}

[root@localhost ~]# cat -n Hello.java 
     1  public class Hello{
     2          public static void main(String[] args){
     3                  System.out.print('hello world');
     4          }
     5  }
     6

| more:分頁顯示,按空格翻頁

[root@localhost ~]# cat -n /etc/profile | more
     1  # /etc/profile
     2
     3  # System wide environment and startup programs, for login set
up
     4  # Functions and aliases go in /etc/bashrc
  • more:以全屏的方式按頁顯示文本文件的內容

空格:向下翻一頁

enter:向下翻一行

q:退出

ctrl+f:向下滾動一屏

ctrl+b:返回上一屏

=:輸出當前行號

:f :輸出文件名和當前行的行號

[root@localhost ~]# more /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
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

  • less用來分屏查看文件內容,並不是一次將整個文件加載之後才顯示。
[root@localhost ~]# less /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
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
  • '>‘輸出重定向,會將原來文件內容和’>>'追加,不會覆蓋原來的內容
[root@localhost home]# ls -l > a.txt
[root@localhost home]# more a.txt
總用量 4
drwx------. 15 alice alice 4096 2月  19 02:34 alice
-rw-r--r--.  1 root  root     0 2月  19 06:16 a.txt
將ls -l顯示的內容寫到a.txt,如果文件不存在,則創建文件,如果文件存在,則覆蓋文件。
[root@localhost home]# ls -l >> a.txt
[root@localhost home]# more a.txt
總用量 4
drwx------. 15 alice alice 4096 2月  19 02:34 alice
-rw-r--r--.  1 root  root     0 2月  19 06:16 a.txt
總用量 8
drwx------. 15 alice alice 4096 2月  19 02:34 alice
-rw-r--r--.  1 root  root   118 2月  19 06:16 a.txt
將ls -l顯示的內容追加到a.txt末尾
[root@localhost home]# echo "hello,world">>  a.txt
[root@localhost home]# more a.txt
總用量 4
drwx------. 15 alice alice 4096 2月  19 02:34 alice
-rw-r--r--.  1 root  root     0 2月  19 06:16 a.txt
總用量 8
drwx------. 15 alice alice 4096 2月  19 02:34 alice
-rw-r--r--.  1 root  root   118 2月  19 06:16 a.txt
hello,world
追加內容到a.txt
[root@localhost home]# more /etc/profile >> a.txt
[root@localhost home]# more a.txt
總用量 4
drwx------. 15 alice alice 4096 2月  19 02:34 alice
-rw-r--r--.  1 root  root     0 2月  19 06:16 a.txt
總用量 8
drwx------. 15 alice alice 4096 2月  19 02:34 alice
-rw-r--r--.  1 root  root   118 2月  19 06:16 a.txt
hello,world
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
將more打開的內容追加到a.txt中
  • echo 輸出內容到控制檯
輸出環境變量
[root@localhost home]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  • head 用於顯示文件到開頭部分,默認顯示前10行

head 文件

head -n 5 文件

[root@localhost home]# head /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
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

[root@localhost home]# head -n 5 /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
  • tail 用於輸出文件中尾部的內容,默認顯示後10行

tail 文件

tail -n 5 文件:查看文件後5行

[root@localhost home]# tail -n 5 /etc/profile
                # Bash non-login shells run only /etc/bashrc
                # Check for double sourcing is done in /etc/bashrc.
                . /etc/bashrc
       fi
fi

tail -f 文件:實時監控文件的更新

  • ln 軟連接指令,類似於windows快捷方式

ln -s 原文件或目錄 軟鏈接名

[root@localhost home]# ln -s /root link_to_root
[root@localhost home]# ls
alice  a.txt  link_to_root
[root@localhost home]# cd link_to_root/
[root@localhost link_to_root]# ls
公共  視頻  文檔  音樂  anaconda-ks.cfg  initial-setup-ks.cfg
模板  圖片  下載  桌面  Hello.java
[root@localhost link_to_root]# pwd
/home/link_to_root

刪除軟連接不要帶斜槓:

[root@localhost home]# rm -rf link_to_root/
[root@localhost home]# ls
alice  a.txt  link_to_root
[root@localhost home]# rm -rf link_to_root
[root@localhost home]# ls
alice  a.txt
  • history 查看已經執行過歷史命令,也可以執行歷史指令

查看所有的歷史命令:

[root@localhost home]# history
    1  ip a
    2  ipconfig
    3  ls -l
    4  ifconfig
    5  ipconfig
    6  ifconfig
    7  ls
    8  ls -l
    9  cd ..

顯示最近使用過的10分指令:

[root@localhost home]# history 10
  205  cd link_to_root/
  206  ls
  207  pwd
  208  cd ..
  209  rm -rf link_to_root/
  210  ls
  211  rm -rf link_to_root
  212  ls
  213  history
  214  history 10

執行206號指令:

[root@localhost home]# !206
ls
alice  a.txt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章