문제

Is there an easy way to copy an specific file nested in an already nested directory creating an structure of directories nested in the same way its file path (in linux)?

for instance;

copy_command A/B/C/a.txt OTHER_DIR

would create

OTHER_DIR/A/B/C/a.txt

creating the directory structure A/B/C into OTHER_DIR and copying the file a.txt on his corresponding dir.

도움이 되었습니까?

해결책

With GNU cp

cp --parents -- A/B/C/a.txt OTHER_DIR

다른 팁

The ${var_name%pattern} syntax removes pattern from the variable's value. With that in mind:

file="A/B/C/a.txt"
mkdir -p "OTHER_DIR/${file%/*}"
cp "$file" "OTHER_DIR/${file%/*}/"

Which is equivalent to:

mkdir -p OTHER_DIR/A/B/C
cp A/B/C/a.txt OTHER_DIR/A/B/C/
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top