ค้นหาเว็บไซต์

10 Wget (ตัวดาวน์โหลดไฟล์ Linux) ตัวอย่างคำสั่งใน Linux


ในบทความนี้ เราจะตรวจสอบยูทิลิตี้ wget ซึ่งดึงไฟล์จาก เวิลด์ไวด์เว็บ (WWW) โดยใช้โปรโตคอลที่ใช้กันอย่างแพร่หลาย เช่น HTTP , HTTPS, FTP และ FTPS

Wget เป็นโปรแกรมอรรถประโยชน์บรรทัดคำสั่งและโปรแกรมดาวน์โหลดไฟล์ผ่านเครือข่ายฟรี ซึ่งมาพร้อมกับคุณลักษณะมากมายที่ทำให้การดาวน์โหลดไฟล์เป็นเรื่องง่าย ได้แก่:

  • ดาวน์โหลดไฟล์ขนาดใหญ่หรือสะท้อนเว็บหรือไซต์ FTP ที่สมบูรณ์
  • ดาวน์โหลดไฟล์หลายไฟล์พร้อมกัน
  • ตั้งค่าแบนด์วิธและขีดจำกัดความเร็วสำหรับการดาวน์โหลด
  • ดาวน์โหลดไฟล์ผ่านพรอกซี
  • สามารถดำเนินการดาวน์โหลดที่ถูกยกเลิกต่อได้
  • มิเรอร์ไดเร็กทอรีแบบวนซ้ำ
  • ทำงานบนระบบปฏิบัติการที่คล้ายกับ UNIX ส่วนใหญ่เช่นเดียวกับ Windows
  • การทำงานแบบอัตโนมัติ/พื้นหลัง
  • รองรับการเชื่อมต่อ HTTP แบบถาวร
  • รองรับ SSL/TLS สำหรับการดาวน์โหลดที่เข้ารหัสโดยใช้ไลบรารี OpenSSL หรือ GnuTLS
  • รองรับการดาวน์โหลด IPv4 และ IPv6

ไวยากรณ์คำสั่ง Wget

ไวยากรณ์พื้นฐานของ Wget คือ:

wget [option] [URL]

ขั้นแรก ตรวจสอบว่าได้ติดตั้งยูทิลิตี wget ไว้ในกล่อง Linux ของคุณหรือไม่ โดยใช้คำสั่งต่อไปนี้

rpm -q wget         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
dpkg -l | grep wget [On Debian, Ubuntu and Mint]

ติดตั้ง Wget บน Linux

หากไม่ได้ติดตั้ง Wget คุณสามารถติดตั้งได้โดยใช้ตัวจัดการแพ็คเกจเริ่มต้นของระบบ Linux ดังที่แสดง

sudo apt install wget -y      [On Debian, Ubuntu and Mint]
sudo yum install wget -y      [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
sudo emerge -a net-misc/wget  [On Gentoo Linux]
sudo pacman -Sy wget           [On Arch Linux]
sudo zypper install wget      [On OpenSUSE]    

ตัวเลือก -y ที่ใช้ในที่นี้คือเพื่อป้องกันการแจ้งเตือนก่อนที่จะติดตั้งแพ็คเกจใดๆ สำหรับตัวอย่างและตัวเลือกคำสั่ง YUM และ APT เพิ่มเติม โปรดอ่านบทความของเราใน:

  • 20 คำสั่ง Linux YUM สำหรับการจัดการแพ็คเกจ
  • 15 ตัวอย่างคำสั่ง APT ใน Ubuntu/Debian & Mint
  • 45 ตัวอย่างคำสั่ง Zypper เพื่อจัดการ OpenSUSE Linux

1. ดาวน์โหลดไฟล์ด้วย Wget

คำสั่งจะดาวน์โหลดไฟล์เดียวและจัดเก็บไว้ในไดเร็กทอรีปัจจุบัน นอกจากนี้ยังแสดง ความคืบหน้าการดาวน์โหลด, ขนาด, วันที่ และ เวลา ในขณะที่ดาวน์โหลด

wget http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz

--2021-12-10 04:15:16--  http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz
Resolving ftp.gnu.org (ftp.gnu.org)... 209.51.188.20, 2001:470:142:3::b
Connecting to ftp.gnu.org (ftp.gnu.org)|209.51.188.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3565643 (3.4M) [application/x-gzip]
Saving to: ‘wget2-2.0.0.tar.gz’

wget2-2.0.0.tar.gz      100%[==========>]   3.40M  2.31MB/s    in 1.5s    

2021-12-10 04:15:18 (2.31 MB/s) - ‘wget2-2.0.0.tar.gz’ saved [3565643/3565643]

2. Wget ดาวน์โหลดไฟล์ด้วยชื่ออื่น

การใช้ตัวเลือก -O (ตัวพิมพ์ใหญ่) ดาวน์โหลดไฟล์ที่มีชื่อไฟล์ต่างกัน ที่นี่เราได้ตั้งชื่อไฟล์ wget.zip ดังที่แสดงด้านล่าง

wget -O wget.zip http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz

--2021-12-10 04:20:19--  http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz
Resolving ftp.gnu.org (ftp.gnu.org)... 209.51.188.20, 2001:470:142:3::b
Connecting to ftp.gnu.org (ftp.gnu.org)|209.51.188.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 446966 (436K) [application/x-gzip]
Saving to: ‘wget.zip’

wget.zip      100%[===================>] 436.49K   510KB/s    in 0.9s    

2021-12-10 04:20:21 (510 KB/s) - ‘wget.zip’ saved [446966/446966]

3. Wget ดาวน์โหลดไฟล์หลายไฟล์ด้วยโปรโตคอล HTTP และ FTP

ที่นี่เราจะดูวิธีการดาวน์โหลดไฟล์หลายไฟล์โดยใช้โปรโตคอล HTTP และ FTP พร้อมด้วยคำสั่ง wget พร้อมกัน

wget http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz ftp://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz.sig

--2021-12-10 06:45:17--  http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz
Resolving ftp.gnu.org (ftp.gnu.org)... 209.51.188.20, 2001:470:142:3::b
Connecting to ftp.gnu.org (ftp.gnu.org)|209.51.188.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3565643 (3.4M) [application/x-gzip]
Saving to: ‘wget2-2.0.0.tar.gz’

wget2-2.0.0.tar.gz      100%[==========>]   4.40M  4.31MB/s    in 1.1s    

2021-12-10 06:46:10 (2.31 MB/s) - ‘wget2-2.0.0.tar.gz’ saved [3565643/3565643]

4. Wget ดาวน์โหลดไฟล์หลายไฟล์จากไฟล์เดียว

หากต้องการดาวน์โหลดไฟล์หลายไฟล์พร้อมกัน ให้ใช้ตัวเลือก -i พร้อมตำแหน่งของไฟล์ที่มีรายการ URL ที่จะดาวน์โหลด ต้องเพิ่ม URL แต่ละรายการในบรรทัดแยกกันดังที่แสดง

ตัวอย่างเช่น ไฟล์ 'download-linux.txt' ต่อไปนี้มีรายการ URL ที่จะดาวน์โหลด

cat download-linux.txt 

https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
https://download.rockylinux.org/pub/rocky/8/isos/x86_64/Rocky-8.5-x86_64-dvd1.iso
https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-11.2.0-amd64-DVD-1.iso
wget -i download-linux.txt

--2021-12-10 04:52:40--  https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
Resolving releases.ubuntu.com (releases.ubuntu.com)... 91.189.88.248, 91.189.88.247, 91.189.91.124, ...
Connecting to releases.ubuntu.com (releases.ubuntu.com)|91.189.88.248|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3071934464 (2.9G) [application/x-iso9660-image]
Saving to: ‘ubuntu-20.04.3-desktop-amd64.iso’

ubuntu-20.04.3-desktop-amd64   4%[=>      ] 137.71M  11.2MB/s    eta 3m 30s
...

หากรายการ URL ของคุณมีรูปแบบการกำหนดหมายเลขเฉพาะ คุณสามารถเพิ่มวงเล็บปีกกาเพื่อดึง URL ทั้งหมดที่ตรงกับรูปแบบนั้น ตัวอย่างเช่น หากคุณต้องการดาวน์โหลดชุดเคอร์เนล Linux โดยเริ่มจากเวอร์ชัน 5.1.1 ถึง 5.1.15 คุณสามารถดำเนินการดังต่อไปนี้

wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.{1..15}.tar.gz

--2021-12-10 05:46:59--  https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.1.tar.gz
Resolving mirrors.edge.kernel.org (mirrors.edge.kernel.org)... 147.75.95.133, 2604:1380:3000:1500::1
Connecting to mirrors.edge.kernel.org (mirrors.edge.kernel.org)|147.75.95.133|:443... connected.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not trusted.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not yet activated.
The certificate has not yet been activated
HTTP request sent, awaiting response... 200 OK
Length: 164113671 (157M) [application/x-gzip]
Saving to: ‘linux-5.1.1.tar.gz’

linux-5.1.1.tar.gz      100%[===========>] 156.51M  2.59MB/s    in 61s     

2021-12-10 05:48:01 (2.57 MB/s) - ‘linux-5.1.1.tar.gz’ saved [164113671/164113671]

--2021-12-10 05:48:01--  https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.2.tar.gz
Reusing existing connection to mirrors.edge.kernel.org:443.
HTTP request sent, awaiting response... 200 OK
Length: 164110470 (157M) [application/x-gzip]
Saving to: ‘linux-5.1.2.tar.gz’

linux-5.1.2.tar.gz     19%[===========]  30.57M  2.58MB/s    eta 50s

5. Wget Resume ดาวน์โหลดไม่เสร็จ

ในกรณีที่ดาวน์โหลดไฟล์ขนาดใหญ่ บางครั้งอาจต้องหยุดดาวน์โหลด ในกรณีนี้เราสามารถดาวน์โหลดไฟล์เดิมที่ค้างไว้โดยมีตัวเลือก -c ได้

แต่เมื่อคุณเริ่มดาวน์โหลดไฟล์โดยไม่ระบุ -c ตัวเลือก wget จะเพิ่มนามสกุล .1 ที่ท้ายไฟล์ ซึ่งถือเป็นไฟล์ใหม่ ดาวน์โหลด ดังนั้นจึงควรเพิ่มสวิตช์ -c เมื่อคุณดาวน์โหลดไฟล์ขนาดใหญ่

# wget -c https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

--2021-12-10 05:27:59--  https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
Resolving releases.ubuntu.com (releases.ubuntu.com)... 91.189.88.247, 91.189.91.123, 91.189.91.124, ...
Connecting to releases.ubuntu.com (releases.ubuntu.com)|91.189.88.247|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 3071934464 (2.9G), 2922987520 (2.7G) remaining [application/x-iso9660-image]
Saving to: ‘ubuntu-20.04.3-desktop-amd64.iso’

ubuntu-20.04.3-desktop-amd64.iso        5%[++++++> ]   167.93M  11.1MB/s               
^C
[root@tecmint ~]# wget -c https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
--2021-12-10 05:28:03--  https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
Resolving releases.ubuntu.com (releases.ubuntu.com)... 91.189.88.248, 91.189.91.124, 91.189.91.123, ...
Connecting to releases.ubuntu.com (releases.ubuntu.com)|91.189.88.248|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 3071934464 (2.9G), 2894266368 (2.7G) remaining [application/x-iso9660-image]
Saving to: ‘ubuntu-20.04.3-desktop-amd64.iso’

ubuntu-20.04.3-desktop-amd64.iso        10%[+++++++=====> ] 296.32M  17.2MB/s    eta 2m 49s ^

6. Wget Mirror เว็บไซต์ทั้งหมด

หากต้องการดาวน์โหลดหรือมิเรอร์หรือคัดลอกเว็บไซต์ทั้งหมดเพื่อดูแบบออฟไลน์ คุณสามารถใช้คำสั่งต่อไปนี้ซึ่งจะสร้างสำเนาของเว็บไซต์ในเครื่องพร้อมกับเนื้อหาทั้งหมด (JavaScript, CSS, รูปภาพ)

wget --recursive --page-requisites --adjust-extension --span-hosts --convert-links --restrict-file-names=windows --domains yoursite.com --no-parent yoursite.com

คำอธิบายของคำสั่งข้างต้น

wget \
     --recursive \ # Download the whole site.
     --page-requisites \ # Get all assets/elements (CSS/JS/images).
     --adjust-extension \ # Save files with .html on the end.
     --span-hosts \ # Include necessary assets from offsite as well.
     --convert-links \ # Update links to still work in the static version.
     --restrict-file-names=windows \ # Modify filenames to work in Windows as well.
     --domains yoursite.com \ # Do not follow links outside this domain.
     --no-parent \ # Don't follow links outside the directory you pass in.
         yoursite.com/whatever/path # The URL to download

7. Wget ดาวน์โหลดไฟล์ในพื้นหลัง

ด้วยตัวเลือก -b คุณสามารถส่งการดาวน์โหลดในเบื้องหลังทันทีหลังจากที่การดาวน์โหลดเริ่มต้นขึ้น และบันทึกถูกเขียนลงในไฟล์ wget.log

wget -b wget.log https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

Continuing in background, pid 8999.
Output will be written to ‘wget.log’.

8. Wget ตั้งค่าขีดจำกัดความเร็วในการดาวน์โหลดไฟล์

ด้วยตัวเลือก --limit-rate=100k การจำกัดความเร็วในการดาวน์โหลดจะถูกจำกัดไว้ที่ 100k และบันทึกจะถูกสร้างขึ้นภายใต้ wget.log ดังที่แสดงด้านล่าง

wget -c --limit-rate=100k -b wget.log https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

Continuing in background, pid 9108.
Output will be written to ‘wget-log’.

ดูไฟล์ wget.log และตรวจสอบความเร็วในการดาวน์โหลดของ wget

tail -f wget-log 

 5600K .......... .......... .......... .......... ..........  0%  104K 8h19m
 5650K .......... .......... .......... .......... ..........  0%  103K 8h19m
 5700K .......... .......... .......... .......... ..........  0%  105K 8h19m
 5750K .......... .......... .......... .......... ..........  0%  104K 8h18m
 5800K .......... .......... .......... .......... ..........  0%  104K 8h18m
 5850K .......... .......... .......... .......... ..........  0%  105K 8h18m
 5900K .......... .......... .......... .......... ..........  0%  103K 8h18m
 5950K .......... .......... .......... .......... ..........  0%  105K 8h18m
 6000K .......... .......... .......... .......... ..........  0% 69.0K 8h20m
 6050K .......... .......... .......... .......... ..........  0%  106K 8h19m
 6100K .......... .......... .......... .......... ..........  0% 98.5K 8h20m
 6150K .......... .......... .......... .......... ..........  0%  110K 8h19m
 6200K .......... .......... .......... .......... ..........  0%  104K 8h19m
 6250K .......... .......... .......... .......... ..........  0%  104K 8h19m
...

9. Wget ดาวน์โหลดไฟล์ที่ป้องกันด้วยรหัสผ่านผ่าน FTP และ HTTP

หากต้องการดาวน์โหลดไฟล์จากเซิร์ฟเวอร์ FTP ที่มีการป้องกันด้วยรหัสผ่าน คุณสามารถใช้ตัวเลือก --ftp-user=username และ --ftp-password=password ดังที่แสดง

wget --ftp-user=narad --ftp-password=password ftp://ftp.example.com/filename.tar.gz

หากต้องการดาวน์โหลดไฟล์จากเซิร์ฟเวอร์ HTTP ที่มีการป้องกันด้วยรหัสผ่าน คุณสามารถใช้ตัวเลือก --http-user=username และ --http-password=password ดังที่แสดง

wget --http-user=narad --http-password=password http://http.example.com/filename.tar.gz

10. Wget ละเว้นการตรวจสอบใบรับรอง SSL

หากต้องการละเว้นการตรวจสอบใบรับรอง SSL ขณะดาวน์โหลดไฟล์ผ่าน HTTPS คุณสามารถใช้ตัวเลือก --no-check-certificate:

wget --no-check-certificate https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.1.tar.gz

--2021-12-10 06:21:21--  https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.1.tar.gz
Resolving mirrors.edge.kernel.org (mirrors.edge.kernel.org)... 147.75.95.133, 2604:1380:3000:1500::1
Connecting to mirrors.edge.kernel.org (mirrors.edge.kernel.org)|147.75.95.133|:443... connected.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not trusted.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not yet activated.
The certificate has not yet been activated
HTTP request sent, awaiting response... 200 OK
Length: 164113671 (157M) [application/x-gzip]
Saving to: ‘linux-5.1.1.tar.gz’
...

11. เวอร์ชัน Wget และวิธีใช้

ด้วยตัวเลือก --version และ --help คุณสามารถดู เวอร์ชัน และ ความช่วยเหลือ ได้ตามต้องการ

wget --version
wget --help

ในบทความนี้ เราได้กล่าวถึง คำสั่ง wget ของ Linux พร้อมด้วยตัวเลือกสำหรับงานการดูแลระบบรายวัน ผู้ชายจะรับไหม หากคุณต้องการทราบข้อมูลเพิ่มเติมเกี่ยวกับเรื่องนี้ กรุณาแบ่งปันผ่านช่องแสดงความคิดเห็นของเราหรือหากเราพลาดสิ่งใดโปรดแจ้งให้เราทราบ