LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to find some files and concatenate them? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-some-files-and-concatenate-them-710399/)

yumener 03-09-2009 11:55 PM

how to find some files and concatenate them?
 
eg. find src_dir -name "*.txt" | cat > dest_file

the above does not work, the content of dest_file is
the path of the files,not the content of each file.

thanks.

yumener 03-10-2009 12:13 AM

Quote:

Originally Posted by yumener (Post 3470384)
eg. find src_dir -name "*.txt" | cat > dest_file

the above does not work, the content of dest_file is
the path of the files,not the content of each file.

thanks.

ok, I know my mistake now. I should run it as,

cat `find src_dir -name "*.txt"` > dest_file.

again, I got another problem, if the result of find is empty, then
the cat command will ask me to input in the terminal, how to
avoid this?

wet-willy 03-10-2009 12:18 AM

What about:
find src_dir -name "*.txt" | cat > /path to/destination/dest_file.txt
OR:
find src_dir -name "*.txt" | cat > less

chrism01 03-10-2009 12:28 AM

find . -name '*.txt' -exec cat '{}' >>new.t \;

wet-willy 03-10-2009 01:00 AM

Sorry, never actually tried it, but tried these:
cat /source_dir/*.txt | less
Or:
cat /source_dir/*.txt > /dest_dir/contents_of_cat_command.txt

jschiwal 03-10-2009 01:15 AM

Use either -exec or -execdir of the find command or pipe the file names to xargs.
find dir/ -name "*.txt" -print0 | xargs -0 cat >cattedtext

post #5 still prints out the list of files and not the contents. The less command will pause after a page of filenames is printed.

wet-willy 03-10-2009 01:21 AM

It does print the contents, just not to a file, it prints it to standard output in such a way that you can read it as it's not flying past your eyes faster than the speed of light.
The second example prints the contents to a file called "contents_of_cat_command.txt" in the destination directory you specify.
As I said, "but tried these".

Edit: Then again, I did this in a Ubuntu 8.10 VM inside of Windows, the directory containing the .txt files is in another NTFS partition..../mnt/hgfs/data

jschiwal 03-10-2009 03:41 PM

I misread the command name & was thinking of find from a previous post. Sorry Wet-Willy. Using cat directly when the files are scattered in different directories and subdirectories is difficult unless all of the files can be located with a wildcard pattern, e.g.: cat *.txt */*/*.txt.

Cat'ing together files like this is of limited use. If one is looking for a particular pattern, using grep to locate that pattern and indicate the filename containing it is more useful.


All times are GMT -5. The time now is 12:05 PM.