Shell scripts recursively Extract files from folders,
Requirement
Two days ago, I ran into the need to extract files from a folder around 10 layers, so I wrote this script.
The file structure is as follows:
dir1 ├── a │ ├── b │ │ └── file1 │ └── file2 ├── c │ └── d │ ├── e │ │ └── file4 │ └── file3 └── file5
We need ~ File5 is extracted and put into another folder.
Script
The script getfilefromdir. sh is as follows:
#! /Bin/bash # desc: get file from directory # author: a decade later, Lu brother (http://www.cnblogs.com/lurenjiashuo/) # example: sh getfilefromdir. sh A BINIT_PATH =$ {1%/} SAVE_PATH =$ {2%/} function checksavepath () {if [-d $ SAVE_PATH] then rm-rf $ SAVE_PATH fi mkdir $ {SAVE_PATH} touch $ SAVE_PATH ". log "} function getfilefromdir () {for file in 'ls $ 1' do if [-d $1 #/" $ file] then getfilefromdir $1 #/" $ file else local path = "$1/$ fi Le "local name = $ file if [! -F $ SAVE_PATH "/" $ name] then echo "cp $ {path} to $ {SAVE_PATH}/$ {name}" cp $ {path} "$ {SAVE_PATH }/ $ {name} "else echo" $ {path} file already exists "echo" $ {path} ">>$ SAVE_PATH ". log "2> & 1 fi done} checksavepathfor sfol in $ {INIT_PATH} do getfilefromdir $ {sfol} done
Run
sh getfilefromdir.sh dir1/ dir2
The first parameter is the source folder, and the second parameter is the destination folder (you do not need to create it in advance ).
If a file with the same name exists, it will exist in dir2.log.
Result:
dir2├── file1├── file2├── file3├── file4└── file5
This article from ten years later brother Lu blog (http://www.cnblogs.com/lurenjiashuo/), reprint please indicate the original address.