I have a directory tree in which there are some files and some subdirectories.
/
/file1.txt
/file2.png
/dir1
/subfile1.gif
The objective is to have a script that generates a gzipped version of each file and saves it next to each file, with an added .gz suffix:
/
/file1.txt
/file1.txt.gz
/file2.png
/file2.png.gz
/dir1
/subfile1.gif
/subfile1.gif.gz
This would handle the creation of new .gz files.
Another part is deletion: Whenever a non-gzipped file is created, the script would need to delete the orphaned .gz version when it runs.
The last and trickiest part is modification: Whenever some (non-gzipped) files are changed, re-running the script would update the .gz version of only those changed files, based on file timestamp (mtime) comparison between a file and its gzipped version.
Is it possible to implement such a script in bash?
Edit: The goal of this is to have prepared compressed copies of each file for nginx to serve using the gzip_static module. It is not meant to be a background service which automatically compresses things as soon as anything changes, because nginx’s gzip_static module is smart enough to serve content from the uncompressed version if no compressed version exists, or if the uncompressed version’s timestamp is more recent than the gzipped version’s timestamp. As such, this is a script that would run occasionally, whenever the server is not busy.
Something like this, maybe?
If you have a Makefile already, by all means use a literal file rather than a here document.
Integrating with
findleft as an exercise. You might want to accept multiple target files and loop over them, if you want to save processes.