I'm trying to write an alias which will jump to the descendant directory of cwd which contains a specified file (or the first find
found occurrence of such a filename):
The following command combination achieves the desired result:
cd `dirname \`find -name 'MyFile.txt' | sed -n 1p\``
However, I can't seem to escape this in the correct way to create a working alias:
alias jump="cd \`dirname \\\`find -name '$1' | sed -n 1p\\\`\`"
Output:
/*
dirname: missing operand
Try `dirname --help' for more information.
bash: cd: find: No such file or directory
My logic is that backticks need escaping in a double quoted string with a single \
and I can't do \\
gets translated to a single backslash within a string, so the second nested backtick requires 1+2=3.
Any suggestions?
Backticks are the old form of command substitution, and you can't nest them easily. However, the new $()
form does nest easily:
cd $(dirname $(find -name 'MyFile.txt' | sed -n 1p))
An alias cannot take an argument like $1
. Use a function instead.
Also use $(command)
for command substitution instead of backticks, as it is easier to nest.
The function would be:
jump() {
cd $(dirname $(find -name "$1" | sed -n 1p))
}
Backticks
doesn't offer nesting. Try using command substitution
which has the syntax $(..)
In your case it will be
cd $(dirname $(find /path/to/search -name 'MyFile.txt' | sed -n 1p))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With