Click here for "Safe Air Conditioner Repair and Proper Freon Recovery".(Japanese Version)

File manipulation with tar command

File manipulation with tar command

●View the contents of the archive created by the tar command

To view the contents of an archive created by the tar command, run the tar command with the "t" option.
If you have saved the file as a file, use the "f" option together.
If the "v" option is not used, only the names of the files included in the archive will be displayed; if it is used, detailed information such as the attributes of each file will be displayed.

※The tar command can omit the "-" (hyphen) option specification.

[root@Lion ~]# tar tf country_list.tgz ←Display the contents of an archive file (file name only)
country_list.txt
country_list_001.txt
country_list_002.txt
country_list_003.txt[root@Lion ~]# tar tvf country_list.tgz ←Archive file contents (detailed information display) display
-rw-rw-r-- root/root 2000 2020-09-22 07:59 country_list.txt
-rw-rw-r-- root/root 2000 2020-09-22 08:09 country_list_001.txt
-rw-rw-r-- root/root 2000 2020-09-25 05:50 country_list_002.txt
-rw-rw-r-- root/root 2001 2020-09-26 07:09 country_list_003.txt

●Display the contents of an archive compressed in Bzip2 format (Detailed Information Display)

[root@Lion ~]# tar tjvf country_list.tbz2
-rw-rw-r-- root/root 2000 2020-09-22 07:59 country_list.txt
-rw-rw-r-- root/root 2000 2020-09-22 08:09 country_list_001.txt
-rw-rw-r-- root/root 2000 2020-09-25 05:50 country_list_002.txt
-rw-rw-r-- root/root 2001 2020-09-26 07:09 country_list_003.tx

To view the contents of an archive compressed in Bzip2 format, run it with the option "j"

●Use the tar command to turn a directory into an archive file.

When archiving files with the tar command, it is difficult to specify all the file names to archive a large number of files. In such a case, it is convenient to group all the files you want to archive in a specific directory and archive that directory.
To archive a directory, run the tar command with the archive file name and the name of the directory to be archived as arguments.
※It is also possible to specify multiple directories to be archived, separated by spaces.

[root@Lion ~]# ls -l
drwxrwxr-x 2 root root 4096 August 28 15:18 2020 temp
[root@Lion ~]# tar czvf temp.tar.gz temp/ ←Make the temp directory an archive file
temp/
temp/country_list_002.txt
temp/country_list.tgz
temp/country_list.txt
temp/country_list.tbz2
temp/country_list_001.txt
temp/country_list_002.txt

[root@Lion ~]# ls -l ←Check the archive file.
drwxrwxr-x 2 root root 4096 August 28 15:18 2020 temp
-rw-rw-r-- 1 root root 5056 September 24 07:29 2020 temp.tar.gz ←An archive file has been created.

●Extract the archive file with the tar command

To extract the archive file, run the tar command with the "-x" option.
If the target file is a file, add the "-f" option.
To display the result of the decompression, add the "-v" option, and
If the file is compressed, add the "-z" option.

After decompression, if a file or directory with the same name exists in the archive in a directory, the timestamp will be compared and the file or directory with the newer date will remain.
By adding the option "-k", it is also possible to display a warning and abort the process if the same file exists.
In addition to these options, the argument can be the name of an archive file to be extracted, and these archive files will remain undeleted after extraction.

[root@Lion ~]# ls -l
drwxrwxr-x 2 root root 4096 August 28 15:18 2020 temp.tar.gz
[root@Lion ~]# tar zxvf temp.tar.gz  ←Extract the archive file
temp/
temp/country_list_002.txt
temp/country_list.tgz
temp/country_list.txt
temp/country_list.tbz2
temp/country_list_001.txt

If a file with the same name exists, display a warning and abort the decompression.
[root@Lion ~]# tar zxvfk temp.tar.gz    ←Add the option "-k" and run
temp/
temp/country_list_002.txt
tar: temp/country_list_002.txt: open Cannot: The file exists
temp/country_list.tgz
tar: temp/country_list.tgz: open Cannot: The file exists
temp/country_list.txt
tar: temp/country_list.txt: open Cannot: The file exists
temp/country_list.tbz2
tar: temp/country_list.tbz2: open Cannot: The file exists
temp/country_list_001.txt
tar: temp/country_list_001.txt: open Cannot: The file exists
tar: Exit with failure status due to previous error
↑Display a warning and abort the deployment because a file with the same name exists
Copied title and URL