Artificial intelligent assistant

Replace slashes in a filename So I need to write a bash script that copies all files to a specified directory however I need to rename the files to it's absolute path by replacing the `/` character with `__`. For example if the file `zad1.sh` is in the directory `/home/123456/` the file needs to be renamed to `__home__123456__zad1.sh` Any ideas on how to do this?

The classic approach would be to use sed:


cp "${filename}" "$(realpath ${filename} | sed s:/:__:g)"


The advantage is primarily portability across shells if you won't necessarily always be using bash.

Of course, it also lets you forego the script and just do it with find:


find /base/path -type f -exec cp \{\} `realpath \{\} | sed s:/:__:g` \;


Find has sorting and filtering options you can use if you need them to only copy certain files.

edit: That find setup works on one of my systems, but not on the other one. Rather than sort out the difference, I just made it more portable:


find /base/path -type f | sed -e "p; s:/:__:g" | xargs -n2 cp

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 4fae771dc2ce6662d3c47aa0e9db463a