Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

Gilles is exactly right, ls is a really bad example because file name glob expansion can be done much easier on the command line without having to use ls at all! If your so called "text files" have file name extensions to identify them, you could do something like this:

editor-command *.txt

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:

editor-command $(grep -i content-type)

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 file will tell you what kind of file something is, and you can grep that list for just text files, and then open just the matching file names like this:

editor-command $(file -ni * | grep 'text/plain' | cut -d: -f1)

The output of the command chain inside the $() construct will be used as arguments for the editor. I sometimes do this in two stages. Say I'm looking through some set of files and get myself a list of every xml file containing the string "content-type"

find -type f -iname '*xml' | xargs grep -Hi 'content-type' |  cut -d: -f1

...and decide I want to open them. I then use the last command history shortcut and do this:

vim $(!!)

...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 xargs or for loop in jmtd's answer depending on whether you are using a terminal based editor that needs stdio.

share|improve this answer
4  
Don't parse the output of ls! Use editor-command *.css, etc. –  Gilles May 4 '11 at 20:23
    
@Gilles: Duly noted, thanks for pointing out the obvious that we all couldn't see. @c0smikdebris Note that I just completely rewrite my answer including a case for if you don't have an obvious way of matching which files are text by just their name. –  Caleb May 4 '11 at 21:04
    
$() is what i was actually looking for. i should've mentioned that i'm using ls as an example here. +1 to both answers. –  c0smikdebris May 5 '11 at 6:29
add comment

To open all the files called something .txt (i.e. *.txt) in the current directory:

$EDITOR *.txt

where $EDITOR is your favorite editor, e.g. gedit *.txt, nano *.txt, vim *.txt, emacs *.txt or whatever. Use just * to match all files in the current directory.

Note that the ls command isn't involved. The job of ls isn't really to display a list of files, although that happens when you invoke it on a directory. Where ls is useful is in displaying file attributes (modification time, permissions, size and so on). To just do something on a bunch of files matching a certain wildcard pattern, the shell's globs are enough.

Advanced enough editors let you do this from inside. For example, in Emacs, just use the normal file opening command (C-x C-f) and enter *.txt.

If you also want to match files in subdirectories, in zsh or bash ≥4, you can use $EDITOR **/*.txt.

share|improve this answer
    
That is really editor-specific. There's no guarantee all editors will treat all their free command-line arguments as files to open, nor support opening more than one file at a time. –  jmtd May 9 '11 at 13:48
add comment

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 find into xargs. This also depends on the $EDITOR not taking over the TTY (so, using a graphical one)

find . -maxdepth 1 -type f -name '*.c' -print0 | xargs -r0n 1 $EDITOR

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):

for i in *.c; do $EDITOR "$i"; done
share|improve this answer
2  
Don't parse the output of ls! Use $EDITOR *.c or for i in *.c; do $EDITOR "$i"; done. –  Gilles May 4 '11 at 20:23
    
Good catch -- edited appropriately. –  jmtd May 9 '11 at 13:49
    
Note that the for case will fail for many filenames, including collapsing/changing whitespace characters. –  jmtd May 9 '11 at 13:52
    
As it's written now, the for case will work with every file name (except possibly names beginning with - or + with some editors; for i in ./*.c would fix that). –  Gilles May 9 '11 at 15:59
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.