Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Every time I need to find a file and then open it, I have to use : find ./ -name **.properties. , then copy the result, and then vi "paste the result here" . If I need to use a mouse, it can be a little trouble. So is there any better way to do this?

share|improve this question
    
Is it possible to open the second file only ? –  Foolish Nov 11 '10 at 2:19
    
find ./ -name *properties | head -2 | tail -1 I think this is not that good –  Foolish Nov 11 '10 at 2:27
add comment

7 Answers

up vote 2 down vote accepted

You can open an arbitrary file by using AWK or sed to print a particular line number.

vi "$(find ./ -name **.properties | awk 'NR==2')"

or

vi "$(find ./ -name **.properties | awk 'NR==2{print;quit}')"

or

vi "$(find ./ -name **.properties | sed -n '2p')"

or

vi "$(find ./ -name **.properties | sed -n '2{p;q}')"

The versions with quit or q return a little quicker if there is a large number of files.

share|improve this answer
add comment
find . -name \*.properties | xargs -d '\n' vi

will open all files in a single vi instance. To open just the first, use

find . -name \*.properties | head -1 | xargs -d '\n' vi
share|improve this answer
    
With GNU findutils, find . -name '*.properties' -exec vi '{}' + will do the first. –  ephemient Nov 11 '10 at 0:16
    
Is it possible to open only second file ? –  Foolish Nov 11 '10 at 2:25
    
@ephemient: no it won't. –  reinierpost Dec 7 '10 at 15:09
    
@Foolish: Sure: find . -name \*.properties | head -2 | tail -1 | xargs -d '\n' vi –  reinierpost Dec 7 '10 at 15:10
    
What do you mean "no it won't"? Works fine here, with GNU findutils 4.5.9. –  ephemient Dec 7 '10 at 16:09
show 3 more comments

If you change your shell to zsh, you'll need find a lot less often, as zsh allows you to do most of what find can do through globbing. To start with, **/ has a special meaning: **/foo expands to files called foo in the current directory and its subdirectories recursively, i.e. it matches the same files as find . -name foo. So you can edit all matches with

vi **/*.properties

Since bash 4.0, if you put shopt -s globstar in your ~/.bashrc, **/ behaves in the same way.

In zsh, you have a lot more possibilities thanks to glob qualifiers (described very tersely near the end of the zshexpn man page). The principle is to restrict the matches by adding one or more qualifiers in parentheses after the match. For example, the qualifier . restricts to regular files, while / restricts to directories. The qualifier [1] retains only the first match; [1,4] retains the first four matches; and so on. For example, to edit the first (in full path lexicographic order) regular file matching *.properties:

vi **/*.properties(.[1])

If you'd rather edit the most recently modified matching file, you can use the om qualifier to sort the matches by modification times (in order of increasing age):

vi **/*.properties(.om[1])
share|improve this answer
add comment

You may want to look at the -quit option for the find command. The following could be an example of what you want to accomplish:

find . -name "*.properties" -print -exec vi {} \; -quit

I should point out that, as I discovered not long after I posted this answer, the -quit switch does not exist for the BSD version of the find command, what you are likely to find, for example, on MacOS.

share|improve this answer
add comment

I think someone has shown the answer:

find -name *.properties -exec vi {} \;

this will case a sequence of editing the files which are find by "find -name *.properties".

If you just want to open the second file:

vi "$(find -name *.properties | awk 'NR==2')"

share|improve this answer
add comment

find . -name "*.properties" -exec vi {} \;

Then use the interrupt key (e.g. ^C) after editing the first file.

share|improve this answer
    
That will open a vi for each file found, in sequence. –  reinierpost Nov 10 '10 at 10:25
    
True, am upvoting your answer. –  RedGrittyBrick Nov 10 '10 at 10:36
add comment

You can put the results of the find command into the command line for vi like this:

vi $(find . -name \*.properties -print)

or like this:

vi $(find -name \*.properties)

since . and -print are implied if omitted. To open just the first result, use

vi $(find -name \*.properties | head -1)
share|improve this answer
    
is it possible to open the second result only ? I tried Head -2, but it will open both first and second files –  Foolish Nov 11 '10 at 2:15
    
Please be aware that this syntax is specific to certain shells such as bash and ksh. –  reinierpost Dec 7 '10 at 15:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.