2014-03-25 34 views
1

我有兩個文件夾,我們稱它們爲A和B.兩個文件夾下都有一個文件夾結構,這是相同的。說A有文件夾1,2,3,4和5.B有文件夾1,2,3,4和5.A的所有子文件夾中都有不同的文件(例如/A/1/file.txt/A /2/anotherfile.txt)。子文件夾的子文件夾也一樣,所以樹很深。在Linux中建立從所有子文件夾到所有子文件夾的硬鏈接?

現在,我可以在所有B:\ S文件夾和子文件夾中製作硬鏈接嗎?所以兩個樹結構看起來都是一樣的,所有的文件夾和子文件夾都有相同的文件。那麼,硬鏈接是。

CP -l在一個所有文件分爲B

回答

2

bash 4,你可以寫一個簡單的循環,像

cd A 
shopt -s globstar 
for d in **/; do 
    mkdir -p "B/$d" 
    for f in "$d"; do 
     [[ -f $f ]] && cp -l "$f" "B/$f" 
    done 
done 
+0

即使我的解決方案是簡短而優雅的,'--link-dest'在手冊頁中被混淆地解釋,這使得它非常令人頭疼,所以我可能寧願自己使用循環,除非我記得如何使用'rsync'這個場景不在我頭頂,所以有一個upvote。 –

+0

我還想添加一個'rsync'的答案,但是我對於它是否會工作同樣感到困惑:) – chepner

+0

我承認我確實需要玩幾分鐘,我從不記得'--link-dest '的作品,但它肯定是方便的:) –

1

這可以輕鬆完成使用rsync

$ rsync -a --link-dest=../A A/ B 

例如:

# Optionally, get rid of your destination 
$ rm -rf B 

$ find 
. 
./A 
./A/2 
./A/2/foo 
./A/1 
./A/1/foo 

# If the argument to --link-dest is relative, it is relative 
# to the target directory which is B in this case, hence the ../A 
$ rsync -a --link-dest=../A A/ B 

$ find 
. 
./B 
./B/2 
./B/2/foo 
./B/1 
./B/1/foo 
./A 
./A/2 
./A/2/foo 
./A/1 
./A/1/foo 

# Notice the identical inodes 
$ ls -1i {A,B}/{1,2}/foo 
349408 A/1/foo 
349409 A/2/foo 
349408 B/1/foo 
349409 B/2/foo 
+0

+1很好解釋,以及。 – chepner

+0

cp -al A/* B/ – Paolo

+0

這個,呃,似乎有用。 D'哦。請注意,這不是可移植的,但在你的情況下,它可能並不重要。 –

相關問題