Tar command

I want to tar multiple folder from a environment but exclude 2 folders among them. How can I do that. Is there any exclude option in tar command.

Please co-operate me.

Thanking you,

Chandrakant.

for i in *
do
   [ -d "$i" ] && [ "$i" != "folder1" "$i" != "folder2" ] && tar ...
done

Suposse I want to exclude the directory named A and B then how the code will be please suggest me.

for i in *
do
   [ -d "$i" ] && [ "$i" != "A" -o "$i" != "B" ] && tar ...
done

If you are using GNU tar, you have the --exclude option

tar cvf myfile.tar /path/to/dirs --exclude /path/to/dirs/A --exclude /path/to/dirs/B

Cheers
ZB

Using SunOS
Echo $SHELL /bin/sh

I Tried

1) tar -cvf test.tar /home/nims/chandrakant/* -X /home/nims/chandrakant/A -X /home/nims/chandrakant/B

2) tar -cvf test.tar /home/nims/chandrakant/* --exclude=/home/nims/chandrakant/A --exclude=/home/nims/chandrakant/B

Still these directories are getting added in same tar file.

Please suggest me what should I do.

for i in *
do
   [ -d "$i" -a "$i" != "A" -a "$i" != "B" ] && dir_lst=${dir_lst}" "${i}
done

dir_lst="tar -cf new.tar ${dir_lst}"
echo ${dir_lst}" | sh
1 Like

You can try something like that :

cd /home/nims/chandrakant
tar cvf test.tar $(ls -d * | egrep -v '^(A|B)$')

Jean-Pierre.

I have tried the below one and it is working file.

tar -cvf test.tar `ls | grep -v A | grep -v modify`

grep -v A --> It will exclude the folder/file name A.

In this way we can create a tar file by excluding the desired files/folders.
we can use find command too in the `find . -name or -type` rather than ls command.

I think it is more flexible. Thanking you for your co-operation.