简体   繁体   中英

How to dot-dot from non-existent directory in Linux (dir1/non-existent-dir/./file)

Is there any way to dot-dot cd/ls/whatever if the path has an non-existent directory in it? Maybe there is some syntax to do so?

Eg I need to cat the file valid_dir/file.txt but for some reason I can't to it directly.

$> cat valid_dir/non-existent-dir/../file.txt

In GNU Coreutils, there is realpath to print resolved absolute filenames:

cat "$(realpath -m valid_dir/non-existent-dir/../file.txt)"

The -m ( --canonicalize-missing ) option is required if there are non-existing components.

If you want relative paths instead, you can define the directory they should be relative to with --relative-to , for example the current directory:

$ realpath --relative-to=. -m valid_dir/non-existent-dir/../file.txt
valid_dir/file.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM