Concatenate files in Ubuntu

Case Study
After every Data science marathon match in Topcoder, the organizer provides all submissions of codes in a zip file. After extracting this file, you will see lot of individual submissions as zip file. So to see the solution of each submission, I had to unzip each submission and see the code as languages such as c++, python, csharp or c. This is time consuming. I wanted to combine all language specific submissions into a single file so that I don’t have to unzip back and forth to see code.

Solution

First I made a script to unzip all zip submissions into separate directories.

for file in *.zip
do
    directory="${file%.zip}"
    unzip "$file" -d "$directory"
done

The above code segment is stored as command.sh

The commands to execute this file:

chmod +x command.sh
./command.sh

These commands will unzip all zip files and put unzip contents in newly created directories.

Next we want to extract all c++ files in those sub-directories and concatenate all contents in a single file named allcpp.txt

The command for that is

find . -type f -name "*.cpp" -exec cat {} + >allcpp.txt

The same is for concatenating all python and csharp files:

find . -type f -name "*.py" -exec cat {} + >allpy.txt
find . -type f -name "*.cs" -exec cat {} + >allcs.txt

Reference:
https://stackoverflow.com/questions/2374772/unzip-all-files-in-a-directory/29248777
https://www.javatpoint.com/steps-to-write-and-execute-a-shell-script

Leave a comment