开发者

Find a file and open it for editing. How to do it fast in a bash shell?

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 开发者_如何学Goapproach?

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


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.


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.


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.


I liked the approach from the first answer, but for my purposes I wanted to do a soft match down current the directory tree, so I modified it as follows:

choose () {
PS3="Choose a file to edit: "
select opt in $(find . -name \*$1\*) quit
do
    if [[ $opt = "quit" ]]
    then
        break
    fi
    nano "$opt"
    done
}


Just use this command:

locate filename | xargs xdg-open
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜