Practical Examples of Linux xargs Command


Are you looking for different ways to simplify your Linux workflow and want to automate your tasks? The Linux xargs command may be just what you didn’t know. In this article, We will see some practical examples of using xargs to execute commands on a list of items generated by other commands.

As someone who uses Linux regularly, We have found the xargs command to be a valuable tool in our workflow. It helps us to perform complex tasks on a large number of files or data with ease. The xargs command is especially useful when working with long lists of items or data, as it simplifies the process of executing a command on each item individually.

With xargs, we can execute commands such as copy, delete, compress, rename, and count a large number of files or data. In the following article, we’ll go through some practical examples of how to use xargs. By the end of this, you'll have a good understanding of how to use xargs and be able to take your Linux skills to the next level.

So, if you're ready to boost your Linux rate of progress, let's dive into the practical examples of the Linux xargs command.

1. Copy a List of Files to a New Directory

Suppose you have a list of files in a directory that you need to copy to a new directory, the xargs command with the cp command can be used to accomplish this task quickly and easily. Rather than copying each file manually one by one, this method allows you to execute the copy operation on all the files at once.

Here's an example of copying files to a new directory −

ls *.txt | xargs -I '{}' cp '{}' /path/to/new_directory/

The command will generate the output similar to this −

file1.txt
file2.txt

To copy all ".txt" files in the current directory to a new directory, we start by using the "ls" command to list all ".txt" files. The resulting output is passed to the xargs command. By using the -I option, we can replace the placeholder '{}' with the actual filename. Then, the "cp" command is used to copy each file to the new directory. In summary, this command takes advantage of xargs' ability to process the output of one command as input to another command, making it easy to copy multiple files to a new location in one go.

2. Delete All Files with a Specific Extension

To delete all files with a specific extension in a directory, you can use the xargs command with the rm command.

Here's an example of deleting files with specific extensions −

ls *.log | xargs rm

The output will look similar to this 

file1.log
file2.log
File3.log

Suppose you want to delete all files in the current directory with the ".log" extension. You can do it by using the above command. This command starts by listing all the files with the ".log" extension in the current directory, and then it passes each of those file names to the "rm" command using the xargs command. The "rm" command deletes those files one by one, leaving you with a cleaner and more organized directory.

3. Compress All Files in a Directory

To compress all files in a directory, you can use the xargs command with the tar command.

Here is an example to compress all files in a directory −

ls | xargs tar -czvf archive.tar.gz

The terminal output will look similar to this 

user@ubuntu:~/directory$ ls | xargs tar -czvf archive.tar.gz
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
archive.tar.gz
user@ubuntu:~/directory$

This command basically takes all the files present in the current directory and sends them to the xargs command. Xargs then passes each filename to the tar command, which compresses all the files into a tar file. In other words, this command is used to create an archive of all the files in the current directory.

4. Convert Multiple Files to Uppercase

If you want to convert multiple files to uppercase in a directory, you can use the xargs command with the mv command.

Here's an example of converting multiple files to uppercase −

ls | xargs -I '{}' sh -c 'mv "{}" $(echo "{}" | tr "[:lower:]" "[:upper:]")'

The terminal output will look similar to this 

file1.txt
file2.txt
file3.txt
mv file1.txt FILE1.TXT
mv file2.txt FILE2.TXT
mv file3.txt FILE3.TXT

The following command looks at all the files in the current directory and sends the list to the xargs command. The -I option lets us use the actual file names instead of a placeholder '{}'. The command then uses the sh command to execute a shell command that converts the file names to uppercase and renames the files.

5. Count the Number of Lines in Multiple Files

If you want to count the number of lines in multiple files in a directory, you can use the xargs command with the wc command.

Below is an example to count the number of lines in multiple files −

ls | xargs wc -l

The output will look like this 

12 file1.txt
24 file2.txt
6 file3.txt

The command "ls | xargs wc -l" will display the number of lines in each file present in the current directory. This is done by using the 'ls' command to list all files in the current directory, and then passing the output to 'xargs'. The 'xargs' command then takes each file name and passes it to the 'wc' command, which counts the number of lines in each file.

6. To Generate a List of all Linux User Accounts that are Present in the System

If you want the list of all the user accounts that is associated with the Linux system then you can refer to the following command.

cut -d: -f1 < /etc/passwd | sort | xargs
  • cut -d: -f1 < /etc/passwd  This extracts the first field from the /etc/passwd file, which contains a list of users on the system, separated by a colon (:) delimiter. The -d option specifies the delimiter to use, and the -f option specifies which field to extract. In this case, we're extracting the first field (i.e., the username).

  • sort  This sorts the list of usernames in alphabetical order.

  • xargs  This takes the list of sorted usernames and passes them as arguments to the next command.

When you run this command you will get this kind of expected output 

$ cut -d: -f1 < /etc/passwd | sort | xargs echo
bin daemon ftp games gnats irc list lp mail man messagebus news nobody proxy root systemd-network sync sys syslog systemd-bus-proxy systemd-journal-gateway systemd-journal-remote systemd-timesync uucp www-data

Conclusion

In conclusion, the xargs command is a versatile and useful tool that can be used to automate complex tasks on large sets of data or files. In this article, we have provided some practical examples of how to use xargs command to perform various operations such as renaming, compressing, and counting the number of lines in multiple files in a directory. The xargs command saves time and simplifies repetitive tasks by streamlining the process of running commands on multiple files. As a Linux user, mastering the xargs command is an essential skill that can significantly enhance productivity and workflow efficiency.

Updated on: 28-Jul-2023

254 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements