Running shell script on the contents of a folder
Posted: Mon Jan 20, 2014 4:56 pm
I am trying to automate the collection of individual .txt files into a single, larger file. To do this, I wrote a shell script that works when I run it in terminal:
I have a number of subfolders I'd like this to apply to and keep those compiled documents in their original subfolders. I have a go into subfolders rule in place on the parent folder. I then made a simple rule that matches any file with the .txt extension
Obviously, I don't want the rule to run on the .txt file, but run in any directory that contains .txt files.
When I run this, I get a COMPILED document in the parent folder that is empty even though there are text files in the subfolders. I did some reading and found that I need to tell the shell script to CD to the folder of interest. When I followed up on this, I found some issues with the $0 and $1 variables. Instead of giving a file name, I get the entire contents of the text document. For example, $0 gives all of the code for the shell script and $1 gives the one line of text I put in the test.txt. When I use $0 or $1 in the terminal, I get the expected script name/first argument.
I think I need a way to get the path for the directory containing the text files to be compiled, but all the examples I've seen for this are based on using $1. Is this an issue with my shell script or the way I'm setting up the hazel rules?
Thanks for your help
- Code: Select all
for f in *.txt ; do
cat "$f" >> COMPILED
echo "***###***" >> COMPILED
done
rm *.txt
I have a number of subfolders I'd like this to apply to and keep those compiled documents in their original subfolders. I have a go into subfolders rule in place on the parent folder. I then made a simple rule that matches any file with the .txt extension
- Code: Select all
If all of the following conditions are met:
Extension is .txt
Do the following to the matched file or folder:
Run shell script embedded script
Obviously, I don't want the rule to run on the .txt file, but run in any directory that contains .txt files.
When I run this, I get a COMPILED document in the parent folder that is empty even though there are text files in the subfolders. I did some reading and found that I need to tell the shell script to CD to the folder of interest. When I followed up on this, I found some issues with the $0 and $1 variables. Instead of giving a file name, I get the entire contents of the text document. For example, $0 gives all of the code for the shell script and $1 gives the one line of text I put in the test.txt. When I use $0 or $1 in the terminal, I get the expected script name/first argument.
I think I need a way to get the path for the directory containing the text files to be compiled, but all the examples I've seen for this are based on using $1. Is this an issue with my shell script or the way I'm setting up the hazel rules?
Thanks for your help