district10 / blog

一点分享
https://github.com/district10/blog-publish
MIT License
22 stars 2 forks source link

拍扁(flatten)一个目录 #11

Open district10 opened 7 years ago

district10 commented 7 years ago

脚本

可以把当前文件夹下所有文件拍扁然后拷贝到当前目录:

find * -type f | sed -e "p;s/\//____/g;" | sed -e "s/^/\"/" -e "s/$/\"/" | xargs -n2 cp

也可以指定文件夹:

find __DIR_NAME__ -type f | sed -e "p;s/\//____/g;" | sed -e "s/^/\"/" -e "s/$/\"/" | xargs -n2 cp

(如果你不想要看到 warning,可以考虑加上 2> /dev/null。)

举例

比如,你要把 modules 文件夹拍扁。

$ pwd
/c/tzx/git/cmake-templates

# 看看文件夹下有啥
$ find modules/ -type f
modules/CMakeLists.txt
modules/demo/calc.c
modules/demo/CMakeLists.txt
modules/includes/functions.h
modules/README.txt
modules/src/CMakeLists.txt
modules/src/cubic/CMakeLists.txt
modules/src/cubic/cubic.c
modules/src/extras/CMakeLists.txt
modules/src/extras/power4.c
modules/src/extras/power5.c
modules/src/square/CMakeLists.txt
modules/src/square/square.c

# 拍扁之
$ find modules -type f | sed -e "p;s/\//____/g;" | sed -e "s/^/\"/" -e "s/$/\"/" | xargs -n2 cp

# 输出
$ ls modules*
modules____CMakeLists.txt
modules____demo____calc.c
modules____demo____CMakeLists.txt
modules____includes____functions.h
modules____README.txt
modules____src____CMakeLists.txt
modules____src____cubic____CMakeLists.txt
modules____src____cubic____cubic.c
modules____src____extras____CMakeLists.txt
modules____src____extras____power4.c
modules____src____extras____power5.c
modules____src____square____CMakeLists.txt
modules____src____square____square.c

modules:
CMakeLists.txt  demo/  includes/  README.txt  src/
district10 commented 7 years ago

如果 cp 的源文件和目标文件是一个,会有 warning,拷贝不会执行。

district10 commented 7 years ago

cp 的 --update 选项在 macOS 上没用。去掉了。

district10 commented 7 years ago

去掉其中的空格:

find * -type f | sed -e "p;s/ /_/g;" | sed -e "s/^/\"/" -e "s/$/\"/" | xargs -n2 mv