Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การแก้ไขปัญหา >> ข้อผิดพลาด Windows

วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10

เมื่ออ่านหัวข้อนี้ครั้งแรก อาจคิดว่ามีความผิดพลาดทางเทคนิค เพราะ Linux ไม่ใช่ส่วนหนึ่งของ Windows OS มันเป็น OS ที่ต่างกันสมบูรณ์ ท่านี้ก่อนหน้านี้ วิธีเดียวที่จะใช้ Linux Scripting กับ Windows คือติดตั้ง Cygwin, Dual Boot Linux, หรือติดตั้ง Samba

แต่นั่นเปลี่ยนไปใน Windows 10! มีหลาย Distribution ของ Linux บน Microsoft Windows Store บทความก่อนหน้านี้เกี่ยวกับ Kali Linux อธิบายวิธีได้ Linux Distribution เฉพาะใน Windows 10

ข้อดีของการใช้ Linux Scripts บน Windows คือใช้ภาษา Scripting อันทรงพลังของ Linux ทำให้งานอัตโนมัติ โดยเฉพาะหากคุ้นเคยกับ Linux Scripting มากกว่า PowerShell หรือ DOS Batch Files บทความนี้อธิบายการเขียน Linux "bash" Script เพื่อจัดระเบียบ Windows Desktop ทั่วไป หากใหม่กับ Linux จะเรียนรู้พื้นฐานและสร้าง Utility ที่มีประโยชน์พร้อมกัน

เราจะใช้ Ubuntu Linux Terminal (มีบน Windows Store ใน Windows 10) ซึ่งรองรับ bash, ssh, git, apt และคำสั่ง Linux อื่นๆ ต้องติดตั้งก่อนหากยังไม่มี วิธีอธิบายอยู่ที่นี่

Linux Command Line

หลังติดตั้ง Ubuntu และรัน จะได้หน้าต่าง Command Line คล้ายภาพ นี่คือ Bash Linux Command Line:

วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10

เราจะเขียน Script สามารถแก้ไขไฟล์ด้วย Editor ใดๆ รวมถึง Notepad ใน Windows หรือ vi ใน Linux

เมื่อเปิด Ubuntu Terminal ตำแหน่งจะอยู่ที่ Home Directory ใน Ubuntu Workspace สามารถตรวจสอบได้ด้วยคำสั่ง:

echo $HOME

จะแสดง Home Directory ตามภาพ Linux ใช้ Forward Slash (/) แทน Directory

วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10

เพื่อความสะดวก เราจะวาง Script ไว้ที่ Home Directory

ขั้นตอนต่อไป หาตำแหน่ง Desktop ในรูปแบบ Linux Path เนื่องจากต้องใช้เพื่อดำเนินการกับไฟล์ใน Directory ที่ถูกต้อง

ใน Terminal พิมพ์ cd / จะไปที่ Root ของ Ubuntu Environment

พิมพ์ ls แสดง Directory ทั้งหมด จะเห็นแบบนี้:

วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10

ต้องหา User Desktop โดยสมมติอยู่ที่ C Drive ให้เข้า Directory mnt ซึ่ง Windows Drives จะถูกระบุไว้:

วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10

ต้องรู้ว่า Desktop อยู่ Directory ไหน ใน Windows หาได้จาก Right-click Desktop Icon ใน Quick Access ของ File Explorer จะแสดงตำแหน่ง Desktop

วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10

จากนี้ สามารถ Change Directory ใน Linux Terminal ได้ (Backslash ใน Windows = Forward Slash ใน Linux) ในตัวอย่าง Path สำหรับคำสั่ง cd คือ:

cd /mnt/c/Users/gofor/OneDrive/Desktop

จากนั้น List ไฟล์บน Desktop ด้วย ls:

ถ้า Desktop เยอะเหมือนของผม จะได้รายการไฟล์ยาวๆ:

วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10

Desktop ใน Windows ดูจะแยกๆ:

วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10

ยังมีไอคอนบน Monitor อีก 2 จอ ต้องจัดกลุ่มและจัดระเบียบไฟล์เหล่านี้! Script ที่ให้มาจะนำแต่ละ File Type ไปไว้ใน Folder ที่ตั้งชื่อเกี่ยวข้องบน Desktop

เช่น Shortcut Files (*.lnk) จะย้ายไป Folder SHORTCUTS
Image Files (.jpg, .png, .bmp, .svg) ไป Folder IMAGES
Documents/Office Docs (.docx, .pdf, .xls) ไป Folder OFFICEDOCS

เมื่อรัน Script ไฟล์ทุกอย่างจะจัดระเบียบอยู่ใน Directory ที่สร้างไว้สำหรับ Category นั้น ทำให้ Desktop ไม่แยกและไฟล์จัดระเบียบดีขึ้น สามารถสร้าง Folder Types และกำหนด Criteria ไฟล์ได้ตามต้องการ ไม่จำเป็นจัดตาม File Type เพียงอย่างเดียว สร้าง Category โฟลเดอร์ได้เช่น HRFILES, PROJECTFILES ตัวอย่างนี้จัดตาม File Type

สร้าง Category และ File List

ต้องมี 2 ไฟล์:

A) ไฟล์ CSV คั่นด้วย Comma เก็บ Category และ File Types แต่ละ Category จะเป็นชื่อ Directory บน Desktop สร้าง CSV ได้ด้วย Editor ใดๆ ตัวอย่างใช้ Notepad
B) Linux Script อ่าน Category File และประมวลผล Tidy Function

สร้างไฟล์ใน Current Directory ใน Terminal พิมพ์:

notepad cleanup.csv

ไฟล์ยังไม่มี จะถามสร้างใหม่ กด Yes

ใส่รายละเอียดในรูปแบบ category,filetype1,filetype2,filetype3,... 1 บรรทัดต่อ 1 Category:

SHORTCUTS,lnk
IMAGES,jpg,png,svg
DOCUMENTS,txt,docx,doc,pdf

Field แรก = ชื่อ Directory บน Desktop ที่จะวางไฟล์ Field ที่เหลือ = File Types ที่ต้องการย้าย

ดังนั้น SHORTCUTS Folder จะได้ *.lnk ทั้งหมด IMAGES Folder จะได้ *.jpg, *.png, *.svg DOCUMENTS Folder จะได้ *.txt, *.docx, *.doc, *.pdf จัดระเบียบไฟล์เข้า Directory ที่ถูกต้อง

หลังสร้าง CSV แล้ว พร้อมเขียน Script จะชื่อว่า cleanup.sh Script นี้มี Validation น้อยมาก ต้องแน่ใจว่า CSV Format ถูกต้อง มิฉะนั้นจะไม่ทำงาน เราแค่เช็คว่าไฟล์/โฟลเดอร์มีอยู่จริงขั้นต่ำ

แก้ไข Script ใน Terminal ได้หากรู้ Unix Editors (vi) หรือใช้ Notepad:

notepad cleanup.sh

Script จะตั้งค่าตัวแปร CSV Filename และ Desktop Location ในตัวอย่างของผม:

บรรทัดแรก 2 บรรทัดใน Script:

DESKTOP=/mnt/c/Users/<your windows location>/Desktop
CSV=cleanup.csv

Script เต็มอยู่ด้านล่าง อ่าน Comments (ขึ้นต้น #) เพื่อเข้าใจคำสั่ง หากไม่แน่ใจคำสั่งไหน พิมพ์ man <command name> ใน Linux Command Line

ทำให้ Cleanup Script Executable และรัน

เพื่อรัน Script จาก Home Command Line ต้องทำให้ Executable:

chmod +x cleanup.sh

รัน Script (อยู่ Current Directory):

./cleanup.sh

Desktop ก่อนและหลังรัน Script โฟลเดอร์ที่มีไฟล์ย้ายไปถูกสร้าง Desktop สะอาดขึ้น:

Before: วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10 After: วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10

โฟลเดอร์ SHORTCUTS ที่มี Shortcuts ทั้งหมดย้ายจาก Desktop:

วิธีใช้ Linux Scripts จัดระเบียบ Desktop ใน Windows 10

Full Desktop Tidy Linux Script สำหรับ Windows

อ่าน Comments (ขึ้นต้น #) ใน Script เพื่อเข้าใจการทำงาน

# Set variables for the location of the local desktop or any directory you wish to clean up, and, the name of the CSV file.
DESKTOP=/mnt/c/Users/gofor/OneDrive/Desktop
CSV=cleanup.csv

# Test to see if the cleanup csv file exists, by using a linux "test" command and the flag "-f" for file existence.
# If the file does not exist, output an error message and then quit the script.
if [ ! -f ${CSV} ]; then
 echo "The cleanup.csv file does not exist."
 exit -1
fi

# Now convert the CSV if it has been saved in dos format to Unix format by removing carriage return characters.
# Save the output to a temporary file, then rename the temporary file back to the original name.
tr -d '\r' < $CSV > temp.csv
mv temp.csv $CSV

# Now, go through the CSV file line by line, and store the name of the first argument, which is the
# category/directory name, upon which, all remaining arguments are the file types which will be
# placed into these directories.
while read csvline
do
 count=1
 for filetype in `echo "$csvline" | tr , '\n'`
 do
 if [ $count -eq 1 ]; then
 # As this is the first argument, check to see if the folder already exists, and if not, create it.
 if [ ! -d ${DESKTOP}/$filetype ]; then
 # The directory does not exist, so we will create it.
 mkdir ${DESKTOP}/$filetype
 fi
 CATEGORY=${filetype}
 else
 # Output a friendly message indicating what the script is doing.
 echo "moving *.${filetype} to ${CATEGORY}"
 # Don't display any error messages (i.e. >2 /dev/null) when moving files in case they don't exist,
 # so the "mv" command is "silent".
 mv ${DESKTOP}/*.${filetype} ${DESKTOP}/${CATEGORY} 2> /dev/null
 fi
 count=`expr $count + 1`
 done
done < cleanup.csv