Sebastian
Sebastian

Reputation: 141

bash script: extracting and converting files

I would like to write a bash script that extracts files from a .zip file and converts them into text files - pretty much adds ".txt" at the end of the file name as they are all text files but do not have an extension.

I am pretty much new to shell. I've found this:

cd /path/to/files
for i in *.gz
do
    gunzip $i
done
for i in *.zip
do
    unzip $i
done

I imagine it extracts the files, but how do I then rename/convert them?

Upvotes: 2

Views: 590

Answers (1)

anubhava
anubhava

Reputation: 786091

You can use use extglob to find all files that don't have .txt extension:

shopt -s extglob

for f in !(*.txt); do
   mv "$f" "$f".txt
done

PS: You can also use !(*.txt) pattern to match all the files with no extension.

Upvotes: 2

Related Questions