Unix shell file copy flattening folder structure

后端 未结 5 1398
走了就别回头了
走了就别回头了 2021-01-30 08:52

On the UNIX bash shell (specifically Mac OS X Leopard) what would be the simplest way to copy every file having a specific extension from a folder hierarchy (including subdirect

5条回答
  •  -上瘾入骨i
    2021-01-30 09:23

    The answers above don't allow for name collisions as the asker didn't mind files being over-written.

    I do mind files being over-written so came up with a different approach. Replacing each / in the path with - keep the hierarchy in the names, and puts all the files in one flat folder.

    We use find to get the list of all files, then awk to create a mv command with the original filename and the modified filename then pass those to bash to be executed.

    find ./from -type f | awk '{ str=$0; sub(/\.\//, "", str); gsub(/\//, "-", str); print "mv " $0 " ./to/" str }' | bash
    

    where ./from and ./to are directories to mv from and to.

提交回复
热议问题