Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

It's often that a long path is mistyped or you get a log file entry that a path does not exist. Is there a command or shell function that navigates the path hierarchy until it finds a matching path?

The command line exchange could go like this:

$ ls /var/lib/my/supper/complicated/path
File or directory not found
$ fixpath /var/lib/my/supper/complicated/path
Found /var/lib/my

As a bonus feature, a similarity detection would be great:

$ fixpath /var/lib/my/supper/complicated/path
Found /var/lib/my
Did you mean /var/lib/my/super/complicated/path
share|improve this question
    
What shell you use ? tcsh has auto correction available, otherwise just loop through the path, it's not hard to make a script –  warl0ck Jun 26 '12 at 13:41

1 Answer 1

 $ fixpath /home/user/docus/collection/unix/djakl/jfkdsl/dfjksld/fsdkl
Found /home/user/docus/collection/unix
 $ type fixpath
fixpath is a function
fixpath ()
{
    CURDIR="$PWD";
    DIR="$1";
    E=1;
    while [ $E -eq 1 ]; do
        cd "$DIR" 2> /dev/null && {
            E=0;
            echo Found "$DIR"
        } || {
            DIR="${DIR%/*}";
            DIR="${DIR:-/}"
        };
    done;
    cd "$CURDIR"
}

Similarity autocorrection most modern shells can do itself. At least bash and zsh do.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.