I'd like to open all text files that are the result of a ls command using a text editor. How do i do this?
Gilles is exactly right,
For the sake of demonstrating a technique, lets use a more complicated example that could not be done with just a filename match and open files based on the content instead of just the file name. Lets say you wanted to open all the files that contained the string "content-type".
Assuming your editor will accept multiple file names and open them all at once in separate buffers or sequentially work it's way through them, you can simply run:
Now back to your original question, lets say you don't know if they are text files or not based on their names. You would then need to use another program to identify them, then open them based on that data. The program
The output of the command chain inside the
...and decide I want to open them. I then use the last command history shortcut and do this:
...to open all of the results of the previous command in vim, my favorite editor.
If your editor will only accept one file at a time and you need to keep spawing editors, you will need to use a variant of either the
To open all the files called something
Note that the
Advanced enough editors let you do this from inside. For example, in Emacs, just use the normal file opening command (
If you also want to match files in subdirectories, in zsh or bash ≥4, you can use
It depends on some nuances of your text editor. Advanced editors can probably handle opening multiple files from the command line in one instance. But say, for example, that your editor ($EDITOR in my examples below) can only open one at a time. You should pipe the output of an appropriate
If you want to use a console editor, a shell loop might be more appropriate, but if will fail for many uncommon filenames (by collapsing or substituting whitespace characters and various other things):