3

Say my folder is like below

- folder
 - file1.json
 - file2.json

I need a shell script which will give me this after running:

- folder
 - file1.json
 - file2.json
 - file1.sh
 - file2.sh

After getting the file created. I need then fill up file1.sh and file2.sh with content which partially is copied from file1.json and file2.json accordingly.

I could do this line by line in below way, but this is not practical when I have many files.

touch file1.sh
cat somecontent.txt >> file1.sh
cat file1.txt >> file1.sh
cat othercontetn.txt >> file1.sh
touch file2.sh
cat somecontent.txt >> file2.sh
cat file1.txt >> file2.sh
cat othercontetn.txt >> file2.sh

I am looking for a method auto parse the folder and create files based on whatever files inside the folder. Could first assume there are only files inside folder. But actually in my case, the best solution should work for situation that there are folders inside folder.

0

2 Answers 2

3

A method auto parse the folder and create files based on whatever files inside the folder.

The following code finds .json and create same content files with {same prefix}.sh

find /folder -name "*.json"  -exec bash -c 'cat "$1" > "${1%.*}.sh"' _ {} \;
Sign up to request clarification or add additional context in comments.

Comments

1

A simple Bash loop will do

for f in folder/*.json
do cat folder/somecontent.txt "$f" > "${f%.json}".sh
done

3 Comments

if there are folder2 under folder, and there are json files inside folder2. Any way for this case?
I know I could do it fold by fold but just curious if any search could go for json files as deep as possible?
@Acton for recursive lookup find is a better alternative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.