[SOLVED] How to run a single program on consecutive files and send output to new files.
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to run a single program on consecutive files and send output to new files.
Hello all, I'm new to linux and I have a question about running a java program on text files and sending the output to new files with the same name but adding a prefix.
Right now I can do this with a single file using the following command.
cat filename.txt | java -cp (java program stuff) > newfile.txt
I want to know if there is a way I can take a mass of files (all located in a single folder), run this program on each individual file and send the output to new files using the filename of the original as part of the new file name?
I tried the for loop and an odd thing is happening. This error continues to repeat itself. "cat: : No such file or directory" Oddly in the folder I directed the output to a single empty file named with only the prefix I wanted to add now exits.
I am typing in the command as such. Am I doing something wrong?
for files in FilesToProcess/* ; do cat "$file" | java -cp (java program stuff) > newFolder/new_"$file" ; done
1) it needs to be "for file in ...", not "for files in ....". Notice how later on you're referencing $file, this needs to match the variable you are using at the start of the for loop.
2) you can't prefix the file with new_"$file" if you give it the full path, otherwise it'll stick the new_ before the directory structure (ie: FilesToProcess/file.txt becomes new_FilesToProcess/file.txt). You'll need to use a combination of dirname and basename if you want to prefix it:
Code:
for file in /path/to/files/* ; do dir=$(dirname "$file"); base=$(basename "$file"); cat "$file" | java -cp (java program stuff) > "$dir/new_$base" ; done
Thank you, your solution worked. Though I must've done something wrong as the processed files ended up in the original folder rather than the destination, but do to the prefix being added moving them to the proper folder wasn't a problem.
Didn't notice you wanted to move them to a new folder, in that case just remove the dir=$(dirname "$file") line, and hard code your destination folder in the java output:
Code:
for file in /path/to/files/* ; do base=$(basename "$file"); cat "$file" | java -cp (java program stuff) > "/path/to/newFolder/new_$base" ; done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.