Of course. Here are several bash one-liners to accomplish this, from the most common and simple to more advanced options.
Option 1: The Standard gzip (Simple & Safe)
This command finds every .NEF file in the target directory, compresses it, and keeps the original file. This is often the best and safest starting point.
cd /path/to/your/dir && find . -maxdepth 1 -iname "*.NEF" -exec gzip -k {} +
How it works:
cd /path/to/your/dir: First, change into the directory where your NEF files are.
&&: This "and" operator ensures the next command only runs if the cd was successful.
find . -maxdepth 1 -iname "*.NEF": Finds all files in the current directory (.) that are not in subdirectories (-maxdepth 1) and whose names end in .NEF (case-insensitive, so it also matches .nef).
-exec gzip -k {} +: For all files found, execute the gzip command.
gzip -k: The -k or --keep flag is crucial. It tells gzip to keep the original file after creating the compressed .gz version.
{}: Is a placeholder for the filename found.
+: Executes gzip with multiple filenames at once, which is more efficient than running it for every single file.
Option 2: Create a Single zip or 7z Archive
This is useful if you want to bundle all your NEF files into a single compressed archive.
Using zip (Very Common)
cd /path/to/your/dir && zip nef-archive.zip ./*.NEF
Using 7z (Better Compression)
You may need to install it first (sudo apt install p7zip-full or sudo yum install p7zip). 7-Zip often provides significantly better compression ratios for RAW files.
cd /path/to/your/dir && 7z a nef-archive.7z ./*.NEF
7z a: The a command means add to archive. It will create nef-archive.7z if it doesn't exist.
Option 3: A Simple for Loop (Easier to Read)
If you prefer a more traditional loop structure, this works perfectly and is easy to understand. This version also keeps the original files.
cd /path/to/your/dir && for f in *.NEF *.nef; do if [ -f "$f" ]; then gzip -c "$f" > "$f.gz"; fi; done
How it works:
for f in *.NEF *.nef: Loop through all files ending in .NEF or .nef.
if [ -f "$f" ]: A safety check to ensure we only process actual files (prevents errors if no .nef files are found, for example).
gzip -c "$f" > "$f.gz": This is the key.
gzip -c: Compresses the file and outputs the result to standard output instead of replacing the original file.
> "$f.gz": Redirects that output into a new file named original_name.NEF.gz.
Important Considerations:
-
Deleting Originals: If you are confident and want to delete the original NEF files after compression, simply remove the -k flag from the gzip command in Option 1.
# DANGER: This deletes the original NEF files!
cd /path/to/your/dir && find . -maxdepth 1 -iname "*.NEF" -exec gzip {} +
-
Subdirectories: If you want to find and compress NEF files in the directory and all its subdirectories, just remove the -maxdepth 1 part from the find command.
# Compresses NEFs in the current directory and all subdirectories
cd /path/to/your/dir && find . -iname "*.NEF" -exec gzip -k {} +
-
Why Compress RAW files? General-purpose compressors like gzip might only save 10-20% on RAW files because the data is already quite noisy (high entropy). For archival, the best practice is often converting to Adobe's Lossless Compressed DNG format, which understands image data and achieves better compression while retaining all RAW information. This, however, requires a tool like Adobe DNG Converter.