通过排除其父文件夹为多个目录创建单个tar文件
I'm working on a bash shell script which creates a tar file for multiple folders by excluding its parent folders to all the directories and also exclude specified folder.
我正在开发一个bash shell脚本,它通过将其父文件夹排除到所有目录并且还排除指定的文件夹来为多个文件夹创建tar文件。
The directory structure is:
目录结构是:
$ ls dir1
dir11 testdir
$ ls dir2
dir12 testdir2
$ ls dir3
dir13 testdir3
$ tar -cvf file1.tar dir1 dir2 dir3 --exclude="test*"
The above works fine.
以上工作正常。
But I dont want the dir1, dir2 and dir3 folders also in tar. I need only inside the files and directories.
但我不想在tar中使用dir1,dir2和dir3文件夹。我只需要在文件和目录中。
So my final tar should contain as following
所以我的最终焦油应该包含如下
$ tar -tvf file1.tar
dir11 dir12 dir13
Can anyone help me out to solve this one. Thanks inadvance
任何人都可以帮我解决这个问题。提前致谢
1 个解决方案
#1
16
If I understand right the question, you can use the -C
(capital C = change directory) option, e.g:
如果我理解正确的问题,您可以使用-C(大写C =更改目录)选项,例如:
tar cvf /tmp/some.tar -C /path/to/dir1 . -C /path/to/dir2 . #multiple -C allowed
check it with
检查一下
tar cf - -C /path/to/dir1 . -C /path/to/dir2 . | tar tvf -
Example:
createing a testcase
创建一个测试用例
cd /tmp
mkdir test
cd test
mkdir -p {dir{1..3},testdir}
touch dir1/file{1..3} dir2/file{4..6} dir3/file{7..9}
the tree is now:
树现在是:
$ find . -print
.
./dir1
./dir1/file1
./dir1/file2
./dir1/file3
./dir2
./dir2/file4
./dir2/file5
./dir2/file6
./dir3
./dir3/file7
./dir3/file8
./dir3/file9
./testdir
The tar:
tar cf - -C dir1 . -C ../dir2 . -C ../dir3 . | tar tvf -
the tar content is:
tar内容是:
drwxr-xr-x 0 jm staff 0 14 aug 13:07 ./
-rw-r--r-- 0 jm staff 0 14 aug 13:07 ./file1
-rw-r--r-- 0 jm staff 0 14 aug 13:07 ./file2
-rw-r--r-- 0 jm staff 0 14 aug 13:07 ./file3
drwxr-xr-x 0 jm staff 0 14 aug 13:08 ./
-rw-r--r-- 0 jm staff 0 14 aug 13:10 ./file4
-rw-r--r-- 0 jm staff 0 14 aug 13:10 ./file5
-rw-r--r-- 0 jm staff 0 14 aug 13:10 ./file6
drwxr-xr-x 0 jm staff 0 14 aug 13:08 ./
-rw-r--r-- 0 jm staff 0 14 aug 13:10 ./file7
-rw-r--r-- 0 jm staff 0 14 aug 13:10 ./file8
-rw-r--r-- 0 jm staff 0 14 aug 13:10 ./file9
If you want something other, please edit your question.
如果您想要其他内容,请编辑您的问题。
更多相关文章
- linux /usr /var /etc 目录
- debain 系统 ll 命令无法使用.目录和文件没有颜色区分解决之道
- Linux 软件安装到 /usr,/usr/local/ 还是 /opt 目录区别
- mysql通过复制data文件夹进行数据迁移
- MySQL——问题:mysql(5.7版本)目录下没有data文件夹
- MySQL数据库阶段学习目录
- 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的
- Android Studio 集成 ShareSDK 如何 覆盖 文件夹
- Android的支持库 && app/apk包进系统 && 预置so库进系统 && 预置