I would use something like this:
find -type f -exec sh -c 'f={}; fnew=$(rev <<< "$f" | sed 's~/~_~' | rev); echo "mv $f $fnew"' \;
It searches for files in the current directory structure and performs the following actions:
- get file name
- replace last
/ by _ . - write
echo "cp $old_file $new_file"
Once you run this and see that it is printing the correct command, delete echo so that it effectively executes the mv command.
I know that fnew=$(rev <<< "$f" | sed 's~/~_~' | rev) is a bit ugly trick, but I couldn't find a better way to replace the last / with _ . Maybe sed -r 's~/([^/]*)$~_\1~' can also be approved, but I always like to use rev :)
Since your find does not work very well with the -sh c expression, use a while for it:
while read -r file do new_file=$(rev <<< "$file" | sed 's~/~_~' | rev) echo "mv $file $new_file"; done < <(find . -type f) done < <(find . -type f)
As single line:
while read -r file; do new_file=$(rev <<< "$file" | sed 's~/~_~' | rev); echo "mv $file $new_file"; done < <(find . -type f)
source share