Operating System - HP-UX
1846334 Members
2992 Online
110256 Solutions
New Discussion

HELP needed with simple script

 
SOLVED
Go to solution
Declan Heerey
Frequent Advisor

HELP needed with simple script

I need a simply solution to the following problem; I have a very basic script which i want to run against every directory in a list OR every directory below a root directory. For example at the moment i do the following

cd /directory_1
for i in `ls *`
do
cat $i >> file
done

cd /directory_2
for i in `ls *`
do
cat $i >> file
done

etc etc

Is there a way of doing this in one sweep? I ALSO need to concatenate the files in a particular order

Thanks in advance - Declan
7 REPLIES 7
John Waller
Esteemed Contributor

Re: HELP needed with simple script

If you want to cat all files from a root starting point then this works:
find /start -type f -exec cat {} >> file \;
H.Merijn Brand (procura
Honored Contributor
Solution

Re: HELP needed with simple script

Above can be done much faster as

--8<---
cd /dir1
cat * >>file

cd /dir2
cat * >>file
-->8---

be sure that 'file' is not in any of these dir's, because that will yield unexpected errors

be aware that '*' will exclude all files that start with a dot (.) and it includes directories, which might also not be what you want

If the directories you want do not contain subfolders, you could do

# find /dir1 /dir2 /dir3 -type f -exec cat {} >>file \;

or if there are not too many files, faster

# find /dir1 /dir2 /dir3 -type f | xargs cat >file

As you didn't tell us where and how the order of the files is determined, I cannot answer that question (yet).

Enjoy, Have FUN! H.merijn
Enjoy, Have FUN! H.Merijn
Ravi_8
Honored Contributor

Re: HELP needed with simple script

Hi
try this

cd /
for i in *
do
if (-d $i)
cd $i
for x in *
cat $x >> file
done
fi
done
never give up
Declan Heerey
Frequent Advisor

Re: HELP needed with simple script

I agree and that would work fine BUT i have to concate the files in a particular order i.e. dir_3, dir_1 dir_a etc
Declan Heerey
Frequent Advisor

Re: HELP needed with simple script

Aghhh, i like the # find /dir1 /dir2 /dir3 -type f -exec cat {} >>file \; and this should work a treat. Don't know why i didn't think of it - Mind block brought on by Christmas festivities

Thanks
Declan Heerey
Frequent Advisor

Re: HELP needed with simple script

can you explain why the xargs is faster than the exec?
H.Merijn Brand (procura
Honored Contributor

Re: HELP needed with simple script

-exec starts a 'cat' process for each file, xargs starts a single 'cat' process for all files.

context switches are expensive on multiuser systems

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn