#!/bin/sh # find files in /boot/(path) subfolders, skip (path)=/overlays/, move others to (path) # ver 2017-10-26 22:55 #start in boot cd /boot/ # redircet echo and error to log log="/boot/move_files.log" touch $log exec 1>>${log} exec 2>>${log} # start with date echo "" echo "++ $(date) - start" # find $PWD -type f -maxdepth 10 will generate a complete file list with path including /boot/ up to 10 subfoldres deep # (if you remove $PWD each file path starts with './' ) # '-type f' list only files = else you get dir mixed in i.e. root listed as . or /boot and ./overlaps or /boot/overlays ) # to create just the folder at /, in FAT32 (/boot/) put a dmy file eg '/my-new/folder/dmy' files=$(find $PWD -maxdepth 10 -type f) count=0 for file in $files do echo "++ processing $file" # only process files in sub-folders. # ${file:6} skips 1st 6 chars (/boot/) = so you get just filename (from root), and sub-folder files are 'fdr/filename' # Only prcess files in sub-folders (=~ will search whole string for sub string) if [[ ${file:6} =~ "/" ]]; then # OK files is in a sub-folder .. only process if not /overlays/ if [[ ${file:6} != "overlays/"* ]]; then # OK, got a valid file, do the move # ${file:5} will skip 5, i.e. chop /boot off the front, so file path/name starts with / echo "++ Moving $file to ${file:5}" # bash mv / cp doesn't create destination driectory, mv -f "$file" "${file:5}", # so we have to use mkdir -p to create dir chain, but that also creates file name as dir mkdir -p "${file:5}" # now remove the end )filename) dir rmdir "${file:5}" # finally, do the mv mv -f "$file" "${file:5}" # increment the count let or () needed for arithmetic proc (else it treats as text) count = $(( count + 1 )) # add execute if .sh if [ $file == *".sh" ]; then echo "++ Adding execute permissions to file ${file:5}" chmod +x "${file:5}" fi else echo " - skipping overlays/ file ${file:6}" fi else echo " - skiping root file ${file:6}" fi done echo "++ $(date) - done $count files moved" # OK thats all echo ""