8+ Xargs 使い方 Article
Introduction
In today's world, where technology is advancing rapidly, it is imperative to have knowledge about various tools and commands that can make our work more efficient. One such command is xargs. It is a powerful Linux command that can perform various operations on a set of files or directories. In this article, we will discuss the basics of xargs and its usage.What is xargs?
Xargs is a command that reads a list of items from standard input and executes a given command for each item. It is useful when you need to perform a particular action on a set of files or directories. The command can be anything from a simple echo statement to a complex script.How to use xargs?
To use xargs, you first need to have a list of items that you want to perform a particular action on. This list can be generated using various commands like find or ls. Once you have the list, you can pipe it to xargs along with the command that you want to execute for each item. For example, if you want to delete all the .txt files in a directory, you can use the following command: ``` find . -name "*.txt" | xargs rm ``` This command will find all the .txt files in the current directory and its subdirectories and execute the rm command for each file.Options in xargs
Xargs comes with various options that can be used to customize its behavior. Some of the commonly used options are:- -I: This option is used to replace a placeholder in the command with the input item. For example: ``` echo "Hello {}" | xargs -I {} echo {} ``` This command will replace the {} placeholder with the input item and print "Hello" followed by the item.
- -p: This option is used to prompt the user before executing each command. For example: ``` find . -name "*.txt" | xargs -p rm ``` This command will prompt the user before deleting each file.
- -n: This option is used to specify the maximum number of arguments that can be passed to the command. For example: ``` find . -name "*.txt" | xargs -n 2 rm ``` This command will pass a maximum of 2 arguments at a time to the rm command.
Examples
Let's take a look at some examples to understand the usage of xargs better.- Example 1: Copying files ``` find . -name "*.txt" | xargs -I {} cp {} backup ``` This command will copy all the .txt files in the current directory and its subdirectories to the backup directory.
- Example 2: Compressing files ``` find . -name "*.txt" | xargs gzip ``` This command will compress all the .txt files in the current directory and its subdirectories using gzip.
- Example 3: Finding files ``` echo "*.txt" | xargs -I {} find . -name {} ``` This command will find all the .txt files in the current directory and its subdirectories.
0 Response to "8+ Xargs 使い方 Article"
Posting Komentar