1

I'm trying to learn bash -slowly- but finding it difficult to get a grasp of the more technical aspects without a real-word example that fits something I would want to use. To that end, I'd like to know how to use bash (and 7zip) to archive some photos in preparation for storing them online.

Essentially, I imagine this is as 'simple' as making a .7z archive of each directory, assigning each archive the same name as the relevant directory. All directories have been named without spaces, should that matter.

Grateful for all advice.

4

The way to compress a directory foo to an archive named foo.7z using 7z is to use command a ("add"):

7z a foo.7z foo

To do so for multiple directories, one could use a loop:

for dir in foo bar baz; do
    7z a "$dir".7z "$dir"
done

(This is safe against directory names that include spaces, because $dir is quoted, but not against directory names that start with a dash; those would result in 7z attempting to interpret them as options.)

If the name of directories you intend to archive share a pattern, then you could use that in the for loop, instead of listing all directories explicitly, e.g. for dir in [0-9][0-9][0-9][0-9][0-9][0-9]-*; ....

Note that the 7z documentation warns against using the 7z format for archival purposes on Unix, because it doesn't preserve ownership information. This may or may not matter for you.

  • I don't know if this answers your question; please comment if not. – dhag Dec 14 '15 at 17:02
  • Thanks @dhag, the for loop idea is just what I'm looking for. Unfortunately, the naming pattern I have for the photos isn't as convenient as that, although they all start with six numbers and a dash to signify a date, instead being descriptive of occasions, people, etc. (e.g. "131225-Christmas-Day"). Does this mean I have to explicitly list every directory to be archived (trivial with 'ls', I assume), and that there's no way of more simply having the loop act on all directories? – Tay Dec 14 '15 at 19:15
  • 1
    I updated my answer to include an example that matches the format you showed: file names starting with six digits, followed by a dash. – dhag Dec 14 '15 at 19:19
  • I forgot to say that I don't need to keep any Linux ownerships or permissions in this case, so 7z will be fine. – Tay Dec 14 '15 at 19:19
  • Brilliant! Even before watching it do exactly what I wanted, seeing the format of your updated answer and how it applied to my photos was instantly explanatory. Thanks again! – Tay Dec 14 '15 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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