For example, you want to create an archive of your /home directory, but exclude Downloads, Documents, Pictures, Music directories. This is where -exclude switch comes in help. You can exclude the non-important files or directories from your backup. This is quite useful when backing up your data. Exclude directories and/or files from while creating an archive The above command will create an archive of Downloads, Documents directories and file.odt file in ostechnix directory and save the archive in the current working directory. To create an gzipped archive of multiple directories or files at once, use this command: $ tar czvf ostechnix.tgz Downloads/ Documents/ ostechnix/file.odt This is another coolest feature of tar command. Or, extract the archive to some other directory: $ tar xjf 2 -C Downloads Create archive of multiple directories and/or files at a time Now, let us extract bzipped archive in the current directory. Or, $ tar cjf archive.tbz file1 file2 file3
$ tar xzf ostechnix.tgz -C Downloads/Ĭreate an archive of a directory: $ tar cjf 2 ostechnix/Ĭreate archive from list of files: $ tar cjf 2 file1 file2 file3 To extract the archive in a different folder, use -C flag. To extract the gzipped archive in the current directory, use: $ tar xzf ostechnix.tgz To create gzipped archive from a list of files: $ tar czf archive.tgz file1 file2 file3 You can use v flag to view the progress while creating the archive. Here, we use z flag to compress the archive using gzip compression method. tbz refers bzipped archive, respectively.įirst, let us create a gzipped archive: $ tar czf ostechnix/ tgz refers a gzipped archive, and the tar files ends with tar.bz2 or. tar extensions refer the plain tar archive, the files ends with tar.gz or.
Also, tar command can be used in conjunction with the compression utilities gzip and bzip2. $ tar xf ostechnix.tar -wildcards "*.png" Create gzipped and bzipped archivesīy default, Tar creates the archive file ends with. For example, the following command extracts the ".png" type files. Some times you may want to extract files of a specific type. $ tar xf ostechnix.tar -C Downloads/Īlternatively, go to the Downloads folder and extract the archive inside it like below. For example, the following command extracts the given archive file in Downloads directory. We can also extract the archive in a different directory using C flag(capital c). To extract an archive in the current directory, simply do: $ tar xf ostechnix.tar Similarly, to create an archive from a set of files in the current working directory, use this command: $ tar cf archive.tar file1 file2 file 3 Extract archives Here, c flag refers create new archive and f refers the file name. Now, let us create a new tar archive of the the directory ostechnix. Create a new archiveįor the purpose of this guide, I will be using a folder named ostechnix that contains three different type of files.
#LINUX COPY DIRECTORY AND CONTENTS HOW TO#
In the first part, we will see how to archive files and directories using Tar command.
So, I am going to publish this article in two parts.