Linux tar komutu: Bir dizini sıkıştırarak ama alt dizinleri exluding

2 Cevap php

Scenario: I have a directory on a server that hosts my website that contains hundreds of user-submitted images. I am creating a backup script that takes all the images from the directory and compresses them into one .tar.gz file. The command I have so far is:

tar -czpf /path/to/backups/my_backup.tar.gz path/to/images/

Problem: No inside my path/to/images/ I have a directory called tmp/. When I run the command, I get a .tar.gz file containing all the image in the path/to/images/ directory and a subdirectory called tmp/.

Question: How can I get the command to skip/not include the tmp/ subdirectory in the .tar.gz file.

Teşekkür a milyon peşin.

2 Cevap

Sen --exclude argüman arıyoruz.

--exclude=PATTERN

Bazı kullanıcılar exclude seçenekleri kafa karıştırıcı bulabilirsiniz. İşte bazı ortak tuzaklar şunlardır:

  • The main operating mode of tar does not act on a path name explicitly listed on the command line if one of its file name components is excluded. In the example above, if you create an archive and exclude files that end with *.o, but explicitly name the file dir.o/foo after all the options have been listed, dir.o/foo will be excluded from the archive.

  • You can sometimes confuse the meanings of --exclude=PATTERN and --exclude-from=FILE-OF-PATTERNS (-X FILE-OF-PATTERNS). Be careful: use --exclude=PATTERN when files to be excluded are given as a pattern on the command line. Use --exclude-from=FILE-OF-PATTERNS to introduce the name of a file which contains a list of patterns, one per line; each of these patterns can exclude zero, one, or many files.

  • When you use --exclude=PATTERN, be sure to quote the PATTERN parameter, so GNU tar sees wildcard characters like *. If you do not do this, the shell might expand the * itself using files at hand, so tar might receive a list of files instead of one pattern, or none at all, making the command somewhat illegal. This might not correspond to what you want.

Örneğin, yazın:

$ tar -c -f ARCHIVE.TAR --exclude '*.o' DIRECTORY

yerine:

$ tar -c -f ARCHIVE.TAR --exclude *.o DIRECTORY
  • You must use use shell syntax, or globbing, rather than regexp syntax, when using exclude options in tar. If you try to use regexp syntax to describe files to be excluded, your command might fail.

Bu (GNU tar varsayarak) çalışması gerekir:

tar -czpf /path/to/backups/my_backup.tar.gz --exclude=tmp path/to/images/