views:

43

answers:

3

Hello,

I am trying to create a file that contains all of the code of an app. I have created a file called catlist.txt so that the files are added in the order I need them.

A snippet of my catlist.txt:

app/controllers/application_controller.rb
app/views/layouts/*

app/models/account.rb
app/controllers/accounts_controller.rb
app/views/accounts/*

When I run the command the files that are explicitly listed get added but the wildcard files do not.

cat catlist.txt|xargs cat > fullcode

I get

cat: app/views/layouts/*: No such file or directory
cat: app/views/accounts/*: No such file or directory

Can someone help me with this. If there is an easier method I am open to all suggestions.

Barb

A: 

Are you positive those directories exist?

The problem with doing a cat on a list like that (where you're using wildcards) is that the cat isn't recursive. It will only list the contents of that directory; not any subdirectories.

Here's what I would do:

#!/bin/bash.exe

output="out.txt"

if [ -f "$output" ]
then 
   rm $output
fi

for file in $(cat catlist.txt)
do
   if [ -f "$file" ]
   then
      echo "$file is a file."
      cat $file >> $output
   elif [ -d "$file" ]
   then
      echo "$file is a directory."
      find $file -type f -exec cat {} >> $output \;
   else
      echo "huh?"
   fi
done

If the entry listed is a directory, it finds all files from that point on and cats them.

DevNull
+1  A: 

Your problem is that xargs is not the shell, so the wildcard is being interpreted literally as an star. You'll need to have a shell to do the expansion for you like this:

cat catlist.txt | xargs -I % sh -c "cat %" > fullcode

Note that the * is not recursive in your data file. I assume that was what you meant. If you want the entries to be recursive, that's a little trickier and would need something more like DevNull's script, but that will require that you change your data file a bit to not include the stars.

Rob Napier
Thank you that worked perfectly!
Barb
A: 

use a while read loop to read your file

while read -r file
do
   if [ -f "$file" ]; then
    yourcode "$file"
   fi       
   # expand asterix 
   case "$file" in 
     *"*" ) 
        for f in $file
        do
           yourcode "$f"
        done
   esac
done <"catlist.txt"
ghostdog74