In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The content of this article is mainly about how to use the Linux system xargs command to tell, the content of the article is clear and easy to understand, clear organization, very suitable for beginners to learn, worth reading. Interested friends can read along with Xiaobian. I hope everyone gets something out of this article!
When using Linux, have you ever had to string commands together, but one of them doesn't accept piped input? In this case, we can use the xargs command. xargs can send the output of one command as an argument to another command.
In Linux, all standard applications have three data streams associated with them. These are the standard input stream (stdin), standard output stream (stdout), and standard error stream (stderr). These flows are run through text, we send input (stdin) to commands using text, and the response (stdout) is displayed as text on the terminal window. Error messages are also displayed as text on the terminal window (stderr).
A great feature of Linux and Unix-like operating systems is the ability to pass the standard output stream of one command to the standard input stream of another. The first command does not care whether its output is written to the terminal window, and the second command does not care whether its input comes from the keyboard.
Although all Linux commands have three standard streams, not all commands accept the standard output of another command as input to its standard input stream. So we can't pipe input to these commands.
xargs is a command that builds execution pipelines using standard data streams. By using the xargs command we can make commands like echo, rm and mkdir accept standard input as their arguments.
The xargs command xargs accepts pipeline input and can also accept input from files. xargs uses this input as an argument to the command we specify. If we do not specify a specific command for xargs, echo is used by default. xargs always produces single-line output, even if the input data is multiline.
If we use the-1 (one file per line) option of ls, we get a list of file names:
$ ls -1 ./*. The sh command lists Shell script files in the current directory.
What happens if we pipe the output to xargs?
$ ls -1 ./*. sh | xargs
As you can see, the output is written to the terminal as a long string of text. Thus, xargs can pass output as arguments to other commands.
Using xargs with wc we can easily use xargs to have wc count the number of words, characters and lines in multiple files
$ ls *.c |xargs wc execution results as follows:
The command results show statistics for each file and the total number.
This command does the following:
ls lists all.page files and passes that list to xargs.
Xargs passes all filenames to wc.
Wc processes these file names as command-line arguments.
With xargs with confirmation messages we can use the-p (interactive) option to have xargs prompt us if we want to proceed.
If we pass a string of file names to the touch command via xargs, touch will create these files.
$ echo 'one two three' | xargs -p touch
The terminal displays the command to be executed, xargs waiting for us to type y or Y, n or N and press Enter to respond. If only Enter is pressed, it is considered n. The command is executed only if we type y or Y.
We press y and Enter, then use ls to check if the file has been created.
$ ls one two three
Using xargs with multiple commands We can use xargs with multiple commands with the-I (initial argument) option. This option defines the replacement string. The values we provide for xargs are inserted wherever replacement strings appear on the command line.
It's a little abstract, so let's take an example.
Let's start by using the tree command to view the subdirectories in the current directory. The-d (directory) option causes the tree command to ignore files and output only directories.
$ tree -d
There is now only one subdirectory images.
In directories.txt we have the names of the directories we want to create. Let's use cat to see what's in it.
$ cat directories.txt
We pass this as input data to xargs and execute the following command:
$ cat directories.txt |xargs -I % sh -c 'echo %; mkdir %' This command does the following:
cat directories.txt: Pass the contents of the directories.txt file (all directory names to be created) to xargs.
xargs -I %: substitution string % defined.
sh -c: Start a new subshell. - c (commond) Let the shell read the command.
'echo %; mkdir %': Each % is replaced with the directory name passed by xargs. The echo command prints directory names, and the mkdir command creates directories.
Command execution results:
We can verify that a directory has been created with a tree.
$ tree -d
Copy files to multiple locations We can use the xargs command to copy files to multiple locations with one command.
First, pipe the names of two directories to xargs. And let xargs pass only one argument at a time to the command in use.
To call cp twice, each time using one of the two directories as a command-line argument, we can do this by setting the-n (max number) option of xargs to 1.
The-v (verbose details) option is also used here to let cp feed back what is being done.
$ echo ~/dir1/ ~/dir2/ | xargs -n 1 cp -v ./*. c We copied the file into two directories, one directory at a time. cp feeds back details to let us see what was done.
Delete files in nested directories xargs will not interpret file names correctly if they contain spaces or other special characters (such as newlines). We can solve this problem by using the-0 (null terminator) option. In this case, xargs will use the null character as the final delimiter of the filename.
Here we take the find command as an example. Find has its own options for handling spaces and special characters in filenames, namely the-print0 (full name, empty character) option.
$ find . -name "*.png" -type f -print0 |The xargs -0 rm -v -rf "{}" command does the following:
find . -name "*.png": find will search the current directory for objects whose name matches *.png, type -f specifies to search only files.
-print0: Names will end with a null character, and spaces and special characters will remain.
xargs -0: xargs will also take into account that file names end in null, and spaces and special characters do not cause problems.
rm -v -rf "{}": rm will feed back the operation in progress (-v), recursively perform the operation (-r), and delete the file directly without sending an error prompt (-f). Replace '{}' with each filename.
After the command is executed, all subdirectories are searched and matching files are deleted.
Delete nested directories Suppose we want to delete a nested set of subdirectories, first look at it with a tree.
$ tree -d
$ find . -name "level_one" -type d -print0 |xargs -0 rm -v -rf "{}" This command uses find to search recursively in the current directory for a directory named level_one, and then passes the directory name to rm via xargs.
The difference between this command and the previous command is that the item searched for is the name of the topmost directory, and-type d specifies the directory to look for, not the file.
The name of each directory is printed when deleted. We can use tree to see the effect again:
$ tree -d
All nested subdirectories have been removed.
Delete all files except one file type We can use find, xargs, and rm to delete all types of files and keep only one type of file we want to keep. This requires the type of file you want to keep.
The-not option causes find to return all filenames that do not match the search pattern. We use the-I (initial parameter) option of xargs again at this point. The replacement string defined this time is {}. This has the same effect as the substitution string % we used earlier.
$ find . -type f -not -name "*.sh" -print0 | xargs -0 -I {} rm -v {}
After the command is executed, we confirm the result with ls. As you can see, only the files matching *.sh remain in the directory.
$ ls -l
Creating compressed files with Xargs We can search for files using the find command and create compressed files by passing the file name to the tar command via xargs.
We will search the current directory for * .sh files.
$ find ./ -name "*.sh" -type f -print0 |The xargs -0 tar -cvzf script_files.tar.gz command lists all.sh files and creates compressed files.
Linux versions are: Deepin, UbuntuKylin, Manjaro, LinuxMint, Ubuntu and other versions. Deepin is one of the best Linux distributions in China; Ubuntu Kylin is a derivative distribution based on Ubuntu;Manjaro is a Linux distribution based on Arch;LinuxMint's default Cinnamon desktop is similar to Windows XP and easy to use;Ubuntu is a Linux operating system based on desktop applications.
Thank you for reading, I believe you have a certain understanding of "Linux system xargs command how to use" this problem, go to practice it quickly, if you want to know more related knowledge points, you can pay attention to the website! The editor will continue to bring better articles to everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.