Monday, June 17, 2019

Linux: How to tar only selected folders and files

Step 1: Select the files and prepare a list.

I do this with a simple function in my .bashrc script:

$> cat >> ~/.bashrc
99 function cscopesetup()
100 {
101     rm cscope.files;

103     echo "Populating filenames...";
104     find $SRC/folder1/ -iname "*.c" -print >> cscope.files #folder1
105     find $SRC/folder9/ -iname "*.h" -print >> cscope.files #folder9
106     echo "Driver done"
107 }

Run this from the bash command:
$> source ~/.bashrc

This will create a file on your home directory with the name cscope.files

If you open this file, you should see the names of all the files you want to tar and backup.

Step 2: Now tar the files
$> tar -cvf myfile.tar -T cscope.files

This will do it. The -T switch takes a file with a list of filenames as an input.