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.

如果您想要其他内容,请编辑您的问题。

更多相关文章

  1. linux /usr /var /etc 目录
  2. debain 系统 ll 命令无法使用.目录和文件没有颜色区分解决之道
  3. Linux 软件安装到 /usr,/usr/local/ 还是 /opt 目录区别
  4. mysql通过复制data文件夹进行数据迁移
  5. MySQL——问题:mysql(5.7版本)目录下没有data文件夹
  6. MySQL数据库阶段学习目录
  7. 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的
  8. Android Studio 集成 ShareSDK 如何 覆盖 文件夹
  9. Android的支持库 && app/apk包进系统 && 预置so库进系统 && 预置

随机推荐

  1. 安卓---EditText控件的使用
  2. 【Android】RelativeLayout相对布局属性
  3. android下eclipse中This LinearLayout la
  4. android布局RelativeLayout中android:gra
  5. Android 开发————1、Android基础简介
  6. Android Trick 2: Android中的透明属性的
  7. Android UI之GridView
  8. Android 中启动自己另一个程序的activity
  9. Android:安卓资源引用符号的含义
  10. 【译】Android系统简介