Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cat command question

Status
Not open for further replies.

ilovelinux2006

Programmer
Jun 6, 2006
32
US
Hello everyone,

I have a directory with about 100 folders. I cannot use the command Cat file1 file2 fil3 file4 > Main.txt

Is there anyway I can use Cat for the entire directory? Lets say the directory is called "files". Thanks!
 
Hi

If the directory structure depth is 1, then simple :
Code:
cat * */*
But the nicer solution is with [tt]find[/tt] ( if your [tt]find[/tt] has -exec option ) :
Code:
find . -exec cat '{}' \;

Feherke.
 
I'm not near a unix box, but what about using find:

Code:
find /path/to/files -type f -print|while read myfile
do
   cat $myfile >> Main.txt
done
 
for i in `ls`;do
cat $i > main.txt
done

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
mrn said:
for i in `ls`;do
cat $i > main.txt
done

Mike

UUoLS

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
And Mike, you get only the last file in main.txt ...

for i in *
do
test -f $i && cat $i
done > main.txt



HTH,

p5wizard
 
OOps, forgot to post the Usefull Use of LS:
for f in `ls 2>/dev/null`; do ...
 
PHV said:
vlad said:

ls is still usefull if the directory could be empty ...
How?

Won't you have to create an empty 'main.txt' anyway?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Won't you have to create an empty 'main.txt' anyway?
Sorry, I don't see the problem.
My point is to not enter the loop with a not globbed *

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
for i in `ls |grep -v main.txt`;do
cat $i >> main.txt
done

would * not include . & ..

and as far as UUoLS - IDGAF it works

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
mrn said:
and as far as UUoLS - IDGAF it works
"whatever makes the customer happy" - settin' the bar low is always helpful.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top