Question

I have a directory like this:

dir
dir/somefile.txt
dir/subdir/subsub/somefile2.txt
dir/subdir2/somefile.txt

and I want to open all the files in all the subdirectories in a single instance of a command. I was trying find with -exec, or xargs, but these open each file with a separate instance of a command.

Basically, I want something that ends up like kate dir/somefile.txt dir/subdir/subsub/somefile2.txt dir/subdir2/somefile.txt, but for any number of files in any number of subdirectories. I'm using bash, but any script suggestions are fine.

clarification: I didn't just mean .txt files, but any ascii file (ie. .php, .txt, .html, etc..)

Was it helpful?

Solution

Try

kate `find . -name \*.txt -type f`

where the -type f prevents you from hitting directories.

Here is an example using ls -1 instead of kate:

edd@ron:~/src/debian/R$ ls -1 `find . -type f -name \*.txt`
./R-2.10.0/src/extra/graphapp/readme.txt
./R-2.10.0/src/extra/xdr/copyrght.txt
./R-2.10.0/src/extra/xdr/README.txt
./R-2.10.0/src/gnuwin32/fixed/etc/rgb.txt
./R-2.10.0/src/gnuwin32/installer/CustomMsg.txt
./R-2.10.0/src/library/grid/inst/doc/changes.txt
./R-2.10.0/src/unix/system.txt
./R-2.9.2-ra-1.2.8/src/extra/graphapp/readme.txt
./R-2.9.2-ra-1.2.8/src/extra/xdr/copyrght.txt
./R-2.9.2-ra-1.2.8/src/extra/xdr/README.txt
./R-2.9.2-ra-1.2.8/src/gnuwin32/fixed/etc/rgb.txt
./R-2.9.2-ra-1.2.8/src/gnuwin32/installer/CustomMsg.txt
./R-2.9.2-ra-1.2.8/src/library/grid/inst/doc/changes.txt
./R-2.9.2-ra-1.2.8/src/unix/system.txt
edd@ron:~/src/debian/R$

and if you really want all files in subdirectories the call simplifies to

kate `find . -type f`

if you are in dir/ or else

kate `find dir -type f`

OTHER TIPS

There are several possible options for this. These answers are based on your scenario where you know all files can be opened by kate, and you want to open files with any extension.

find dir -type f -exec kate {} +  

kate $(find dir -type f)

kate `find dir -type f`

The second and third forms are almost equivalent. The main difference[1] is that the first version will handle files with whitespace in their name, while the second and third do not.

[1] Thanks for pointing this out NVRAM, I didn't realise when I first posted the answer.

kate $(find dir -type f)

You're either not very familiar with xargs, or you're not using it correctly, because what you're trying to do is exactly the problem xargs is designed to solve: Given an arbitrarily long list of strings, pass them as arguments to a program in as few executions as possible while not exceeding the system limits on the number of arguments a program can take.

You're right for find ... -exec, but that can also be fixed. Just add + at the end of the find command and voila, it behaves like xargs.

None of the solutions above that use kate $(...) (or the equivalent with backticks) work in general (they don't support file names with spaces, will not run at all if the list of files is very long). Both find ... + and xargs get around these limitations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top