[英]Appending rather than overwriting files when moving
我有以下目錄結構:
+-archive
+-a
+-data.txt
+-b
+-data.txt
+-incoming
+-a
+-data.txt
+-c
+-data.txt
我如何做相當於mv incoming/* archive/
但是將incoming
文件的內容附加到archive
中的archive
而不是覆蓋它們?
# move to incoming/ so that we don't
# need to strip a path prefix
cd incoming
# create directories that are missing in archive
for d in `find . -type d`; do
if [ ! -d "../archive/$d" ]; then
mkdir -p "../archive/$d"
fi
done
# concatenate all files to already existing
# ones (or automatically create them)
for f in `find . -type f`; do
cat "$f" >> "../archive/$f"
done
這應該在incoming
找到任何文件並將其連接到archive
的現有文件。
重要的部分是在 incoming
內部 ,因為否則我們必須剝離路徑前綴(這是可能的,但在上面的情況下是不必要的)。 在上面的例子中, $f
的值通常看起來像./a/data.txt
,因此重定向轉到../archive/./a/data.txt
。
這是一個適當引用的版本:
#!/bin/sh
if [ -z "$1" ]; then
# acting as parent script
find incoming -type f -exec "$0" {} \;
else
# acting as child script
for in_file; do
if [ -f "$in_file" ]; then
destfile="${in_file/incoming/archive}"
test -d "$(dirname "$destfile")" || mkdir -p "$_"
cat "$in_file" >> "$destfile" &&
rm -f "$in_file"
fi
done
fi
在當前目錄上運行它。
find ./incoming -type f | while read -r FILE
do
dest=${FILE/incoming/archive}
cat "$FILE" >> "$dest"
done
雖然incoming/c
那個不會被追加
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.