遍历当前路径下目录并移动文件到当前目录下的tmp目录中脚本(处理了文件名空格)
使用场景
在我的某个目录下,有许多的文件和文件夹,我希望将这个目录下所有文件及所有子文件夹下的文件,移动到指定的一个目录下,同时,使用子文件夹的路径作为新文件名的前缀,以--作为分隔符。
简单点说,就是消除目录层级
此shell脚本考虑了文件名中包含空格的情况,当然,也不存在文件名冲突的问题。
脚本如下
#!/bin/bash
#set -e #"Exit immediately if a simple command exits with a non-zero status."
#set +e # don’t bail out of bash script if cache doesn’t exist
#将当前目录下所有子目录中的所有文件移动到当前目录下的tmp目录中
#即消除目录层级
old_dir=`pwd`
cd `dirname $0`
mkdir tmp_pic
curr_dir=`pwd`
function getdir(){
for element in `ls $1 | tr " " "\?"`
do
my_element=`echo $element | tr "\?" " "`
dir_or_file="$1/$my_element"
if [[ "${dir_or_file}" =~ ^.*tmp_pic$ || "${dir_or_file}" =~ ^.*sh$ ]]