Windows filepath converted to Linux filepath
There would be a way to do both replacements at once using sed
, but it's not necessary.
Here's how I would solve this problem:
- Put filenames in array
- Iterate over array
filenames=(
'C:\Users\abcd\Downloads\testingFile.log'
# ... add more here ...
)
for f in "${filenames[@]}"; do
f="${f/C://c}"
f="${f//\\//}"
echo "$f"
done
If you want to put the output into an array instead of printing, replace the echo
line with an assignment:
filenames_out+=( "$f" )
If it's something you want to do many times, then why not create a little shell function?
win2lin () { f="${1/C://c}"; printf '%s\n' "${f//\\//}"; }
$ file='C:\Users\abcd\Downloads\testingFile.log'
$ win2lin "$file"
/c/Users/abcd/Downloads/testingFile.log
$
$ file='C:\Users\pqrs\Documents\foobar'
$ win2lin "$file"
/c/Users/pqrs/Documents/foobar
You would be able to achieve this in one line using sed
file="$(echo "$file" | sed -r -e 's|^C:|/c|' -e 's|\\|/|g')"
Note the two patterns must remain separate nonetheless as the matches are replaced by different substitutions.