Extract filename and path from URL in bash script

后端 未结 13 967
别跟我提以往
别跟我提以往 2021-01-30 14:17

In my bash script I need to extract just the path from the given URL. For example, from the variable containing string:

http://login:password@example.com/one/more/dir/fi

13条回答
  •  失恋的感觉
    2021-01-30 14:28

    Best bet is to find a language that has a URL parsing library:

    url="http://login:password@example.com/one/more/dir/file.exe?a=sth&b=sth"
    path=$( echo "$url" | ruby -ruri -e 'puts URI.parse(gets.chomp).path' )
    

    or

    path=$( echo "$url" | perl -MURI -le 'chomp($url = <>); print URI->new($url)->path' )
    

提交回复
热议问题