0

I have a directory structure

DIR
        SUBDIR1
                    11-01-11.txt
                    13-05-23.txt

        SUBDIR2
                    12-05-56.txt
                    13-04-02.txt
                    15-04-06.txt

I would like to write a bash script that leads to this Desired output

DIR
        SUBDIR1
                    sub_dir1_merged.txt

        SUBDIR2
                    sub_dir2_merged.txt

I would like to maintain the original directory structure, merge files into a single sub_dirname_merged.txt file for each and delete all original files.

I tried the following code

for f in */
do
cat $f/*.txt > "$f"/$f_merged.txt
rm $f/*.txt 
done

this does not work fully.

4
  • Your redirection > doesn't append the content, it rewrites the file every loop. You have to use >>. – schrodigerscatcuriosity Jan 15 at 20:19
  • okay, i can try out the append >> but how to delete the original files while keeping the new.txt file? – sheth7 Jan 15 at 20:24
  • The problem is you are looping over the do directories, no the txt files. – schrodigerscatcuriosity Jan 15 at 20:27
  • can you suggest a different approach with commands? Thanks. – sheth7 Jan 15 at 20:28
2

The script:

for f in */*.txt; do 
  cat "$f" >> "$(dirname "$f")/$(dirname "$f")_merged.txt" 
  rm "$f"
done

Loop the txt files across the subdirectories:

for f in */*.txt; do 

$(dirname "$f")

returns the name of the folder in which the txt resides wich is used both to name the file and the path to save the file.


rm "$f"

removes the file. When you are going to use rm command be sure the result of the script is what you expected before executing it.

2

You can use find in combination with -execdir flag. It gets as simple as the following:

find DIR/ -type f -execdir bash -c 'cat -- $1 >> "${PWD##*/}_merged.txt"' _ {} \;

I slightly modified the above one-liner so the original files are removed.

find DIR/ -type f -execdir bash -c 'for f; do cat -- $f >> "${PWD##*/}_merged.txt"; done;  [[ $f != *${PWD##*/}* ]] && rm -v "$f"' _ {} \;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.