When I copy any file and paste it in console or text editos it is passed as


when I pass it to script it is not found

What is the easiest way to convert that to normal linux path or somehow make script support it?

for example

cat file:///home/user/path/file


No such file or directory

5 Answers 5


I don't know of any commands that convert between file urls and file paths, but you can convert with python, or any other language with bindings to gio. E.g.:

$ python -c 'import gio,sys; print(gio.File(sys.argv[1]).get_path())' file:///home/user/path/file%20with%20spaces
/home/user/path/file with spaces
  • Thanks a bunch! Your command also works with umlauts, which is perfect for my use case. Dec 25, 2012 at 2:06
  • Using this solution, rather than rolling your own, has the further advantage that support for protocols other than file:///, such as sftp://, drops out of the box for free, via gvfs.
    – aplaice
    Nov 17, 2016 at 1:16

You can also use urlencode (sudo apt-get gridsite-clients):

$ echo "$(urlencode -d "file:///folder/with%20spaces")"
file:///folder/with spaces
$ echo "$(urlencode -d "file:///folder/with%20spaces"|cut -c 8-)"
/folder/with spaces

If you don't need hexadecimal support, you can just use cut -c 8-. Alternatively, you could use urlencode with any other method of removing the file:// (sed, brace expansion, etc.)


To remove the file:// prefix from the URL, you can use sed:

echo "file:///home/user/path/file" | sed "s/^file:\/\///g"

What the above does:

  • Displays the URL to the standard output (so it can be modified with sed)
  • Replaces all occurrences of file:// in any line that begins with file:// with nothing. This effectively removes file:// from the URL leaving only /home/user/path/file

To use this from a script you can try the following:

cat $(echo "file:///home/user/path/file" | sed "s/^file:\/\///g")

Now the error message is:

cat: /home/user/path/file: No such file or directory

(Please note that it refers to the correct filename instead of the URL.)

It would be much cleaner to store the converted filename in a shell variable and use it afterwards.

MYFILE=$(echo "file:///home/user/path/file" | sed "s/^file:\/\///g")

I believe you can do this is bash itself. Try the following

echo "file:///home/user/path/file" | cut -d'/' -f3-

It will delimit till file:// and the rest will be echoed on the terminal.

  • Nice, +1. However, cut is in no way part of bash.
    – terdon
    Aug 1, 2013 at 18:18
  • This will work incorrectly if the path doesn't start with file://, though.
    – kiri
    Jan 11, 2014 at 1:36

You can use this, assuming file_path contains the path:


file_path='file:///home/me/Desktop/path test'


echo "${file_path}"

which prints /home/me/Desktop/path test. This allows it to work with or without file://, using only Bash string manipulation.

You can add this to a function (in .bashrc) for ease of use:


norm_path() {
    echo "${@#file://}"


cat "$(norm_path file:///home/user/path/file)"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .