Ubuntu – Windows filepath converted to Linux filepath

bashcommand linepathstext processing

I have a Windows path in a bash variable as a string:

file='C:\Users\abcd\Downloads\testingFile.log'

I am trying to convert this path into a Linux path starting with /c/Users....

My attempt

The following works:

file=${file/C://c}
file=${file//\\//}
echo $file
> /c/Users/abcd/Downloads/testingFile.log

Problem

Here, I have done this for a string that contains the filepath. The reason I am asking this question is that I have to convert 20 such strings in a bash script in Ubuntu 16.04 and each time I do this I have to write 2 lines per conversion – it is taking up a lot of space!

Question

Is there a way to combine the 2 commands

file=${file/C://c}
file=${file//\\//}

into one command?

Best Answer

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:

  1. Put filenames in array
  2. 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" )