Question : tarring multiple files in mutliple directories

Hello All.

I want to tar multiple files ending with ".txt' extension in multiple sub directories under single main directory. I am looking to tar files only but not sub directories. I have a tar script right now which access each subdir and tars the files. something like

cd /basedir/subdirA
tar -cvf  all the text files in that dir and so on.

Also I would like to know if there is anyway to tarr all text files in all subdirectories to single tar ball?
Following is my code
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
#!/bin/sh
cd $BASEDIR/subdirA
FILES=$(find . -type f -name "*.txt.*")
 
if [[ -z "${FILES}" ]]; then
tar -cvf /archive/Files_From_subdirA_archive_$(date +'%m%d%Y').tgz  ${FILES}
rm ${FILES} #...to remove what you archived...
else
   echo " no files to tar"
fi
 
### tar files from subdir B
cd $BASEDIR/subdirB
FILES=$(find . -type f -name "*.txt.*")
 
if [[ -z "${FILES}" ]]; then
tar -cvf /archive/Files_From_subdirB_archive_$(date +'%m%d%Y').tgz  ${FILES}
rm ${FILES} #...to remove what you archived...
else
   echo " no files to tar"
fi
so on..
Open in New Window Select All

Answer : tarring multiple files in mutliple directories

Please put --remove-files after all option. Tar command should look like:

tar -cvf  /backup/archive.tar -T - --remove-files
Random Solutions  
 
programming4us programming4us