views:

107

answers:

4

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?

+1  A: 

The basic problem is that you need to specify a way to choose one of the paths that locate finds. You are doing this manually now, but to automate it you need to specify how to make that choice.

For example if you just want any old path, then take the first one:

nano `locate php.ini | head -n 1`

To pick a result from the middle use both head and tail, for example the 11th:

nano `locate php.ini | head -n 11 | tail -n 1

In general change the argument to head to be the number of the line that you want to edit. Other ways that you might want to try are if you know a unique part of the path, say blah is part of the path that you want, but not in any of the others i.e /opt/bin/blah/php.ini. Then you can can use grep instead :

nano `locate php.ini | grep blah'

Edit:

Although these tips all do what you want I've just reread your question and realised that there is a much better way. Do what you do now - manually run locate, then type nano /some/path/php.ini.

Everytime after this just type ctrl-r then php.ini and bash will find that command in your history for you so that you don't have to type it again.

Amoss
If there is 20 results (and I want to choose the 11th.. it's a little painful still to count the lines). Some tip about that?
corbacho
I've added a couple more examples to help.
Amoss
+1  A: 

if you have only one result:

nano `locate php.ini`

if you want to edit the last

nano `locate php.ini|tail -n 1`

if you want to search in a directory only:

nano `find /path/where/the/directory/is -type f -name php.ini`

etc.

KARASZI István
but keep in mind, that `locate` is using a precached database, so new files won't be there until the next `updatedb` run, but `find` searches on the actual data so it's slower but more accurate
KARASZI István
Nice tips :) They help. I see that `find` has `-exec` parameter. Can be helpful to automatize more and not to run 2 commands every time?
corbacho
I would suggest to use `xargs` instead of `-exec` it's far better. e.g.: `find . -type f -print0|xargs -n 20 -0 nano` -- this will edit the files in the current directory 20 files at one shot and whitespace doesn't matter in the filename
KARASZI István
+1  A: 

A few ways of doing this:

  1. To edit first matched file:

    nano `locate -l1 php.ini`
  2. To search back in history for recent commands press CTRL+R repeatedly.

  3. In zsh you can do (usually slower than locate):

    nano /etc/**/php.ini

Tip: Use aliases or shell functions for frequently used commands.

hluk
Maybe can be combined with history commands? and make a function that takes the last command in the history? `!!` and then process it? so the function could be `myfunction 3` (take the third result of the last command executed)
corbacho
+4  A: 

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 () {
    PS3="Choose a file to edit: "
    select opt in $(locate "$1") quit
    do
        if [[ $opt = "quit" ]]
        then
            break
        fi
        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
Dennis Williamson
Awesome piece of art. Other answers are good tips but this is definitely what I was looking for :)
corbacho