Linux command # 5

Linux # 5.

tar command

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

Check the contents of the archive file

#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

Deploying the archive

#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

gzip command

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

Since gzip does not have the ability to archive, only one file can be compressed.

Therefore, use the tar command to compress multiple files at once.

[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

Linux command # 4
Linux command # 3
Linux command # 5
linux at command
[Linux] Search command
Linux command <Basic 2>
Linux [directory command]
Linux server command
Linux # Command Memo 1
Linux command [read]
Linux Command Summary
[Basic] linux command
[Linux] Command / Knowledge
My linux command
Linux command <Basic 1>
Linux command collection
Linux mkdir command
Linux command basics
[Linux] Git command
Linux (command memory)
[Linux] Volume configuration command
Linux command (sequential update)
Linux basic command memorandum
Linux command [File operation]
[Linux] Basic command summary
Linux
Linux command for self-collection
linux command error collection 1
Linux command line shortcut
linux sar command CPU usage
[Linux] tar.gz compression / decompression command
What is Linux? [Command list]
Easy df command on Linux
Linux tar xz command memo
Linux Command Dictionary (for myself)
linux: create original Terminal command
[Note] Useful linux command collection
Linux command memorandum [for beginners]
Linux PC spec check command
[Linux] User / group command summary
linux memorandum
Linux commands
[Linux convenient command] Try inserting exa
Linux commands
Linux overview
Linux basics
Command to create Linux Live USB
Command memorandum
Organize Linux
command prompt
[Linux] OS recovery with restore command
Linux practice
Ubuntu Linux 20.04
Completion of docker command on Linux
vim command
[Linux convenient command] Try inserting csview
Permission and ownership change command [Linux]
Linux Summary
LINUX command [wc edition] Usage example
Linux process
Linux command [ldconfig] LPIC learning memo