การใช้ `xargs` เชื่อมคำสั่ง
เรียนรู้การสร้างและเรียกใช้บรรทัดคำสั่งจากอินพุตมาตรฐานด้วย `xargs` เพื่อทำงานต่าง ๆ ให้เป็นอัตโนมัติอย่างมีประสิทธิภาพ
การใช้ `xargs` เชื่อมคำสั่ง เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Linux Command Line Mastery และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Intro to `xargs`
xargs is a powerful command-line utility that acts as a bridge between commands. It takes input from standard input (stdin) and builds new command lines, then executes them.
- It reads items (usually words or lines) from stdin.
- It then uses these items as arguments for another command.
- This is incredibly useful for automating tasks involving many files or inputs from other commands.
Basic `xargs` Usage
Let's see xargs in its simplest form. Here, echo sends a list of words to xargs. xargs then takes these words and passes them as arguments to a second echo command.
Try running this example:
echo "apple banana cherry" | xargs echoChaining `find` with `xargs`
One of the most common and powerful uses for xargs is processing files found by the find command. find lists files, and xargs then takes those names to perform an operation.
First, we'll create some dummy files, then use find to locate .txt files and xargs to display their names.
touch doc1.txt doc2.txt report.pdf
find . -name "*.txt" | xargs echoDefault Argument Handling
By default, xargs tries to take as many arguments as possible from its standard input and appends them to a single execution of the target command.
- It reads items separated by spaces or newlines.
- It builds a command by adding these items at the end.
- If the list of items is too long for one command, it will automatically run the command multiple times.
Limit Arguments with `-n`
Sometimes you need to run the target command multiple times, with only a specific number of arguments each time. The -n option allows you to specify how many arguments xargs should pass to each command execution.
Here, xargs will run echo for every two words it receives:
echo "one two three four five six" | xargs -n 2 echoCustom Placement with `-I`
When the input item isn't just the last argument, you need more control. The -I option allows you to define a placeholder string (e.g., {}) that xargs will replace with each input item.
This lets you insert the item anywhere in your command, not just at the end.
echo "fileA.txt fileB.txt" | xargs -I {} echo "Processing: {}"Parallel Execution with `-P`
For tasks that can be run independently, xargs -P can significantly speed things up by executing multiple commands in parallel. You specify the number of parallel processes to run.
-P 0tellsxargsto run as many processes as possible.- Be cautious with resource-intensive commands, as this can overload your system.
- This is excellent for batch processing, like resizing many images.
Safe Handling with `-0`
Filenames in Linux can contain spaces, newlines, or other special characters. If not handled correctly, these can break xargs, as it usually splits input by spaces or newlines.
The solution is to use find -print0 (which outputs null-terminated strings) with xargs -0 (which safely reads null-terminated strings).
touch "my file with spaces.txt"
find . -name "*.txt" -print0 | xargs -0 echoPractical Use: Deleting Files
Let's combine what we've learned for a practical task: finding and deleting all .bak files in the current directory. We'll use find -print0 and xargs -0 rm for safety.
Caution: The rm command is permanent. Always double-check your commands before running!
touch file1.txt.bak file2.log.bak temp.bak
echo "Files before deletion:"
ls -l *.bak
find . -name "*.bak" -print0 | xargs -0 rm
echo "Files after deletion:"
ls -l *.bakQuick Check on `xargs`
Consider the following command:
echo "one two three four five" | xargs -n 2 echoWhat will be the exact output?
Recap: `xargs` for Automation
You've learned how xargs acts as a powerful bridge, taking standard input and turning it into arguments for other commands. It's a fundamental tool for efficient command-line automation!
- It's essential for chaining commands like
findwith other utilities. - Options like
-n,-I,-P, and-0provide fine-grained control over command construction and execution. - Using
xargs -0withfind -print0is crucial for safely handling filenames with special characters.
Keep practicing to master this versatile tool and streamline your shell scripting!
คำถามที่พบบ่อย
บทเรียน “การใช้ `xargs` เชื่อมคำสั่ง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การใช้ `xargs` เชื่อมคำสั่ง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การใช้ `xargs` เชื่อมคำสั่ง”
เรียนรู้การสร้างและเรียกใช้บรรทัดคำสั่งจากอินพุตมาตรฐานด้วย `xargs` เพื่อทำงานต่าง ๆ ให้เป็นอัตโนมัติอย่างมีประสิทธิภาพ คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การใช้ `xargs` เชื่อมคำสั่ง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Linux Command Line Mastery นี้ได้ไหม
ได้ บทเรียน Linux Command Line Mastery ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เชี่ยวชาญนิพจน์ทั่วไป (Regex)
- รูปแบบขั้นสูงของ `grep` และ `find`
- การใช้ `xargs` เชื่อมคำสั่ง
- การแก้ไขสตรีมในระดับขนาดใหญ่ด้วย sed และ tr