linux複製當前目錄到其子目錄下

2021-10-01 11:18:19 字數 1075 閱讀 9453

假設有以下目錄結構,需要把當前目錄.下所有檔案及資料夾(dst除外)複製到其子目錄dst資料夾下:

tree

.├── dst

├── file1.txt

└── src

├── dir

│ └── file3.txt

└── file2.txt

不能直接使用cp -a . dst

cp: cannot copy a directory, 『.』, into itself, 『dst/.』
可行的一種方式:

ls

|grep -v '^dst$'

|xargs

cp -at dst

# grep -v:正則匹配取反

# cp -t: --target-directory=dir 表示將所有引數指定的源目錄/檔案複製到目標目錄下

# cp -a: --archive 等同於 cp -dr --preserve=all

執行結果:

tree

.├── dst

│ ├── file1.txt

│ └── src

│ ├── dir

│ │ └── file3.txt

│ └── file2.txt

├── file1.txt

└── src

├── dir

│ └── file3.txt

└── file2.txt

如果當前目錄下有.開頭的隱藏檔案也需要複製,則要使用

ls -a |

grep -v '^\(\.\\|dst\)$'

|xargs

cp -at dst

注:需要把當前目錄.和上級目錄..也排除

拷貝當前目錄到SD卡

echo off setlocal enabledelayedexpansion set src root dir e tmp set dst root dir sdcard gameloft games asphalt6 set adb push android sdk home platform...

拷貝當前目錄到SD卡

echo off setlocal enabledelayedexpansion set src root dir e tmp set dst root dir sdcard gameloft games asphalt6 set adb push android sdk home platform...

python如何實現複製目錄到指定目錄

儲存下面 為乙個檔案直接執行 import os import time copyfilecounts 0 def copyfiles sourcedir,targetdir global copyfilecounts print sourcedir print u s 當前處理資料夾 s已處理 s...