パスの../..部分をプログラム的に圧縮する方法はありますか?

パスの../..部分をプログラム的に圧縮する方法はありますか?

たとえば、私のスクリプトには次の種類のファイルパスがあります。

/path/to/some/file/../../file1.txt

readlinkそのパスを実際の物理形式に変換するために呼び出すことができるコマンドはありますか?

/path/to/file1.txt

ベストアンサー1

ああ、あまりにも急な質問ですね。 Linuxでは、スイッチreadlinkで使用するのが答えです-m

$ readlink -m /home/saml/web/../web_login_form_examples/basic-php-parsing.zip
/home/saml/web_login_form_examples/basic-php-parsing.zip

読み取りリンクのマニュアルページ

-m, --canonicalize-missing
       canonicalize by following every symlink in every component of the
       given name recursively, without requirements on components existence

おすすめ記事