Home > front end >  tar folder and exclude all subfolders, then tar to specific path
tar folder and exclude all subfolders, then tar to specific path

Time:11-08

Is there a tar command to exclude sub-directories but include every file in the root of the folder and then place them at specific path?

I would like to omit every subdir, and only keep files max-min depth = 1 I have tried the follwing but its not working:

tar -cvf r.tar --exclude='~/root-dir/[subdir, ... ]' ~/Desktop

root-dir/
|
├── subdir/
│   ├── subdir/
│   │   └── a.txt
│   |
│   ├── subdir/
│   │   └── b.txt
│   │
│   └── c.txt
├── subdir/
│   ├── subdir/
│   │   └── d.txt
│   │   
│   ├── subdir/
│   │   └── subdir/
│   │       └── subdir/
|   |           └── subdir/...
|   
|
└── apple.txt
└── banana.image
└── ... 

CodePudding user response:

first, use find to find the files meeting your criteria:

find ~/Desktop -type f -maxdepth 1

then pipe it to tar, using -T ( or --files-from) to tell tar to get the list of files from stdin:

 find ~/Desktop -type f -maxdepth 1 | \
tar -T - cvf r.tar
  • Related