iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 29
2
自我挑戰組

Linux學習系列 第 29

Linux find指令

  • 分享至 

  • twitterImage
  •  

https://blog.gtwang.org/linux/unix-linux-find-command-examples/

find指令是用來找檔案的路徑

先創多個文字檔findtest和多個資料夾findTest來練習

先 find -name findtest
有找到findtest,但是沒找到findTest,表示有分大小寫
然後有很多permission denied
所以檢查權限,只要有permission denied是沒辦法透過ls來看權限的
https://ithelp.ithome.com.tw/upload/images/20181111/20111994KOG4m26EQc.png
所以就先來查看dir4的權限,因為user權限只有read,代表這個資料夾裡的權限只有看的到檔名,所以ls才一堆問號,切換到root就可以看到權限內容了
https://ithelp.ithome.com.tw/upload/images/20181111/20111994mVW7qbnFxB.png
所以接下來就換到root來使用find
iname代表不分大小寫
-type d代表資料夾
-type f代表檔案
https://ithelp.ithome.com.tw/upload/images/20181111/20111994WhilJR3JCl.png

除了找檔名外,也可以找權限,像要找電腦是000權限的檔案
可以看到有這些檔案,還有些是no such file or directory 或是 Permission denied
先看Permission denied的檔案是怎麼回事,回到前面的資料夾查看gvfs
都是?號
https://ithelp.ithome.com.tw/upload/images/20181111/20111994h9iwVkSZj3.png
所以來了解下gvfs到底是什麼:
https://blog.csdn.net/codemacket/article/details/77864770
https://unix.stackexchange.com/questions/77453/why-cannot-find-read-run-user-1000-gvfs-even-though-it-is-running-as-root

there's nothing to fix. /run/user/$uid/gvfs or ~$user/.gvfs is the mount point for the FUSE interface to GVFS. GVFS is a virtual filesystem implementation for Gnome, which allows Gnome applications to access resources such as FTP or Samba servers or the content of zip files like local directories. FUSE is a way to implement filesystem drivers as user code (instead of kernel code). The GVFS-FUSE gateway makes GVFS filesystem drivers accessible to all applications, not just the ones using Gnome libraries.

FUSE是Filesystem in Userspace,就是在使用者空間的檔案系統(就是平常可以看到檔名、目錄都是因為檔案系統)

那什麼是使用者空間?應該就是user mode。就是kernel有很多種: Monolithic kernel的檔案系統就會在kernel mode而不會在user mode,而Microkernel的檔案系統就會在user mode。而linux作業系統通常是Monolithic kernel,所以檔案系統就會在kernel mode。

所以為什麼會有Filesystem in Userspace,因為檔案系統的驅動程式要在user mode(implement filesystem drivers as user code (instead of kernel code))。

驅動程式是什麼?
https://www.youtube.com/watch?v=z8vWxk3iY4M
電腦主要有這三層:應用層(平常使用的)、作業系統層(kernel、shell)、硬體層(實體的,CPU、硬碟之類的)
層與層之間怎麼溝通?就是由驅動程式來溝通
像顯卡驅動程式,用來讓作業系統(windows)跟硬體(顯卡)溝通,而這邊的driver應該是讓應用層(就是user mode,平常用電腦使用的畫面)跟作業系統(linux的filesystem)溝通

GVFS就是GNOME Virtual file system,Virtual file system不等於file system,所以來了解Virtual file system
虛擬檔案系統(英語:Virtual file system,縮寫為VFS)
又稱虛擬檔案切換系統(virtual filesystem switch)
https://zh.wikipedia.org/wiki/%E8%99%9B%E6%93%AC%E6%AA%94%E6%A1%88%E7%B3%BB%E7%B5%B1
所以VFS應該就是檔案系統和使用者之間溝通的橋樑,。就像kernel是軟體和硬體的溝通橋樑

在來了解什麼是Gnome

GNOME是一個完全由自由軟體組成的桌面環境。它的目標作業系統是Linux。

所以意思應該就是. /run/user/$uid/gvfs or ~$user/.gvfs這些路徑是代表FUSE (Filesystem in Userspace)interface有個GVFS(GNOME Virtual file system)的掛載點(可以讀或寫檔案系統),GVFS用來allows Gnome applications to access resources such as FTP or Samba servers or the content of zip files like local directories,而FUSE用來implement filesystem drivers as user code

所以GVFS-FUSE用來:GVFS filesystem drivers accessible to all applications, not just the ones using Gnome libraries.

接著來看為什麼就算是root還是不能看到檔案權限

Managing trust boundaries with FUSE filesystems is difficult, because the filesystem driver is running as an unprivileged user, as opposed to kernel code for traditional filesystems. To avoid complications, by default, FUSE filesystems are only accessible to the user running the driver process. Even root doesn't get to bypass this restriction.

管理FUSE filesystems是很麻煩的。因為filesystem driver是任何使用者都可以使用。所以管理方式就是只允許執行driver的使用者可以使用FUSE filesystems,就算是root也不能使用FUSE filesystems

雖然沒有很懂,不過先跳過吧,再來了解為什麼會有no such file or directory
解決方法,但還不知道為什麼:
https://superuser.com/questions/393812/supresss-the-no-such-file-or-directory-message-from-find
2>/dev/null 把錯誤丟進黑洞
| tee log 把檔案存到一個叫log的文字檔,log的文字檔不會顯示錯誤的資訊
https://ithelp.ithome.com.tw/upload/images/20181111/20111994yCNwkGCUqe.png

-exec 可以將搜尋出來的結果,使用其他的指令進行後續的處理動作
像是可以先透過find來找檔名findtest,然後exec再接grep找有find文字的,但是這樣只會顯示檔案內容,不會顯示檔案位置,所以用grep –H

接著可以找全部檔案而且內容裡有apache2文字的檔案
https://ithelp.ithome.com.tw/upload/images/20181111/20111994Omk1LvAWMS.png

find exec 跟 find | xargs 相似的意思,但是順序好像不同,不知道為什麼
有關xargs:
https://blog.gtwang.org/linux/xargs-command-examples-in-linux-unix/
https://ithelp.ithome.com.tw/upload/images/20181111/20111994njHV5rwRSg.png


上一篇
Linux Questions
下一篇
Linux證照
系列文
Linux學習30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言