如何在不保留目录结构的情况下给目录加焦油?

我正在编写一个备份脚本,想要修改一个文件目录:

tar czf ~/backup.tgz /home/username/drupal/sites/default/files

这会使它变得更加糟糕,但是当我解压缩结果文件时,它包含了完整的文件结构: 这些文件位于 home/username/drupal/sites/default/files中。

有没有排除父目录的方法,这样产生的 tar 只知道最后一个目录(files) ?

122392 次浏览
cd /home/username/drupal/sites/default/files
tar czf ~/backup.tgz *

Use the --directory option:

 tar czf ~/backup.tgz --directory=/home/username/drupal/sites/default files
tar -Cczf ~/backup.tgz /home/username/drupal/sites/default/files

-C does the cd for you

Hi I've a better solution when enter in the specified directory it's impossible (Makefiles,etc)

tar -cjvf files.tar.bz2 -C directory/contents/to/be/compressed .

Do not forget the dot (.) at the end !!

To gunzip all txt (*.txt) files from /home/myuser/workspace/zip_from/ to /home/myuser/workspace/zip_to/ without directory structure of source files use following command:

tar -P -cvzf /home/myuser/workspace/zip_to/mydoc.tar.gz  --directory="/home/myuser/workspace/zip_from/" *.txt

This worked for me:

gzip -dc "<your_file>.tgz" | tar x -C <location>

For me -C or --directory did not work, I use this

cd source/directory/or/file
tar -cvzf destination/packaged-app.tgz *.jar
# this will put your current directory to what it previously was
cd -

Create a tar archive

tar czf  $sourcedir/$backup_dir.tar --directory=$sourcedir WEB-INF en

Un-tar files on a local machine

tar -xvf $deploydir/med365/$backup_dir.tar -C $deploydir/med365/

Upload to a server

scp -r -i $privatekey $sourcedir/$backup_dir.tar $server:$deploydir/med365/
echo "File uploaded.. deployment folders"

Un-tar on server

ssh -i $privatekey $server tar -xvf $deploydir/med365/$backup_dir.tar -C $deploydir/med365/

Kindly use the below command to generate tar file without directory structure

tar -C <directoryPath> -cvzf <Path of the tar.gz file> filename1 filename2... filename N

eg:

tar -C /home/project/files -cvzf /home/project/files/test.tar.gz text1.txt text2.txt

To build on nbt's and MaikoID's solutions:

tar -czf destination.tar.gz -C source/directory $(ls source/directory)

This solution:

  • Includes all files and folders in the directory
  • Does not include any of the directory structure (or .) in the final product
  • Does not require you to change directories.

However, it requires the directory to be given twice, so it may be most useful in another script. It may also be less efficient if there are a lot of files/folders in source/directory. Adjust the subcommand as necessary.

So for instance for the following structure:

|- source
|  |- one
|  `- two
`- working

the following command:

working$ tar -czf destination.tar.gz -C ../source $(ls ../source)

will produce destination.tar.gz where both one and two (and sub-files/-folders) are the first items.

If you want to tar files while keeping the structure but ignore it partially or completely when extracting, use the --strip-components argument when extracting.

In this case, where the full path is /home/username/drupal/sites/default/files, the following command would extract the tar.gz content without the full parent directory structure, keeping only the last directory of the path (e.g. files/file1).

tar -xzv --strip-components=5 -f backup.tgz

I've found this tip on https://www.baeldung.com/linux/tar-archive-without-directory-structure#5-using-the---strip-components-option.