如何在不保留目录结构的情况下对目录进行tar处理?

时间:2022-03-28 01:51:29

I'm working on a backup script and want to tar up a file directory:

我正在编写一个备份脚本,并希望对文件目录进行跟踪:

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

This tars it up, but when I untar the resulting file, it includes the full file structure: the files are in home/username/drupal/sites/default/files.

这将它设置为,但是当我解压缩结果文件时,它包含完整的文件结构:文件在home/username/drupal/站点/默认/文件中。

Is there a way to exclude the parent directories, so that the resulting tar just knows about the last directory (files)?

是否有一种方法可以排除父目录,以便产生的tar只知道最后一个目录(文件)?

5 个解决方案

#1


23  

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

#2


140  

Use the --directory option:

使用——目录选项:

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

#3


32  

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

你好,我有一个更好的解决方案当我进入指定的目录时是不可能的(makefile,等等)

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

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

别忘了最后的那个点(.)!

#4


1  

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:

要对来自/home/myuser/workspace/zip_from/ To /home/myuser/workspace/zip_to/的所有txt (*.txt)文件进行gunzip压缩,源文件的目录结构如下:

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

#5


-1  

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

-C does the cd for you

c为你做cd

#1


23  

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

#2


140  

Use the --directory option:

使用——目录选项:

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

#3


32  

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

你好,我有一个更好的解决方案当我进入指定的目录时是不可能的(makefile,等等)

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

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

别忘了最后的那个点(.)!

#4


1  

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:

要对来自/home/myuser/workspace/zip_from/ To /home/myuser/workspace/zip_to/的所有txt (*.txt)文件进行gunzip压缩,源文件的目录结构如下:

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

#5


-1  

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

-C does the cd for you

c为你做cd