I face many times this simple and repetitive task configuring the LAMP or some stuff in Ubuntu or Drupal:
I have to edit a config file (php.ini, httpd.conf, ... whatever) so quite frequently, if I don't remember the path by heart, I run these 2 commands:
locate php.ini
------- typing manually one of the paths that are shown in the list...
nano /etc/php5/apache2/php.ini
I'm sure this can be improved (without having to memorize the paths). Is there a better approach?
I like the way how this was solved with the command history
: you can execute one of the previous commands in history with !32
(for example). Is there a similar way with locate
or find
commands?
This won't work if files or directories have spaces in their names, but it wouldn't be too hard to make a version that would.
choose () {
local PS3="Choose a file to edit: "
select opt in $(locate "$1") quit
do
if [[ $opt = "quit" ]]
then
break
fi
${EDITOR:-nano} "$opt"
done
}
Demo:
$ choose php.ini
1) /etc/php5/apache2/php.ini
2) /etc/php5/apache2/php.ini.ucf-old
3) /etc/php5/apache2/php.ini~
4) /etc/php5/cli/php.ini
5) /etc/php5/cli/php.ini~
6) /home/dennis/Downloads/php.ini
7) /usr/share/doc/php5-common/examples/php.ini-dist
8) /usr/share/doc/php5-common/examples/php.ini-paranoid
9) /usr/share/doc/php5-common/examples/php.ini-recommended
10) /usr/share/php5/php.ini-dist
11) /usr/share/php5/php.ini-dist.cli
12) quit
Choose a file to edit: 12