LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-10-2009, 12:55 AM   #1
yumener
Member
 
Registered: Oct 2005
Posts: 63

Rep: Reputation: 15
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.
 
Old 03-10-2009, 01:13 AM   #2
yumener
Member
 
Registered: Oct 2005
Posts: 63

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by yumener View Post
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?
 
Old 03-10-2009, 01:18 AM   #3
wet-willy
Member
 
Registered: Dec 2008
Posts: 44

Rep: Reputation: 17
What about:
find src_dir -name "*.txt" | cat > /path to/destination/dest_file.txt
OR:
find src_dir -name "*.txt" | cat > less
 
Old 03-10-2009, 01:28 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
find . -name '*.txt' -exec cat '{}' >>new.t \;
 
Old 03-10-2009, 02:00 AM   #5
wet-willy
Member
 
Registered: Dec 2008
Posts: 44

Rep: Reputation: 17
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
 
Old 03-10-2009, 02:15 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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.
 
Old 03-10-2009, 02:21 AM   #7
wet-willy
Member
 
Registered: Dec 2008
Posts: 44

Rep: Reputation: 17
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

Last edited by wet-willy; 03-10-2009 at 02:45 AM. Reason: Added seemingly useless garble
 
Old 03-10-2009, 04:41 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate files sorted by date docaia Programming 5 08-16-2008 09:32 PM
concatenate mpeg (vob) files, command line tool. kaz2100 Linux - Software 2 02-09-2008 06:42 PM
Script to concatenate several files docaia Linux - General 10 02-03-2008 02:59 PM
Concatenate PDF files? mykrob Linux - Software 5 11-07-2006 06:25 AM
concatenate binary files???? justin19fl Linux - Newbie 6 05-14-2001 03:13 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration