How do I get the name of a folder from a linux find commnad.
如何從linux find commnad獲取文件夾的名稱。
I have a command like this:
我有這樣的命令:
find /root/wgetlog -name -type d -empty
Whic produces the following results:
Whic產生以下結果:
/root/wgetlog/smil3
/root/wgetlog/smil5
/root/wgetlog/smil4
how do I get just the name of the folder:
如何獲得文件夾的名稱:
Example: smil3 smil4 smil5
例如:smil3 smil4 smil5
4 个解决方案
#1
2
find /root/wgetlog -type d -empty -printf "%f\n"
查找/root/wgetlog -type d -empty -printf "%f\n"
#2
1
If all you need is a relative path, then
如果你需要的只是一個相對路徑。
{ pushd /root/wgetlog/; find . -name -type d -empty; popd; }
is the approach, especially if you do care about subdirectories of /root/wgetlog/*.
是方法,特別是如果您確實關心/root/wgetlog/*的子目錄。
#3
0
使用::
find /root/wgetlog -type d -empty -exec basename {} \;
You don't need -name.
你不需要- name。
#4
0
You could also use sed to filter out the leading elements of each path:
您也可以使用sed來過濾每個路徑的主要元素:
$ find /usr/bin -type d
/usr/bin
/usr/bin/multiarch-i386-linux
/usr/bin/multiarch-x86_64-linux
/usr/bin/gda_trml2pdf
/usr/bin/gda_trml2html
...
$ find /usr/bin -type d | sed 's|.*/||'
bin
multiarch-i386-linux
multiarch-x86_64-linux
gda_trml2pdf
gda_trml2html
...
This might be more portable than using the -printf option of find, although that should not be an issue if you stick to Linux.
這可能比使用find的-printf選項更具可移植性,盡管如果您堅持使用Linux,這應該不是問題。
Disclaimer: this will fail horribly if you have newlines in your file/folder names. On the other hand, this snippet is probably not the only thing that would fail in that case...
免責聲明:如果您的文件/文件夾名稱中有新行,這將嚴重失敗。另一方面,這個片段可能不是在這種情況下唯一會失敗的東西……