Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

fdupes: เครื่องมือบรรทัดคำสั่งที่มีประสิทธิภาพสำหรับค้นหาและลบไฟล์ซ้ำใน Linux

เป็นความต้องการทั่วไปในการค้นหาและแทนที่ไฟล์ซ้ำสำหรับผู้ใช้คอมพิวเตอร์ส่วนใหญ่ การค้นหาและลบไฟล์ซ้ำเป็นงานที่เหนื่อยล้า ต้องใช้เวลาและความอดทน การค้นหาไฟล์ซ้ำสามารถทำได้ง่ายมาก หากเครื่องของคุณใช้ GNU/Linux ขอบคุณโปรแกรมยูทิลิตี้ 'fdupes'

fdupes: เครื่องมือบรรทัดคำสั่งที่มีประสิทธิภาพสำหรับค้นหาและลบไฟล์ซ้ำใน Linux

fdupes คืออะไร?

Fdupes เป็นยูทิลิตี้ Linux ที่เขียนโดย Adrian Lopez ด้วยภาษาโปรแกรม C เผยแพร่ภายใต้ MIT License แอปพลิเคชันนี้สามารถค้นหาไฟล์ซ้ำในไดเรกทอรีและซับไดเรกทอรีที่กำหนดได้ Fdupes ระบุไฟล์ซ้ำโดยการเปรียบเทียบลายเซ็น MD5 ของไฟล์ ตามด้วยการเปรียบเทียบแบบ Byte-to-byte มีตัวเลือกมากมายที่สามารถส่งผ่านให้ Fdupes เพื่อรายการ ลบ และแทนที่ไฟล์ด้วย Hardlinks ไปยังไฟล์ซ้ำ

การเปรียบเทียบเริ่มต้นตามลำดับ:

เปรียบเทียบขนาด > เปรียบเทียบลายเซ็น MD5 บางส่วน > เปรียบเทียบลายเซ็น MD5 เต็ม > เปรียบเทียบ Byte-to-byte

ติดตั้ง fdupes บน Linux

การติดตั้ง fdupes เวอร์ชันล่าสุด (fdupes version 1.51) ง่ายเท่ากับการรันคำสั่งต่อไปนี้บนระบบที่ใช้ Debian เช่น Ubuntu และ Linux Mint

$ sudo apt-get install fdupes

บนระบบ CentOS/RHEL และ Fedora คุณต้องเปิดใช้งาน EPEL Repository เพื่อติดตั้งแพ็กเกจ fdupes

# yum install fdupes
# dnf install fdupes [On Fedora 22 onwards]

หมายเหตุ: Package Manager เริ่มต้น yum ถูกแทนที่ด้วย dnf ตั้งแต่ Fedora 22 เป็นต้นมา…

วิธีใช้คำสั่ง fdupes

1. เพื่อการสาธิต ให้สร้างไฟล์ซ้ำ 몇 ไฟล์ภายใต้ไดเรกทอรี (เช่น tecmint) ดังนี้:

$ mkdir /home/"$USER"/Desktop/tecmint && cd /home/"$USER"/Desktop/tecmint && for i in {1..15}; do echo "I Love Tecmint. Tecmint is a very nice community of Linux Users." > tecmint${i}.txt ; done

หลังจากรันคำสั่งข้างต้น ให้ตรวจสอบว่าไฟล์ซ้ำถูกสร้างขึ้นหรือไม่โดยใช้คำสั่ง ls

$ ls -l
total 60
-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint10.txt
-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint11.txt
... (ไฟล์อื่นๆ ถูกตัดทอนเพื่อความกระชับ)

สคริปต์ข้างต้นสร้าง 15 ไฟล์ คือ tecmint1.txt, tecmint2.txt…tecmint15.txt และทุกไฟล์มีข้อมูลเหมือนกันคือ

"I Love Tecmint. Tecmint is a very nice community of Linux Users."

2. ตอนนี้ค้นหาไฟล์ซ้ำภายในโฟลเดอร์ tecmint

$ fdupes /home/$USER/Desktop/tecmint
/home/tecmint/Desktop/tecmint/tecmint13.txt
/home/tecmint/Desktop/tecmint/tecmint8.txt
... (รายการไฟล์ซ้ำทั้งหมด)

3. ค้นหาไฟล์ซ้ำแบบ Recursive ภายใต้ทุกไดเรกทอรีรวมถึงซับไดเรกทอรี โดยใช้ตัวเลือก -r

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

$ fdupes -r /home
Progress [37780/54747] 69%

4. ดูขนาดของไฟล์ซ้ำที่พบภายในโฟลเดอร์โดยใช้ตัวเลือก -S

$ fdupes -S /home/$USER/Desktop/tecmint
65 bytes each:
/home/tecmint/Desktop/tecmint/tecmint13.txt
... (รายการไฟล์ซ้ำทั้งหมด)

5. คุณสามารถดูขนาดของไฟล์ซ้ำสำหรับทุกไดเรกทอรีและซับไดเรกทอรีที่พบได้โดยใช้ตัวเลือก -S และ -r พร้อมกัน:

$ fdupes -Sr /home/avi/Desktop/
65 bytes each: ...
107 bytes each:
/home/tecmint/Desktop/resume_files/r-csc.html
/home/tecmint/Desktop/resume_files/fc.html

6. นอกจากการค้นหาในโฟลเดอร์เดียวหรือทุกโฟลเดอร์แบบ Recursive แล้ว คุณยังสามารถเลือกค้นหาในสองหรือสามโฟลเดอร์ตามต้องการ ไม่ต้องพูดถึงการใช้ตัวเลือก -S และ/หรือ -r หากต้องการ

$ fdupes /home/avi/Desktop/ /home/avi/Templates/

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

$ fdupes -d /home/$USER/Desktop/tecmint
[1] /home/tecmint/Desktop/tecmint/tecmint13.txt
[2] /home/tecmint/Desktop/tecmint/tecmint8.txt
... (รายการไฟล์ซ้ำทั้ง 15 ไฟล์)
Set 1 of 1, preserve files [1 - 15, all]:

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

Set 1 of 1, preserve files [1 - 15, all]: 2-15
[-] /home/tecmint/Desktop/tecmint/tecmint13.txt
[+] /home/tecmint/Desktop/tecmint/tecmint8.txt
... (แสดงไฟล์ที่จะลบและเก็บไว้)

8. จากมุมมองความปลอดภัย คุณอาจต้องการพิมพ์ Output ของ 'fdupes' ลงไฟล์ แล้วตรวจสอบไฟล์ข้อความเพื่อตัดสินใจว่าจะลบไฟล์ไหน ซึ่งจะลดโอกาสที่ไฟล์จะถูกลบโดยไม่ได้ตั้งใจ คุณสามารถทำได้ดังนี้:

$ fdupes -Sr /home > /home/fdupes.txt

หมายเหตุ: คุณสามารถแทนที่ '/home' ด้วยโฟลเดอร์ที่ต้องการ ใช้ตัวเลือก '-r' และ '-S' หากต้องการค้นหาแบบ Recursive และแสดงขนาดตามลำดับ

9. คุณสามารถข้ามไฟล์แรกในแต่ละชุดของผลลัพธ์ที่ตรงกันได้โดยใช้ตัวเลือก '-f'

ก่อนอื่น รายการไฟล์ในไดเรกทอรี:

$ ls -l /home/$USER/Desktop/tecmint
total 20
-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (3rd copy).txt
-rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (4th copy).txt
... (ไฟล์อื่นๆ)

จากนั้นข้ามไฟล์แรกในแต่ละชุดของผลลัพธ์ที่ตรงกัน:

$ fdupes -f /home/$USER/Desktop/tecmint
/home/tecmint/Desktop/tecmint9 (copy).txt
/home/tecmint/Desktop/tecmint9 (3rd copy).txt
... (ไฟล์ซ้ำที่เหลือ)

10. ตรวจสอบเวอร์ชัน fdupes ที่ติดตั้งอยู่

$ fdupes --version
fdupes 1.51

11. หากคุณต้องการความช่วยเหลือเกี่ยวกับ fdupes สามารถใช้สวิตช์ '-h'

$ fdupes -h
Usage: fdupes [options] DIRECTORY...
-r --recurse           for every directory given follow subdirectories encountered within
-R --recurse:          for each directory given after this option follow subdirectories encountered within (note the ':' at the end of the option, manpage for more details)
-s --symlinks          follow symlinks
-H --hardlinks         normally, when two or more files point to the same disk area they are treated as non-duplicates; this option will change this behavior
-n --noempty           exclude zero-length files from consideration
-A --nohidden          exclude hidden files from consideration
-f --omitfirst         omit the first file in each set of matches
-1 --sameline          list each set of matches on a single line
-S --size              show size of duplicate files
-m --summarize         summarize dupe information
-q --quiet             hide progress indicator
-d --delete            prompt user for files to preserve and delete all others; important: under particular circumstances, data may be lost when using this option together with -s or --symlinks, or when specifying a particular directory more than once; refer to the fdupes documentation for additional information
-N --noprompt          together with --delete, preserve the first file in each set of duplicates and delete the rest without prompting the user
-v --version           display fdupes version
-h --help              display this help message

ขอจบเพียงเท่านี้คราวนี้ แจ้งให้เราทราบว่าคุณเคยค้นหาและลบไฟล์ซ้ำใน Linux ยังไง? และความคิดเห็นของคุณเกี่ยวกับยูทิลิตี้นี้คืออะไร ใส่ Feedback มีค่าของคุณในส่วนความคิดเห็นด้านล่าง และอย่าลืม Like/Share เพื่อช่วยให้เราแพร่กระจายได้มากขึ้น

ผมกำลังทำงานบนยูทิลิตี้อีกตัวหนึ่งชื่อ fslint สำหรับลบไฟล์ซ้ำ จะโพสต์เร็วๆ นี้ และทุกคนจะชอบอ่านแน่นอน