Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a large archive of edited images from my camera, each image is actually a pair of files - *.nef & *.xmp. I would like to go through all the folders and then zip these pairs into single tgz files.

Each of the filenames in a directory is unique, so it would be sufficient to move the file names from output of ls *.nef to the command tar -czf {filename}.tgz {filename}.*, but I'm afraid I've no idea where to start.

Thanks in advance.

share|improve this question
1  
You need to take care when thinking of Linux and Unix filenames: they do not have "extensions" except by convention. –  Bruce Ediger May 19 '11 at 16:50
1  
OK, that comment isn't helpful. You need to take care when thinking of Linux and Unix filenames: they do not have "extensions" except by convention. That is, "file.nef" and "file.xmp" have different filenames. The ".nef" and ".xmp" are parts of the filenames, not separate fields kept apart from the file name proper, like in FAT filesystems, or ODS-11. –  Bruce Ediger May 19 '11 at 16:57
add comment

2 Answers 2

up vote 3 down vote accepted

A short script, xfun:

#!/bin/bash
b=$(basename "$1" .xmp)
# echo "tar -cjf $b.tar.bz2 $b.xmp $b.nef"
tar -cjf "$b.tar.bz2" -- "$b.xmp" "$b.nef"

Invocation:

find -name "*xmp" -execdir ./xfun {} ";" 
share|improve this answer
    
works on my linux box, but my nas doesn't have find - are there alternatives? –  Martin KS May 19 '11 at 14:30
    
in a flat directory - if you don't want to visit subdirectories: for f in *.xmp; do ./xfun "$f"; done –  user unknown May 19 '11 at 14:45
    
Quickly riding this question: I have a similar problem as the OP, but not all .xmp files have a paired .nef file. Is there a way to execute the command only for those .xmp files that have a similarly named .nef? –  Gaminic Jun 16 at 11:13
    
b) Describe 'similarly'. a) For a new question, create a new question. You might then link to this question, pointing out, that it is a similar problem. This should give you much more attention. Note that longer scripts/commands in comments are hardly readable. –  user unknown Jun 16 at 22:28
add comment

In zsh or bash ≥4:

for x in **/*.nef; do
  x=${x%.nef}
  if [ -e "$x.xmp" ]; then
    tar -czf "$x.tgz" -- "$x.nef" "$x.xmp" && rm -- "$x.nef" "$x.xmp"
  fi
done

Remove the && rm … part if you don't want to delete the archived files.

Remove the **/ part on the first line to only act in the current directory and not in its subdirectories as well (and then the snippet will work in any shell).

Some old tar implementations may not support -z to create a gzip archive; then you can use tar -cf "$x.tar" -- "$x.nef" "$x.xmp" && gzip -o "$x.tgz" -- "$x.tar" && rm …. If you don't want to delete the files, another possibility is tar -cf - -- "$x.nef" "$x.xmp" | gzip >"$x.tgz" (this form can't be combined with && rm because it would delete the source files even if the tar command failed).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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