shell获取目录的上级目录_Shell:如何获取文件指定的父目录的名称?(Shell: How to a file's specified parent directory's name?)...
Shell:如何獲取文件指定的父目錄的名稱?(Shell: How to a file's specified parent directory's name?)
我是Shell編程的初學(xué)者。 我目前正在編寫一個(gè)腳本來操作找到的文件。 但我需要獲取已找到文件的父目錄名稱。 例如,
SEARCH_PATH=/home/test
for file in `find $SEARCH_PATH -name "pattern"`;
do
echo $file;
done
在搜索路徑中有幾個(gè)文件夾有文件模式,
/home/test/type1/log/pattern
/home/test/type2/log/pattern
/home/test/type3/log/pattern
我需要做的是在這些文件中找到'pattern',并將祖父母目錄的名稱改為'type'名稱...
I am a beginner for Shell programming. I am currently writing a script to manipulate found files. but I would be needing to get the parent directories' names for found files. For example,
SEARCH_PATH=/home/test
for file in `find $SEARCH_PATH -name "pattern"`;
do
echo $file;
done
in the search path there are several folders have the file pattern,
/home/test/type1/log/pattern
/home/test/type2/log/pattern
/home/test/type3/log/pattern
What I need to do is to find 'pattern' in those files, and also got the grandparent directory's name to the the 'type' name ...
原文:https://stackoverflow.com/questions/16674974
2019-10-23 18:10
滿意答案
又臟又快:
kent$ dirname $(dirname "/home/test/type1/log/pattern")
/home/test/type1
如果您的文件名中沒有/ ,您也可以使用sed,awk cut ....一個(gè)sed示例:
kent$ echo "/home/test/type1/log/pattern"|sed 's#/[^/]*/[^/]*$##'
/home/test/type1
編輯
僅限類型:
basename&dirname:
kent$ basename $(dirname $(dirname "/home/test/type1/log/pattern"))
type1
用awk:
kent$ echo "/home/test/type1/log/pattern"|awk -F'/' '$0=$(NF-2)'
type1
dirty and quick:
kent$ dirname $(dirname "/home/test/type1/log/pattern")
/home/test/type1
if there is no / in your filename, you could also do with sed, awk cut.... one sed example:
kent$ echo "/home/test/type1/log/pattern"|sed 's#/[^/]*/[^/]*$##'
/home/test/type1
EDIT
type only:
basename & dirname:
kent$ basename $(dirname $(dirname "/home/test/type1/log/pattern"))
type1
with awk:
kent$ echo "/home/test/type1/log/pattern"|awk -F'/' '$0=$(NF-2)'
type1
2013-05-21
相關(guān)問答
鑒于第1部分(通用前綴)的答案,第2部分的答案是直截了當(dāng)?shù)? 您將每個(gè)名稱的前綴分割出來,這可能會(huì)在其他選項(xiàng)中使用sed完成。 那么有趣的部分是找到共同的前綴。 最小公共前綴是/ (例如,用于/etc/passwd和/bin/sh )。 最大公共前綴(按定義)出現(xiàn)在所有字符串中,因此我們只需將其中一個(gè)字符串拆分為段,并將可能的前綴與其他字符串進(jìn)行比較。 概述: split name A into components
known_prefix="/"
for each extra componen...
目錄版本 get-item是你友好的幫助手。 (get-item $scriptPath ).parent.parent
如果你只想要字符串 (get-item $scriptPath ).parent.parent.FullName
文件版本 如果$scriptPath指向一個(gè)文件,那么您必須首先調(diào)用Directory屬性,因此調(diào)用將如下所示 (get-item $scriptPath).Directory.Parent.Parent.FullName
備注 這將僅在$scriptPath...
在R中,相對(duì)文件位置總是相對(duì)于當(dāng)前工作目錄。 你可以像這樣明確地設(shè)置你的工作目錄: setwd("~/some/location")
一旦設(shè)置完成,您可以獲取與當(dāng)前工作目錄相關(guān)的源文件。 source("some_script.R") # In this directory
source("../another_script.R") # In the parent directory
source("folder/stuff.R") # In a child d...
運(yùn)行dirname兩次(嵌套)。 ~$ dirname $PWD
/home
~$ dirname `dirname $PWD`
/
~$
Run dirname twice (nested). ~$ dirname $PWD
/home
~$ dirname `dirname $PWD`
/
~$
訪問URL時(shí),您將無法從Amazon S3獲取目錄列表頁面 。 最接近的是XML對(duì)象列表,您必須為其授予對(duì)桶策略的ListObjects權(quán)限。 它看起來像這樣: 此XML文件似乎沒有與之關(guān)聯(lián)的任何樣式信息。 文檔樹如下所示。
my-bucket
1000
引用File.getParent() javadoc(強(qiáng)調(diào)我的): 返回此抽象路徑名父級(jí)的路徑名字符串,如果此路徑名未命名父目錄,則返回null 。 抽象路徑名的父節(jié)點(diǎn)由路徑名的前綴(如果有)和路徑名名稱序列中除最后一個(gè)以外的每個(gè)名稱組成。 如果名稱序列為空,則路徑名不會(huì)命名父目錄。 請(qǐng)記住, File對(duì)象表示路徑字符串 ,而不是文件系統(tǒng)上的實(shí)際文件。 字符串""沒有父項(xiàng)。 字符串"a/b/c"具有"a/b"作為父項(xiàng),即使它們沒有物理存在。 所以,首先你必須通過調(diào)用getCanonicalFile...
你不能。 也apt 。 軟件包管理器只需將所需的數(shù)據(jù)/變量寫入文件中,然后由程序本身,程序的補(bǔ)丁或包裝器讀取。 好的例子可以在/etc/default/* 。 這些文件具有可變定義,有些甚至可以幫助描述它們的來源: $ cat /etc/default/ssh
# Default settings for openssh-server. This file is sourced by /bin/sh from
# /etc/init.d/ssh.
# Options to pass to ssh...
標(biāo)準(zhǔn)C中沒有這樣的功能。您可以在Windows上嘗試運(yùn)氣:GetFullPathName http://msdn.microsoft.com/en-us/library/aa364963%28v=vs.85%29.aspx 然后也許_splitpath http://msdn.microsoft.com/en-us/library/e737s6tf%28v=vs.80%29.aspx 但是正如所寫的那樣,做這類事情并不是標(biāo)準(zhǔn)功能。 There is no such function in Stan...
這是因?yàn)?對(duì)Make有特殊的含義,所以如果你想把它傳遞給shell,你必須“逃避”它。 如果是Make,你可以通過加倍來逃避美元符號(hào)。 所以你必須使用$$PWD 。 另外,你所做的并不是最好的方法 - 如果可能的話,最好避免使用shell并使用Make功能。 在你的情況下,做你想要的最好的方法是: BUILD_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/../../build)
你必須把上面的行放在make文件中,靠近最上面,這樣它才...
又臟又快: kent$ dirname $(dirname "/home/test/type1/log/pattern")
/home/test/type1
如果您的文件名中沒有/ ,您也可以使用sed,awk cut ....一個(gè)sed示例: kent$ echo "/home/test/type1/log/pattern"|sed 's#/[^/]*/[^/]*$##' ...
相關(guān)文章
Gao Yuanyuan from after 2005 drama " world fir
...
出現(xiàn)bad interpreter:No such file or directory的原因 是文件格
...
這篇文件寫的非常好,推薦大家重溫一下: http://highscalability.com/blog
...
調(diào)用文件系統(tǒng)(FS)Shell命令應(yīng)使用 bin/Hadoop fs <args>的形式。
...
Data Week: Becoming a data scientist Data Pointed,
...
Windowsis an extremely effective and a an efficient
...
hadoop變得越來越熱門,但是hadoop的設(shè)計(jì)是用來處理靜態(tài)數(shù)據(jù)和批處理任務(wù),流處理實(shí)施起來不是很
...
http://www.codeproject.com/Articles/42799/Storm-the
...
Bottega Veneta og?oszenie Fire continues Daleko ali
...
http://spark-project.org/ 項(xiàng)目首頁 http://shark.cs.berk
...
最新問答
如果啟用了復(fù)制處理程序,請(qǐng)確保將其置于其中一個(gè)安全角色之后。 我見過人們做的另一件事是在不同的端口上運(yùn)行admin。 最好在需要auth的頁面上使用SSL,這樣你就不會(huì)發(fā)送明確的密碼,因此管理和復(fù)制將發(fā)生在8443上,而常規(guī)查詢將在8080上發(fā)生。 如果您要簽署自己的證書,請(qǐng)查看此有用的SO頁面: 如何在特定連接上使用不同的證書? I didn't know that /admin was the context for SOLR admin because /admin does not re
第一:在您的樣本中,您有: 但是你在詢問 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大寫'T')。 xpath區(qū)分大小寫。 第二:通過查詢// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一個(gè)'td'元素,而它們是兄弟姐妹。 有很多方法可以在這里獲得制作和模型
這是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;
問題是,在啟用Outlook庫引用的情況下, olMailItem是一個(gè)保留常量,我認(rèn)為當(dāng)您將Dim olMailItem as Outlook.MailItem ,這不是問題,但是嘗試設(shè)置變量會(huì)導(dǎo)致問題。 以下是完整的解釋: 您已將olMailItem聲明為對(duì)象變量。 在賦值語句的右側(cè),在將其值設(shè)置為對(duì)象的實(shí)例之前,您將引用此Object 。 這基本上是一個(gè)遞歸錯(cuò)誤,因?yàn)槟阌袑?duì)象試圖自己分配自己。 還有另一個(gè)潛在的錯(cuò)誤,如果之前已經(jīng)分配了olMailItem ,這個(gè)語句會(huì)引發(fā)另一個(gè)錯(cuò)誤(可能是
我建議使用wireshark http://www.wireshark.org/通過記錄(“捕獲”)設(shè)備可以看到的網(wǎng)絡(luò)流量副本來“監(jiān)聽”網(wǎng)絡(luò)上發(fā)生的對(duì)話。 當(dāng)您開始捕獲時(shí),數(shù)據(jù)量似乎過大,但如果您能夠發(fā)現(xiàn)任何看起來像您的SOAP消息的片段(應(yīng)該很容易發(fā)現(xiàn)),那么您可以通過右鍵單擊并選擇來快速過濾到該對(duì)話'關(guān)注TCP Stream'。 然后,您可以在彈出窗口中查看您編寫的SOAP服務(wù)與Silverlight客戶端之間的整個(gè)對(duì)話。 如果一切正常,請(qǐng)關(guān)閉彈出窗口。 作為一個(gè)額外的好處,wireshar
Android默認(rèn)情況下不提供TextView的合理結(jié)果。 您可以使用以下庫并實(shí)現(xiàn)適當(dāng)?shù)腶ligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/
你的代碼適合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我將它下載到c:\ temp \ apples.java。 以下是我編譯和運(yùn)行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV
12個(gè)十六進(jìn)制數(shù)字(帶前導(dǎo)0x)表示48位。 那是256 TB的虛擬地址空間。 在AMD64上閱讀wiki(我假設(shè)你在上面,對(duì)嗎?)架構(gòu)http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar
這將取決于你想要的。 對(duì)象有兩種屬性:類屬性和實(shí)例屬性。 類屬性 類屬性對(duì)于類的每個(gè)實(shí)例都是相同的對(duì)象。 class MyClass: class_attribute = [] 這里已經(jīng)為類定義了MyClass.class_attribute ,您可以使用它。 如果您創(chuàng)建MyClass實(shí)例,則每個(gè)實(shí)例都可以訪問相同的class_attribute 。 實(shí)例屬性 instance屬性僅在創(chuàng)建實(shí)例時(shí)可用,并且對(duì)于類的每個(gè)實(shí)例都是唯一的。 您只能在實(shí)例上使用它們。 在方法__init__中定
總結(jié)
以上是生活随笔為你收集整理的shell获取目录的上级目录_Shell:如何获取文件指定的父目录的名称?(Shell: How to a file's specified parent directory's name?)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的世界观【文津图书奖获奖作品】
- 下一篇: 近期刷题总结[2019 03 09]