Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 基礎速查:進程、shell 變數、find 與 locate 一次整理

零散但常用的小知識,集中在一頁,需要的時候翻回來看。

查看當前進程與其父進程

$$        # 當前 shell 的 PID
echo $PPID    # 父進程 PID

shell 腳本的 for 迴圈

for ((i=0; i<10; ++i))
do
    echo "hello", $i
done

注意 for 與左括號之間必須有空格,這是許多人第一次寫就卡住的地方。

set、env 與 export 的差別

關鍵前提:每個 shell 都是一個獨立進程。

· set 定義的變數,屬於該 shell 進程私有。

· env 列出的,是所有 shell 進程之間共享的環境變數。

· 要把私有變數變成共享,就得用 export 把它導出。

檔案搜尋:find

find /etc/ -name interfaces

基本格式為 find [路徑] [選項] [動作],路徑是第一個參數,這點和許多指令相反。與時間相關的選項:

-atime   最後訪問時間
-ctime   建立/狀態變更時間
-mtime   最後修改時間

以 -mtime 為例:

-mtime n     n 天前的「一天之內」修改過的檔案
-mtime +n    早於 n 天(不含 n 本身)修改過的檔案
-mtime -n    n 天以內(含 n 本身)修改過的檔案
-newer file  列出比 file 更新的檔案
Linux 基礎速查:進程、shell 變數、find 與 locate 一次整理

實際例子——列出家目錄中 24 小時內有變動的檔案:

find ~ -mtime 0

列出家目錄下比 Code 資料夾新的檔案:

find ~ -newer /home/shiyanlou/Code

whereis、which 與 locate 的分工

找指令相關的檔案(執行檔、man page、原始碼)用 whereis 與 which;找普通檔案則用 locate 與 find。

locate 的速度來自預先建好的索引,代價是可能查到已經不存在的檔案。手動更新索引:

updatedb

一句話總結:要即時準確用 find,要速度用 locate,要找「這個指令在哪」用 which/whereis。

Copyright © Linux教程網 All Rights Reserved