Bash에서 파일 경로를 URI로 변환

Bash에서 파일 경로를 URI로 변환

명령줄에서 파일 경로를 URI로 변환하는 방법은 무엇입니까?

:

/home/MHC/directory with spaces and ümläuts

도착하다

file:///home/MHC/directory%20with%20spaces%20and%20%C3%BCml%C3%A4uts

답변1

한 가지 방법은 사용하는 것입니다 urlencode(Ubuntu에 설치하여 sudo apt-get install gridsite-clients).

urlencode -m "$filepath"

경로를 URI로 변환합니다. URI의 "file://" 부분은 생략되지만 bash 한 줄로 쉽게 추가할 수 있습니다.

uri=$(urlencode -m "$1"); echo "file://$uri"

아니면 직접

echo "file://$(urlencode -m "$1")"

또는

echo -n file://; urlencode -m "$1"

참고해 주신 Michael Kjörling에게 많은 감사를 드립니다!

답변2

CentOS에서는 추가 종속성이 필요하지 않습니다.

$ python -c "import urllib;print urllib.quote(raw_input())" <<< "$my_url"

답변3

Perl 모듈을 사용할 수도 있습니다.URI::파일명령줄에서 직접:

$ path="/home/MHC/directory with spaces and ümläuts"
$ echo $path | perl -MURI::file -e 'print URI::file->new(<STDIN>)."\n"'
file:///home/MHC/directory%20with%20spaces%20and%20%C3%BCml%C3%A4uts
$

답변4

다음 스크립트에 경로를 매개변수로 전달할 수 있습니다.

#!/usr/bin/env gjs

const { Gio } = imports.gi;

let path = Gio.File.new_for_path(ARGV[0]);
let uri = path.get_uri();
print(uri);

uri를 경로로 변환하려면 다음 스크립트를 사용하십시오.

#!/usr/bin/env gjs

const { Gio } = imports.gi;

let uri = Gio.File.new_for_uri(ARGV[0]);
let path = uri.get_path();
print(path);

관련 정보