Linux # 5.
Archive the file
Archive the file attributes as they are. (Permissions, timestamps, etc.)
When executing the tar command as a general user, some file attributes may not be maintained. If the owner is root and only the owner has read permissions, then as a regular user Cannot be archived.
#tar cf <Archive file> <Archive source file path>
#c option means create
#f option means file
[wataru@localhost work]$ tar cf dir1.tar dir1
[wataru@localhost work]$ ls -l
total 20
drwxrwxr-x. 2 wataru wataru 96 Aug 14 05:06 dir1
#.tar has been created
-rw-rw-r--. 1 wataru wataru 10240 Aug 14 05:07 dir1.tar
drwxrwxr-x. 2 wataru wataru 6 Jul 5 04:38 gogodur
drwxrwxr-x. 3 wataru wataru 156 Jul 17 04:25 testgo
-rw-rw-r--. 1 wataru wataru 83 Jul 29 05:56 work.02.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.03.txt
-rw-rw-r--. 1 wataru wataru 25 Jul 21 04:32 work.04.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.05.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.06.txt
#Display the contents of the archive file with the t option
#t represents t in list
[wataru@localhost work]$ tar tf dir1.tar
dir1/
dir1/file-1.txt
dir1/file-2.txt
dir1/file-3.txt
dir1/file-4.txt
dir1/file-5.txt
#To make sure you can restore the original files from the archive
#Delete the dir1 directory in advance
[wataru@localhost work]$ rm -rf dir1
[wataru@localhost work]$ ls -l
total 20
-rw-rw-r--. 1 wataru wataru 10240 Aug 14 05:07 dir1.tar
drwxrwxr-x. 2 wataru wataru 6 Jul 5 04:38 gogodur
drwxrwxr-x. 3 wataru wataru 156 Jul 17 04:25 testgo
-rw-rw-r--. 1 wataru wataru 83 Jul 29 05:56 work.02.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.03.txt
-rw-rw-r--. 1 wataru wataru 25 Jul 21 04:32 work.04.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.05.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.06.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.07.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.08.txt
#Can be expanded by specifying the x option
#x represents x in extract
[wataru@localhost work]$ tar xf dir1.tar
[wataru@localhost work]$ ls -l
total 20
#Make sure the dir1 directory is restored
drwxrwxr-x. 2 wataru wataru 96 Aug 14 05:06 dir1
-rw-rw-r--. 1 wataru wataru 10240 Aug 14 05:07 dir1.tar
drwxrwxr-x. 2 wataru wataru 6 Jul 5 04:38 gogodur
drwxrwxr-x. 3 wataru wataru 156 Jul 17 04:25 testgo
-rw-rw-r--. 1 wataru wataru 83 Jul 29 05:56 work.02.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.03.txt
-rw-rw-r--. 1 wataru wataru 25 Jul 21 04:32 work.04.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.05.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.06.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.07.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.08.txt
[wataru@localhost work]$ tar cvf dir11.tar dir1
#Use option v to list the files that were targeted when creating the archive
#To display
dir1/
dir1/file-1.txt
dir1/file-2.txt
dir1/file-3.txt
dir1/file-4.txt
dir1/file-5.txt
Compress the file
[wataru@localhost work]$ gzip work.02.txt
#gzip <Compression source file>
#.A compressed file is created with the extension gz
[wataru@localhost work]$ ls -l
total 32
drwxrwxr-x. 2 wataru wataru 96 Aug 14 05:06 dir1
-rw-rw-r--. 1 wataru wataru 10240 Aug 21 03:55 dir11.tar
-rw-rw-r--. 1 wataru wataru 10240 Aug 14 05:07 dir1.tar
drwxrwxr-x. 2 wataru wataru 6 Jul 5 04:38 gogodur
drwxrwxr-x. 3 wataru wataru 156 Jul 17 04:25 testgo
-rw-rw-r--. 1 wataru wataru 88 Jul 29 05:56 work.02.txt.gz
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.03.txt
-rw-rw-r--. 1 wataru wataru 25 Jul 21 04:32 work.04.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.05.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.06.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.07.txt
[wataru@localhost work]$ gzip -d work.02.txt.gz
#You can use the d option to decompress the compressed file
[wataru@localhost work]$ ls -l
total 32
drwxrwxr-x. 2 wataru wataru 96 Aug 14 05:06 dir1
-rw-rw-r--. 1 wataru wataru 10240 Aug 21 03:55 dir11.tar
-rw-rw-r--. 1 wataru wataru 10240 Aug 14 05:07 dir1.tar
drwxrwxr-x. 2 wataru wataru 6 Jul 5 04:38 gogodur
drwxrwxr-x. 3 wataru wataru 156 Jul 17 04:25 testgo
-rw-rw-r--. 1 wataru wataru 83 Jul 29 05:56 work.02.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.03.txt
-rw-rw-r--. 1 wataru wataru 25 Jul 21 04:32 work.04.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.05.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.06.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.07.txt
[wataru@localhost work]$ tar cfz dir100.tar.gz dir1
#tar with the tar command+Can create gz files
#This can be achieved by using the z option
[wataru@localhost work]$ ls -l
total 36
drwxrwxr-x. 2 wataru wataru 96 Aug 14 05:06 dir1
-rw-rw-r--. 1 wataru wataru 183 Aug 21 04:42 dir100.tar.gz
-rw-rw-r--. 1 wataru wataru 10240 Aug 21 03:55 dir11.tar
-rw-rw-r--. 1 wataru wataru 10240 Aug 14 05:07 dir1.tar
drwxrwxr-x. 2 wataru wataru 6 Jul 5 04:38 gogodur
drwxrwxr-x. 3 wataru wataru 156 Jul 17 04:25 testgo
-rw-rw-r--. 1 wataru wataru 83 Jul 29 05:56 work.02.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.03.txt
-rw-rw-r--. 1 wataru wataru 25 Jul 21 04:32 work.04.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.05.txt
[wataru@localhost work]$ tar xfz dir100.tar.gz
#tar+Use the x and z options to extract the gz file directly
#You can see that dir1 is expanded
[wataru@localhost work]$ ls -l
total 36
drwxrwxr-x. 2 wataru wataru 96 Aug 14 05:06 dir1
-rw-rw-r--. 1 wataru wataru 183 Aug 21 04:42 dir100.tar.gz
-rw-rw-r--. 1 wataru wataru 10240 Aug 21 03:55 dir11.tar
-rw-rw-r--. 1 wataru wataru 10240 Aug 14 05:07 dir1.tar
drwxrwxr-x. 2 wataru wataru 6 Jul 5 04:38 gogodur
drwxrwxr-x. 3 wataru wataru 156 Jul 17 04:25 testgo
-rw-rw-r--. 1 wataru wataru 83 Jul 29 05:56 work.02.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.03.txt
-rw-rw-r--. 1 wataru wataru 25 Jul 21 04:32 work.04.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.05.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.06.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.07.txt
Recommended Posts