如何在不保留目录结构的情况下 tar 目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5695881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I tar a directory without retaining the directory structure?
提问by Brock Boland
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/sites/default/files
.
Is there a way to exclude the parent directories, so that the resulting tar just knows about the last directory (files
)?
有没有办法排除父目录,以便生成的 tar 只知道最后一个目录 ( files
)?
采纳答案by John Gibb
cd /home/username/drupal/sites/default/files
tar czf ~/backup.tgz *
回答by John Gibb
Use the --directory option:
使用 --directory 选项:
tar czf ~/backup.tgz --directory=/home/username/drupal/sites/default files
回答by raf
tar -Cczf ~/backup.tgz /home/username/drupal/sites/default/files
-C does the cd for you
-C 为你做 cd
回答by MaikoID
Hi I've a better solution when enter in the specified directory it's impossible (Makefiles,etc)
嗨,当进入指定目录时,我有一个更好的解决方案,这是不可能的(Makefiles 等)
tar -cjvf files.tar.bz2 -C directory/contents/to/be/compressed .
Do not forget the dot (.) at the end !!
不要忘记末尾的点 (.) !!
回答by sangram
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:
要将所有 txt (*.txt) 文件从源文件的目录结构从/home/myuser/workspace/zip_from/
到压缩/home/myuser/workspace/zip_to/
,请使用以下命令:
tar -P -cvzf /home/myuser/workspace/zip_to/mydoc.tar.gz --directory="/home/myuser/workspace/zip_from/" *.txt
回答by Vishrant
This worked for me:
这对我有用:
gzip -dc "<your_file>.tgz" | tar x -C <location>
回答by Aashutosh Taikar
For me -C or --directory did not work, I use this
对我来说 -C 或 --directory 不起作用,我用这个
cd source/directory/or/file
tar -cvzf destination/packaged-app.tgz *.jar
# this will put your current directory to what it previously was
cd -
回答by Deepesh Rajpal
Create a tar archive
创建 tar 存档
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/