18 exemples útils d'ordres Tar per a tots els administradors de sistemes Linux
El tar de Linux significa arxiu de cintes, que és utilitzat per un gran nombre d'administradors de sistemes Linux/Unix per fer front a la còpia de seguretat de les unitats de cinta.
L'ordre tar s'utilitza per extreure una col·lecció de fitxers i directoris en un fitxer d'arxiu altament comprimit anomenat habitualment tarball o tar, gzip i bzip a Linux.
El tar és l'ordre més utilitzada per crear fitxers d'arxiu comprimits i que es pot moure fàcilment d'un disc a un altre disc o d'una màquina a una màquina.
En aquest article, revisarem i discutirem diversos exemples d'ordres tar, com ara com crear fitxers d'arxiu mitjançant la compressió (tar, tar.gz i tar.bz2), com extreure un fitxer d'arxiu, extreure un únic fitxer, veure contingut del fitxer, verificar un fitxer, afegir fitxers o directoris al fitxer d'arxiu existent, estimar la mida del fitxer d'arxiu tar, etc.
[També us pot agradar: 7-Zip: comprimiu i descomprimiu fitxers amb una alta relació de compressió]
L'objectiu principal d'aquesta guia és proporcionar diversos exemples d'ordres tar que us poden ser útils per entendre i convertir-vos en un expert en la manipulació d'arxius tar.
L'ordre d'exemple següent crearà un fitxer d'arxiu tar tecmint-14-09-12.tar per a un directori /home/tecmint al directori de treball actual. Vegeu l'exemple d'ordre en acció.
# tar -cvf tecmint-14-09-12.tar /home/tecmint/ /home/tecmint/ /home/tecmint/cleanfiles.sh /home/tecmint/openvpn-2.1.4.tar.gz /home/tecmint/tecmint-14-09-12.tar /home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm /home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
Parlem de cada opció utilitzada a l'ordre anterior per crear un fitxer d'arxiu tar.
- c: crea un fitxer d'arxiu .tar nou.
- v – Mostra de manera textual el progrés del fitxer .tar.
- f: tipus de nom de fitxer del fitxer d'arxiu.
Per crear un fitxer d'arxiu gzip comprimit fem servir l'opció com a z. Per exemple, l'ordre següent crearà un fitxer comprimit MyImages-14-09-12.tar.gz per al directori /home/MyImages. (Nota: tar.gz i tgz són semblants).
# tar cvzf MyImages-14-09-12.tar.gz /home/MyImages OR # tar cvzf MyImages-14-09-12.tgz /home/MyImages /home/MyImages/ /home/MyImages/Sara-Khan-and-model-Priyanka-Shah.jpg /home/MyImages/RobertKristenviolent101201.jpg /home/MyImages/Justintimerlake101125.jpg /home/MyImages/Mileyphoto101203.jpg /home/MyImages/JenniferRobert101130.jpg /home/MyImages/katrinabarbiedoll231110.jpg /home/MyImages/the-japanese-wife-press-conference.jpg /home/MyImages/ReesewitherspoonCIA101202.jpg /home/MyImages/yanaguptabaresf231110.jpg
La funció bz2 comprimeix i crea un fitxer d'arxiu inferior a la mida del gzip. La compressió bz2 triga més temps a comprimir i descomprimir fitxers que gzip, que triga menys temps.
Per crear un fitxer tar molt comprimit fem servir l'opció j. L'ordre d'exemple següent crearà un fitxer Phpfiles-org.tar.bz2 per a un directori /home/php. (Nota: tar.bz2 i tbz són similars a tb2).
# tar cvfj Phpfiles-org.tar.bz2 /home/php OR # tar cvfj Phpfiles-org.tar.tbz /home/php OR # tar cvfj Phpfiles-org.tar.tb2 /home/php /home/php/ /home/php/iframe_ew.php /home/php/videos_all.php /home/php/rss.php /home/php/index.php /home/php/vendor.php /home/php/video_title.php /home/php/report.php /home/php/object.html /home/php/video.php
Per destar o extreure un fitxer tar, només cal que emeti l'ordre següent mitjançant l'opció x (extreure). Per exemple, l'ordre següent desterrarà el fitxer public_html-14-09-12.tar al directori de treball actual.
Si voleu destar en un directori diferent, feu servir l'opció com a -C (directori especificat).
## Untar files in Current Directory ## # tar -xvf public_html-14-09-12.tar ## Untar files in specified Directory ## # tar -xvf public_html-14-09-12.tar -C /home/public_html/videos/ /home/public_html/videos/ /home/public_html/videos/views.php /home/public_html/videos/index.php /home/public_html/videos/logout.php /home/public_html/videos/all_categories.php /home/public_html/videos/feeds.xml
Per descomprimir el fitxer d'arxiu tar.gz, només cal que executeu l'ordre següent. Si ens agradaria destar en diferents directoris, només cal que utilitzeu l'opció -C i la ruta del directori, tal com es mostra a l'exemple anterior.
# tar -xvf thumbnails-14-09-12.tar.gz /home/public_html/videos/thumbnails/ /home/public_html/videos/thumbnails/katdeepika231110.jpg /home/public_html/videos/thumbnails/katrinabarbiedoll231110.jpg /home/public_html/videos/thumbnails/onceuponatime101125.jpg /home/public_html/videos/thumbnails/playbutton.png /home/public_html/videos/thumbnails/ReesewitherspoonCIA101202.jpg /home/public_html/videos/thumbnails/snagItNarration.jpg /home/public_html/videos/thumbnails/Minissha-Lamba.jpg /home/public_html/videos/thumbnails/Lindsaydance101201.jpg /home/public_html/videos/thumbnails/Mileyphoto101203.jpg
Per descomprimir el fitxer tar.bz2 altament comprimit, només cal que utilitzeu l'ordre següent. L'ordre d'exemple següent destacarà tots els fitxers .flv del fitxer d'arxiu.
# tar -xvf videos-14-09-12.tar.bz2 /home/public_html/videos/flv/katrinabarbiedoll231110.flv /home/public_html/videos/flv/BrookmuellerCIA101125.flv /home/public_html/videos/flv/dollybackinbb4101125.flv /home/public_html/videos/flv/JenniferRobert101130.flv /home/public_html/videos/flv/JustinAwardmovie101125.flv /home/public_html/videos/flv/Lakme-Fashion-Week.flv /home/public_html/videos/flv/Mileyphoto101203.flv /home/public_html/videos/flv/Minissha-Lamba.flv
Per llistar el contingut del fitxer d'arxiu tar, només cal que executeu l'ordre següent amb l'opció t (contingut de la llista). L'ordre següent mostrarà el contingut del fitxer uploadprogress.tar.
# tar -tvf uploadprogress.tar -rw-r--r-- chregu/staff 2276 2011-08-15 18:51:10 package2.xml -rw-r--r-- chregu/staff 7877 2011-08-15 18:51:10 uploadprogress/examples/index.php -rw-r--r-- chregu/staff 1685 2011-08-15 18:51:10 uploadprogress/examples/server.php -rw-r--r-- chregu/staff 1697 2011-08-15 18:51:10 uploadprogress/examples/info.php -rw-r--r-- chregu/staff 367 2011-08-15 18:51:10 uploadprogress/config.m4 -rw-r--r-- chregu/staff 303 2011-08-15 18:51:10 uploadprogress/config.w32 -rw-r--r-- chregu/staff 3563 2011-08-15 18:51:10 uploadprogress/php_uploadprogress.h -rw-r--r-- chregu/staff 15433 2011-08-15 18:51:10 uploadprogress/uploadprogress.c -rw-r--r-- chregu/staff 1433 2011-08-15 18:51:10 package.xml
Utilitzeu l'ordre següent per llistar el contingut del fitxer tar.gz.
# tar -tvf staging.linux-console.net.tar.gz -rw-r--r-- root/root 0 2012-08-30 04:03:57 staging.linux-console.net-access_log -rw-r--r-- root/root 587 2012-08-29 18:35:12 staging.linux-console.net-access_log.1 -rw-r--r-- root/root 156 2012-01-21 07:17:56 staging.linux-console.net-access_log.2 -rw-r--r-- root/root 156 2011-12-21 11:30:56 staging.linux-console.net-access_log.3 -rw-r--r-- root/root 156 2011-11-20 17:28:24 staging.linux-console.net-access_log.4 -rw-r--r-- root/root 0 2012-08-30 04:03:57 staging.linux-console.net-error_log -rw-r--r-- root/root 3981 2012-08-29 18:35:12 staging.linux-console.net-error_log.1 -rw-r--r-- root/root 211 2012-01-21 07:17:56 staging.linux-console.net-error_log.2 -rw-r--r-- root/root 211 2011-12-21 11:30:56 staging.linux-console.net-error_log.3 -rw-r--r-- root/root 211 2011-11-20 17:28:24 staging.linux-console.net-error_log.4
Per llistar el contingut del fitxer tar.bz2, emet l'ordre següent.
# tar -tvf Phpfiles-org.tar.bz2 drwxr-xr-x root/root 0 2012-09-15 03:06:08 /home/php/ -rw-r--r-- root/root 1751 2012-09-15 03:06:08 /home/php/iframe_ew.php -rw-r--r-- root/root 11220 2012-09-15 03:06:08 /home/php/videos_all.php -rw-r--r-- root/root 2152 2012-09-15 03:06:08 /home/php/rss.php -rw-r--r-- root/root 3021 2012-09-15 03:06:08 /home/php/index.php -rw-r--r-- root/root 2554 2012-09-15 03:06:08 /home/php/vendor.php -rw-r--r-- root/root 406 2012-09-15 03:06:08 /home/php/video_title.php -rw-r--r-- root/root 4116 2012-09-15 03:06:08 /home/php/report.php -rw-r--r-- root/root 1273 2012-09-15 03:06:08 /home/php/object.html
Per extreure un únic fitxer anomenat cleanfiles.sh de cleanfiles.sh.tar, utilitzeu l'ordre següent.
# tar -xvf cleanfiles.sh.tar cleanfiles.sh OR # tar --extract --file=cleanfiles.sh.tar cleanfiles.sh cleanfiles.sh
Per extreure un sol fitxer tecmintbackup.xml del fitxer d'arxiu tecmintbackup.tar.gz, utilitzeu l'ordre de la següent manera.
# tar -zxvf tecmintbackup.tar.gz tecmintbackup.xml OR # tar --extract --file=tecmintbackup.tar.gz tecmintbackup.xml tecmintbackup.xml
Per extreure un únic fitxer anomenat index.php del fitxer Phpfiles-org.tar.bz2 utilitzeu la següent opció.
# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php OR # tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php /home/php/index.php
Per extreure o destar diversos fitxers del fitxer d'arxiu tar, tar.gz i tar.bz2. Per exemple, l'ordre següent extreu fitxer 1 fitxer 2 dels fitxers d'arxiu.
# tar -xvf tecmint-14-09-12.tar "file1" "file2" # tar -zxvf MyImages-14-09-12.tar.gz "file1" "file2" # tar -jxvf Phpfiles-org.tar.bz2 "file1" "file2"
Per extreure un grup de fitxers fem servir l'extracció basada en comodins . Per exemple, per extreure un grup de tots els fitxers el patró dels quals comença amb .php d'un fitxer d'arxiu tar, tar.gz i tar.bz2.
# tar -xvf Phpfiles-org.tar --wildcards '*.php' # tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php' # tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php' /home/php/iframe_ew.php /home/php/videos_all.php /home/php/rss.php /home/php/index.php /home/php/vendor.php /home/php/video_title.php /home/php/report.php /home/php/video.php
Per afegir fitxers o directoris als fitxers d'arxiu tar existents fem servir l'opció r (afegir). Per exemple, afegim el fitxer xyz.txt i el directori php al fitxer d'arxiu existent tecmint-14-09-12.tar.
# tar -rvf tecmint-14-09-12.tar xyz.txt # tar -rvf tecmint-14-09-12.tar php drwxr-xr-x root/root 0 2012-09-15 02:24:21 home/tecmint/ -rw-r--r-- root/root 15740615 2012-09-15 02:23:42 home/tecmint/cleanfiles.sh -rw-r--r-- root/root 863726 2012-09-15 02:23:41 home/tecmint/openvpn-2.1.4.tar.gz -rw-r--r-- root/root 21063680 2012-09-15 02:24:21 home/tecmint/tecmint-14-09-12.tar -rw-r--r-- root/root 4437600 2012-09-15 02:23:41 home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm -rw-r--r-- root/root 12680 2012-09-15 02:23:41 home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm -rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/ -rw-r--r-- root/root 1751 2012-09-15 03:06:08 php/iframe_ew.php -rw-r--r-- root/root 11220 2012-09-15 03:06:08 php/videos_all.php -rw-r--r-- root/root 2152 2012-09-15 03:06:08 php/rss.php -rw-r--r-- root/root 3021 2012-09-15 03:06:08 php/index.php -rw-r--r-- root/root 2554 2012-09-15 03:06:08 php/vendor.php -rw-r--r-- root/root 406 2012-09-15 03:06:08 php/video_title.php
L'ordre tar no té cap opció per afegir fitxers o directoris a un fitxer d'arxiu comprimit tar.gz i tar.bz2 existent. Si ho intentem, obtindrem el següent error.
# tar -rvf MyImages-14-09-12.tar.gz xyz.txt # tar -rvf Phpfiles-org.tar.bz2 xyz.txt tar: This does not look like a tar archive tar: Skipping to next header xyz.txt tar: Error exit delayed from previous errors
Per verificar qualsevol fitxer arxivat tar o comprimit utilitzem l'opció W (verificar). Per fer-ho, només cal que utilitzeu els següents exemples d'ordres. (Nota: no podeu fer la verificació en un fitxer d'arxiu comprimit ( *.tar.gz, *.tar.bz2 ).
# tar tvfW tecmint-14-09-12.tar tar: This does not look like a tar archive tar: Skipping to next header tar: Archive contains obsolescent base-64 headers tar: VERIFY FAILURE: 30740 invalid headers detected Verify -rw-r--r-- root/root 863726 2012-09-15 02:23:41 /home/tecmint/openvpn-2.1.4.tar.gz Verify -rw-r--r-- root/root 21063680 2012-09-15 02:24:21 /home/tecmint/tecmint-14-09-12.tar tar: /home/tecmint/tecmint-14-09-12.tar: Warning: Cannot stat: No such file or directory Verify -rw-r--r-- root/root 4437600 2012-09-15 02:23:41 home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm tar: /home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm: Warning: Cannot stat: No such file or directory Verify -rw-r--r-- root/root 12680 2012-09-15 02:23:41 home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm tar: /home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm: Warning: Cannot stat: No such file or directory Verify -rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt Verify drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/
Per comprovar la mida de qualsevol fitxer d'arxiu tar, tar.gz i tar.bz2, utilitzeu l'ordre següent. Per exemple, l'ordre següent mostrarà la mida del fitxer d'arxiu en kilobytes (KB).
# tar -czf - tecmint-14-09-12.tar | wc -c 12820480 # tar -czf - MyImages-14-09-12.tar.gz | wc -c 112640 # tar -czf - Phpfiles-org.tar.bz2 | wc -c 20480
- c: crea un fitxer d'arxiu.
- x: extreu un fitxer d'arxiu.
- v: mostra el progrés del fitxer d'arxiu.
- f: nom del fitxer d'arxiu.
- t: visualització del contingut del fitxer d'arxiu.
- j: filtra l'arxiu mitjançant bzip2.
- z: filtra l'arxiu mitjançant gzip.
- r: afegiu o actualitzeu fitxers o directoris als fitxers d'arxiu existents.
- W: verifica un fitxer d'arxiu.
- comodins: especifiqueu patrons a l'ordre tar d'UNIX.
Això és tot de moment, esperem que els exemples d'ordres tar anteriors siguin suficients per aprendre i, per obtenir més informació, utilitzeu l'ordre man tar.
Si voleu dividir qualsevol fitxer d'arxiu tar gran en diverses parts o blocs, només heu de revisar aquest article:
- Com dividir el fitxer Tar en diversos fitxers de certa mida
- Com baixar i extreure fitxers tar amb una ordre
Si ens hem perdut algun exemple, si us plau, compartiu-lo amb nosaltres a través del quadre de comentaris i no us oblideu de compartir aquest article amb els vostres amics. Aquesta és la millor manera de donar les gràcies....