iT邦幫忙

第 12 屆 iThome 鐵人賽

0
自我挑戰組

30天菜鳥學 Linux 系列 第 34

34天-檔案與目錄管理

  • 分享至 

  • xImage
  •  

$PATH 類似於 windows 的 PATH

test@test:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
test@test:~$ sudo -s
[sudo] password for test: 
root@test:/home/test# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

java、python 功能都安裝在上面
像是bin下面有python2、python2.7、python3、python3.8文件夾
我們就可以使用這四個命令

root@test:/home/test# ls /usr/bin | grep python
dh_python2
python2
python2.7
python3
python3.8
root@test:/home/test# python [TAB][TAB]
python2    python2.7  python3    python3.8 

另外學到可以使用 ls 路徑 | wc -l 計算文件夾數量

test@test:~$ ls /usr/bin |  wc -l
1174

另外鳥哥提到的重要概念

如果我有兩個ls指令在不同的目錄中,例如/usr/local/bin/ls與/bin/ls那麼當我下達 ls 的時候,哪個ls會被執行?
${PATH} 裡面哪個目錄先被查詢,則那個目錄下的指令就會被先執行了

為了安全起見,不建議將『.』加入PATH的搜尋目錄中

ls 的 option

test@test:~$ cd /

test@test:/$ ls 
bin  boot  cdrom  dev  etc  home  lib  lib32  lib64  libx32  lost+found  media  mnt  opt  proc  root  run  sbin  snap  srv  swap.img  sys  tmp  usr  var

test@test:/$ ls -all 
total 2097232
drwxr-xr-x  20 root root       4096 Sep 24 05:57 .
drwxr-xr-x  20 root root       4096 Sep 24 05:57 ..
lrwxrwxrwx   1 root root          7 Jul 31 16:28 bin -> usr/bin
drwxr-xr-x   4 root root       4096 Sep 29 06:57 boot
drwxr-xr-x   2 root root       4096 Sep 24 05:50 cdrom
drwxr-xr-x  18 root root       4080 Oct  3 09:28 dev
drwxr-xr-x 100 root root       4096 Oct  6 06:21 etc
drwxr-xr-x   3 root root       4096 Sep 24 06:00 home
lrwxrwxrwx   1 root root          7 Jul 31 16:28 lib -> usr/lib
lrwxrwxrwx   1 root root          9 Jul 31 16:28 lib32 -> usr/lib32
lrwxrwxrwx   1 root root          9 Jul 31 16:28 lib64 -> usr/lib64
lrwxrwxrwx   1 root root         10 Jul 31 16:28 libx32 -> usr/libx32
drwx------   2 root root      16384 Sep 24 05:50 lost+found
drwxr-xr-x   2 root root       4096 Jul 31 16:28 media
drwxr-xr-x   2 root root       4096 Jul 31 16:28 mnt
drwxr-xr-x   2 root root       4096 Jul 31 16:28 opt
dr-xr-xr-x 137 root root          0 Oct  3 09:27 proc
drwx------   6 root root       4096 Sep 28 09:07 root
drwxr-xr-x  29 root root        960 Oct  7 06:06 run
lrwxrwxrwx   1 root root          8 Jul 31 16:28 sbin -> usr/sbin
drwxr-xr-x  10 root root       4096 Oct  2 18:35 snap
drwxr-xr-x   2 root root       4096 Jul 31 16:28 srv
-rw-------   1 root root 2147483648 Sep 24 05:57 swap.img
dr-xr-xr-x  13 root root          0 Oct  3 09:27 sys
drwxrwxrwt  15 root root       4096 Oct  7 06:09 tmp
drwxr-xr-x  14 root root       4096 Jul 31 16:29 usr
drwxr-xr-x  14 root root       4096 Oct  3 08:50 var

# 容量大小人性化轉換
test@test:/$ ls -all -h
total 2.1G
drwxr-xr-x  20 root root 4.0K Sep 24 05:57 .
drwxr-xr-x  20 root root 4.0K Sep 24 05:57 ..
lrwxrwxrwx   1 root root    7 Jul 31 16:28 bin -> usr/bin
drwxr-xr-x   4 root root 4.0K Sep 29 06:57 boot
drwxr-xr-x   2 root root 4.0K Sep 24 05:50 cdrom
drwxr-xr-x  18 root root 4.0K Oct  3 09:28 dev
drwxr-xr-x 100 root root 4.0K Oct  6 06:21 etc
drwxr-xr-x   3 root root 4.0K Sep 24 06:00 home
lrwxrwxrwx   1 root root    7 Jul 31 16:28 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Jul 31 16:28 lib32 -> usr/lib32
lrwxrwxrwx   1 root root    9 Jul 31 16:28 lib64 -> usr/lib64
lrwxrwxrwx   1 root root   10 Jul 31 16:28 libx32 -> usr/libx32
drwx------   2 root root  16K Sep 24 05:50 lost+found
drwxr-xr-x   2 root root 4.0K Jul 31 16:28 media
drwxr-xr-x   2 root root 4.0K Jul 31 16:28 mnt
drwxr-xr-x   2 root root 4.0K Jul 31 16:28 opt
dr-xr-xr-x 136 root root    0 Oct  3 09:27 proc
drwx------   6 root root 4.0K Sep 28 09:07 root
drwxr-xr-x  29 root root  960 Oct  7 06:06 run
lrwxrwxrwx   1 root root    8 Jul 31 16:28 sbin -> usr/sbin
drwxr-xr-x  10 root root 4.0K Oct  2 18:35 snap
drwxr-xr-x   2 root root 4.0K Jul 31 16:28 srv
-rw-------   1 root root 2.0G Sep 24 05:57 swap.img
dr-xr-xr-x  13 root root    0 Oct  3 09:27 sys
drwxrwxrwt  15 root root 4.0K Oct  7 06:09 tmp
drwxr-xr-x  14 root root 4.0K Jul 31 16:29 usr
drwxr-xr-x  14 root root 4.0K Oct  3 08:50 var

# 單純顯示大小
test@test:/$ ls -s
total 2097224
      0 bin          0 dev         0 lib          0 libx32            4 mnt         4 root        4 snap            0 sys        4 var
      4 boot         4 etc         0 lib32       16 lost+found        4 opt         0 run         4 srv             4 tmp
      4 cdrom        4 home        0 lib64        4 media             0 proc        0 sbin  2097156 swap.img        4 usr
test@test:/$ ls -s -h
total 2.1G
   0 bin   4.0K cdrom  4.0K etc      0 lib       0 lib64    16K lost+found  4.0K mnt     0 proc     0 run   4.0K snap  2.1G swap.img  4.0K tmp  4.0K var
4.0K boot     0 dev    4.0K home     0 lib32     0 libx32  4.0K media       4.0K opt  4.0K root     0 sbin  4.0K srv      0 sys       4.0K usr

# 檔案大小作排序
# 還有這些可以用 - ‘none’ - ‘time’ - ‘size’ - ‘extension’ - ‘version’
test@test:/$ ls -s --sort=size
total 2097224
2097156 swap.img          4 cdrom        4 media        4 root        4 tmp        0 dev           0 lib32        0 bin         0 sys
     16 lost+found        4 etc          4 mnt          4 snap        4 usr        0 run           0 lib64        0 lib
      4 boot              4 home         4 opt          4 srv         4 var        0 libx32        0 sbin         0 proc
# 顯示時間
test@test:/$ ls --full-time -h
total 2.1G
lrwxrwxrwx   1 root root    7 2020-07-31 16:28:20.000000000 +0000 bin -> usr/bin
drwxr-xr-x   4 root root 4.0K 2020-09-29 06:57:48.327177055 +0000 boot
drwxr-xr-x   2 root root 4.0K 2020-09-24 05:50:55.484116042 +0000 cdrom
drwxr-xr-x  18 root root 4.0K 2020-10-03 09:28:11.565355300 +0000 dev
drwxr-xr-x 100 root root 4.0K 2020-10-06 06:21:30.827218016 +0000 etc
drwxr-xr-x   3 root root 4.0K 2020-09-24 06:00:22.939278500 +0000 home
lrwxrwxrwx   1 root root    7 2020-07-31 16:28:20.000000000 +0000 lib -> usr/lib
lrwxrwxrwx   1 root root    9 2020-07-31 16:28:20.000000000 +0000 lib32 -> usr/lib32
lrwxrwxrwx   1 root root    9 2020-07-31 16:28:20.000000000 +0000 lib64 -> usr/lib64
lrwxrwxrwx   1 root root   10 2020-07-31 16:28:20.000000000 +0000 libx32 -> usr/libx32
drwx------   2 root root  16K 2020-09-24 05:50:25.000000000 +0000 lost+found
drwxr-xr-x   2 root root 4.0K 2020-07-31 16:28:23.000000000 +0000 media
drwxr-xr-x   2 root root 4.0K 2020-07-31 16:28:23.000000000 +0000 mnt
drwxr-xr-x   2 root root 4.0K 2020-07-31 16:28:23.000000000 +0000 opt
dr-xr-xr-x 132 root root    0 2020-10-03 09:27:53.004000000 +0000 proc
drwx------   6 root root 4.0K 2020-09-28 09:07:39.300224784 +0000 root
drwxr-xr-x  29 root root  960 2020-10-07 06:19:58.582870949 +0000 run
lrwxrwxrwx   1 root root    8 2020-07-31 16:28:20.000000000 +0000 sbin -> usr/sbin
drwxr-xr-x  10 root root 4.0K 2020-10-02 18:35:08.757509943 +0000 snap
drwxr-xr-x   2 root root 4.0K 2020-07-31 16:28:23.000000000 +0000 srv
-rw-------   1 root root 2.0G 2020-09-24 05:57:04.569245966 +0000 swap.img
dr-xr-xr-x  13 root root    0 2020-10-03 09:27:54.612000000 +0000 sys
drwxrwxrwt  15 root root 4.0K 2020-10-07 06:19:58.394870762 +0000 tmp
drwxr-xr-x  14 root root 4.0K 2020-07-31 16:29:15.000000000 +0000 usr
drwxr-xr-x  14 root root 4.0K 2020-10-03 08:50:52.011545348 +0000 var

# 加上雙括號
test@test:/$ ls -Q
"bin"   "cdrom"  "etc"   "lib"    "lib64"   "lost+found"  "mnt"  "proc"  "run"   "snap"  "swap.img"  "tmp"  "var"
"boot"  "dev"    "home"  "lib32"  "libx32"  "media"       "opt"  "root"  "sbin"  "srv"   "sys"       "usr"

# 反轉顯示
test@test:/$ ls -r
var  usr  tmp  sys  swap.img  srv  snap  sbin  run  root  proc  opt  mnt  media  lost+found  libx32  lib64  lib32  lib  home  etc  dev  cdrom  boot  bin

# 顯示每一個子資料夾內檔案
test@test:/lib/apt$ cd /lib/apt
test@test:/lib/apt$ ls -R --full-time -h
.:
total 68K
-rwxr-xr-x 1 root root  39K 2020-05-12 20:02:05.000000000 +0000 apt-helper
-rwxr-xr-x 1 root root  16K 2020-04-09 10:21:07.000000000 +0000 apt.systemd.daily
drwxr-xr-x 2 root root 4.0K 2020-07-31 16:28:57.000000000 +0000 methods
drwxr-xr-x 2 root root 4.0K 2020-07-31 16:28:58.000000000 +0000 planners
drwxr-xr-x 2 root root 4.0K 2020-07-31 16:28:58.000000000 +0000 solvers

./methods:
total 876K
-rwxr-xr-x 1 root root  59K 2020-05-12 20:02:05.000000000 +0000 cdrom
-rwxr-xr-x 1 root root  47K 2020-05-12 20:02:05.000000000 +0000 copy
-rwxr-xr-x 1 root root  51K 2020-05-12 20:02:05.000000000 +0000 file
-rwxr-xr-x 1 root root 127K 2020-05-12 20:02:05.000000000 +0000 ftp
-rwxr-xr-x 1 root root  99K 2020-05-12 20:02:05.000000000 +0000 gpgv
-rwxr-xr-x 1 root root 183K 2020-05-12 20:02:05.000000000 +0000 http
lrwxrwxrwx 1 root root    4 2020-05-12 20:02:05.000000000 +0000 https -> http
-rwxr-xr-x 1 root root 115K 2020-05-12 20:02:05.000000000 +0000 mirror
lrwxrwxrwx 1 root root    6 2020-05-12 20:02:05.000000000 +0000 mirror+copy -> mirror
lrwxrwxrwx 1 root root    6 2020-05-12 20:02:05.000000000 +0000 mirror+file -> mirror
lrwxrwxrwx 1 root root    6 2020-05-12 20:02:05.000000000 +0000 mirror+ftp -> mirror
lrwxrwxrwx 1 root root    6 2020-05-12 20:02:05.000000000 +0000 mirror+http -> mirror
lrwxrwxrwx 1 root root    6 2020-05-12 20:02:05.000000000 +0000 mirror+https -> mirror
-rwxr-xr-x 1 root root  75K 2020-05-12 20:02:05.000000000 +0000 rred
-rwxr-xr-x 1 root root  59K 2020-05-12 20:02:05.000000000 +0000 rsh
lrwxrwxrwx 1 root root    3 2020-05-12 20:02:05.000000000 +0000 ssh -> rsh
-rwxr-xr-x 1 root root  51K 2020-05-12 20:02:05.000000000 +0000 store

./planners:
total 24K
-rwxr-xr-x 1 root root 23K 2020-05-12 20:02:05.000000000 +0000 apt
lrwxrwxrwx 1 root root  15 2020-05-12 20:02:05.000000000 +0000 dump -> ../solvers/dump

./solvers:
total 52K
-rwxr-xr-x 1 root root 27K 2020-05-12 20:02:05.000000000 +0000 apt
-rwxr-xr-x 1 root root 23K 2020-05-12 20:02:05.000000000 +0000 dump
# 獲取檔案名稱、路徑方式
test@test:~$ touch test.txt
test@test:~$ basename test.txt
test.txt
test@test:~$ dirname test.txt
.
# 只顯示前N行資料
test@test:~$ vim demo.txt
test@test:~$ cat demo.txt
1
2
3
4
5
6
7
8
test@test:~$ head -n 5 demo.txt
1
2
3
4
5

# 取得倒數N行資料
test@test:~$ tail -n 3 demo.txt
6
7
8

# 取得N1-N2資料
test@test:~$ head -n 5 demo.txt | tail -n 2
4
5

上一篇
33天-學習 SSH 正確登入的方式
下一篇
35天-研究 touch 命令
系列文
30天菜鳥學 Linux 59
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言