Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What I would like to do is to print the contents of all text files in a particular directory, recursively. Problem being that there are directories and possibly binaries scattered around in the filesystem as well.
Trying cat * works as long as there are no directories in there, but when there are it gives an error instead and prints nothing.
I'm sure it's easy using file -f or something but I can't figure it!
catting some non-text files is likely to do strange things to the terminal; it would be prudent to run the file command on each file found and only cat the file if it is a text type. This could most easily be done by writing a script and calling that from find (beware that AlucardZero's +; has been changed to \; so the script is called with a single file name):
Code:
find /dir -type f -exec /path_to_your_script '{}' \;
The script could be something like this (not tested):
It could be made more robust and could loop over multiple file names given as arguments but is probably "good enough" as long as you don't have a lot of files.
Last edited by catkin; 01-23-2011 at 09:27 AM.
Reason: spurious text excised
Excellent! Thanks v much, that worked for my purpose.
@Catkin - you're right about cat-ing non-text files possibly doing strange things, it buggered up my console on one occasion, causing the font to change to some non-ASCII gobbldigook.
Thanks very much for your additional info, i'll try that out.
@Catkin - you're right about cat-ing non-text files possibly doing strange things, it buggered up my console on one occasion, causing the font to change to some non-ASCII gobbldigook.
In that case you can type reset, even if you cannot read what you type, press enter and the terminal restores its normal status.
-exec command ;
Execute command; true if 0 status is returned. All following arguments to find are
taken to be arguments to the command until an argument consisting of ‘;’ is encoun‐
tered. [..cut..]
-exec command {} +
This variant of the -exec action runs the specified command on the selected files,
but the command line is built by appending each selected file name at the end; the
total number of invocations of the command will be much less than the number of
matched files. [..etc..]
What I would like to do is to print the contents of all text files in a particular directory, recursively. Problem being that there are directories and possibly binaries scattered around in the filesystem as well.
Trying cat * works as long as there are no directories in there, but when there are it gives an error instead and prints nothing.
I'm sure it's easy using file -f or something but I can't figure it!
In find . -name "*", -name "*" gives the default behaviour so could as well be find . And spoovy has said only ordinary files are relevant -- not directories or any of the special file types -- so we can cut down on the files passed down the pipeline (some of which, in perverse cases, could cause unwanted effects) by using find . -type f
The grep ASCII is both too loose and too tight. It is too loose in that it will pass files containing ASCII in the name as well as in the type that file reports. It is too tight in that there are many text file types which spoovy probably wants to cat that do not have ASCII in the type that file reports.
The cut -d ":" -f1 will not do as intended for files with names including the : character.
The cat ` ` will not work on files with names including embedded whitespace (space, tab, newline -- only the first is likely) in their names.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.