NAME
cdpart-backend - Change directory part
VERSION
This document describes version 0.009 of cdpart-backend (from Perl distribution App-CdUtils), released on 2025-06-01.
SYNOPSIS
To use in shell:
% cdpart() { cd `cdpart-backend "$1"`; }
% cd /media/mv/en/c/celine-dion
% cdpart fr; # /media/mv/fr/c/celine-dion
DESCRIPTION
cdpart is a command to change directory which can be convenient in some cases. It takes your argument and tries to substitute your argument into the directory path element starting from the deepest to the topmost, stopping after it finds an existing directory, then change to the directory.
The best way to explain it is by an example. Consider this directory structure:
media/
mv/
en/
a/
b/
c/
carly-rae-japsen/
celine-dion/
charlie-puth/
...
...
fr/
a/
b/
c/
celine-dion/
christophe-willem/
...
de/
...
music/
en/
a/
b/
c/
celine-dion/
...
fr/
a/
b/
c/
celine-dion/
...
...
...
...
You can see that the directories mv
and music
have the same structure: the first subdirectory level is language code (en
, fr
, ...), the second level is the first letter of artist's name (a
, b
, c
, ...) and the third level is the name of the artist.
Suppose you are now in directory:
/media/mv/en/c/celine-dion
and you want to see Céline's French music videos (in /media/mv/fr/c/celine-dion
). If you use cd, the command will be:
% cd ../../fr/c/celine-dion
which is too much to type because you are retyping the substructure. With cdpart you just have to type:
% cdpart fr
What cdpart will do is try the following directories in order:
/media/mv/en/c/fr
/media/mv/en/fr/celine-dion
/media/mv/fr/c/celine-dion
The third succeeds so it goes there. If no match is found until the topmost directory, cdpart will try these in order:
/media/mv/en/fr
/media/mv/fr/c
/media/fr/en/c
/fr/mv/en/c
/media/mv/fr
/media/fr/en
/fr/mv/en
/media/fr
/fr/mv
/fr
If the last doesn't succeed too, cdpart won't change directory.
Another example:
% cdpart music
will eventually change the directory to /media/music/fr/c/celine-dion
.
COMPLETION
This script has shell tab completion capability with support for several shells.
bash
To activate bash completion for this script, put:
complete -C cdpart-backend cdpart-backend
in your bash startup (e.g. ~/.bashrc
). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.
It is recommended, however, that you install modules using cpanm-shcompgen which can activate shell completion for scripts immediately.
tcsh
To activate tcsh completion for this script, put:
complete cdpart-backend 'p/*/`cdpart-backend`/'
in your tcsh startup (e.g. ~/.tcshrc
). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.
It is also recommended to install shcompgen
(see above).
other shells
For fish and zsh, install shcompgen
as described above.
ENVIRONMENT
DEBUG
Bool. If set to true, will print the directories as they are tried.
CDPART_COMPLETE_ARRAY_ELEM_PER_LEVEL
Uint.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/App-CdUtils.
SOURCE
Source repository is at https://github.com/perlancar/perl-App-CdUtils.
SEE ALSO
AUTHOR
perlancar <perlancar@cpan.org>
CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
COPYRIGHT AND LICENSE
This software is copyright (c) 2025 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=App-CdUtils
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.