Server Management/LInux Server

[추천] tar 옵션

BAGE 2008. 12. 1. 23:55

압축 -cvf or cvfz

tar cvf test.tar a b
tar cvfz test.tar.gz a b


해제 -xvf or -xvfz

tar xvf test.tar
tar xvfz test.tar.gz

목록확인 -tf or tvf or --list

tar tf test.tar
tar tvf test.tar
tar tvf test.tar


tar 파일내부의 파일삭제 --delete

tar --delete -vf test.tar.gz .bashrc


잘못푼파일 삭제 (ftz + 파이프 + xargs)

tar ftz test.tar.gz | xargs rm -rf




tar and untar - part deux

Now we've got the basics of tar and untar completed, we can move onto some more advanced (and useful) techniques.

So let's see how to add a single file or folder to existing backups. What about deleting individual files without extracting the whole archive?


Let's start with a common requirement by adding a file or folder to an existing tar archive.

Why would we do this? Well, it's a lot easier adding a file to an existing archive than extracting the archive, adding the file and then tarring it again, especially if the archive is large.

Add a file

To add a file to an existing archive:

tar rvf dest.tar myfile.txt

The command uses the 'r' option which is the short form of 'append'. In this case, the file called 'myfile.txt' is added (appended) to the tar archive named 'dest.tar'.

Add a directory

Exactly the same procedure is used to add a directory:

tar rvf dest.tar myfolder/

In that case, the directory name 'myfolder' was added to the archive named 'dest.tar'.

Listing contents

Don't forget that you can check the procedure worked by listing the contents of the archive:

tar tvf dest.tar

Deleting files

In the same way as wanting the ability to add files to our archive, we want to be able to delete files and folders.

As you would imagine, this is pretty simple:

tar --delete -vf dest.tar myfile.txt

Note the syntax of the command. The delete option is followed by the '-vf' options. Where as the 'v', meaning 'verbose' is optional, the 'f' option, meaning that this is not actually a tape drive but a file system, is not.

So that command would delete the file named 'myfile.txt' from the archive named 'dest.tar'.

Deleting folders

To delete a folder from the archive, append the folder name to the command:

tar --delete -vf dest.tar myfolder/

As expected, this removes the directory named 'myfolder' from the archive.

Excluding files

We know that creating an archive is fairly simple. However, the situation often occurs where you have a directory of files you want to archive but there are some files you want to leave out.

This is where the 'exclude' option comes in:

tar cvf dest.tar --exclude='myfile.txt' myfolder/

The format is very similar to creating an full archive. However, this time we excluded the file named 'myfile.txt'.

Exclude list

It can be convenient to create a list of files to exclude from the archive, especially when using tar for regular backups.

To do this, create a file named 'exclude.txt' and enter each filename to be excluded.

The file may look like this:

myfile.txt
myfile2.txt
.config

Now when issuing the archive command you would use the 'X' option:

tar cvf dest.tar -X exclude.txt myfolder/

As you may expect, the archive name 'dest.tar' is created from the contents of 'myfolder' but the list of files in 'exclude.txt' have not been included.

man

There are, of course, more options available with the tar command and, as you can see, it's very flexible and can be used for more than creating 'simple' backups.

Ask the man about tar. He'll tell you more:

man tar

PickledOnion.