Linux Commands

How To Use xargs With Find in Linux

This tutorial explains how to use the commands xargs and find to make combined operations.

After reading this tutorial, you can search files using the find command and execute a specific command based on matched results.

Instructions and examples are helpful for most Linux distributions. The content is optimized both for new and experienced Linux users.

All steps described in this article contain screenshots to make it easy for all Linux users to understand and apply them.

Usage examples included in this article are the following:

  • Find and delete files by extension (File type).
  • Find and delete files by name.
  • Find and change file permissions based on current permissions.

Brief Introduction To Find and xargs Commands

The xargs command, when combined with other commands like find, uses the output of the first command as an argument.

For example, let’s run the find command to identify files with a specific extension or file type. We can add the xargs command to execute an action for all files matching the extension specified to find.

As you can see in the following line, a pipe separates COMMAND 1 and COMMAND 2, where COMMAND 1 may be any command like ls, and COMMAND 2, including the xargs command, followed by a specific action like cat.

[COMMAND 1] | [COMMAND 2]

The applicable example would be:

ls | xargs cat

This command will list all files within the current directory. Then the output (listed files) will be used by xargs as an argument, and their content will be printed as instructed by xargs cat. See the following screenshot:

How To Find and Move Files Using xargs

The first section of this tutorial explains how to use find and xargs commands to search and move files matching a condition, in this case, the file type.

Let’s begin by finding files by extension and moving them to a specific directory using xargs.

Before beginning, run the ls command to show files and directories within my subdirectory named testdir.

ls

As you can see, there are different file types, including .txt, .c, and files without extensions. Also, pay attention to the tutorialdir directory.

Let’s assume you want to move files with a specific extension to a directory. The syntax is the following:

find <SourceDir> -name '*.<Extension>' -type f | xargs mv -t <DestinationDir>

In the previous syntax, the -name option precedes the file name or condition based on the file name. The type f option specifies that the find command deals with files and not directories. The -t option previous to the destination directory is used to define the target directory. Pay attention to the wildcard (*) previous to the extension, instructing the command find to search all files of the specified extension independently of the name.

Thus, if you want to move all .txt files to the directory named tutorialdir, execute the following command:

find . -name '*.txt' -type f | xargs mv -t tutorialdir

As you can see, after running the ls command twice, the files were moved from the current directory to the tutorialdir subdirectory.

The syntax is the same for all file types. Let’s repeat it, this time for .c files:

find . -name '*.c' -type f | xargs mv -t tutorialdir

The first ls command shows three .c files in the current directory. After running the find command with xargs, the tree command shows all .c files were moved to the tutorialdir directory, where .txt files were carried in the previous example.

Find and Delete Files and Directories by Name With xargs

The syntax to find and delete files using xargs is the following:

find <Dir> -name <File Name> | xargs rm

This is where <Dir> is the parent directory you search files, and <File Name> is the name of the file you want to find and remove.

Let’s see the current scenario in the following screenshot:

ls

In the first example, choose files by type, as done in the previous section, to remove them, as shown below:

find . -name "*.c" | xargs rm

As you can see above, all .c files were successfully deleted.

Now, the scenario is the following:

ls

In the current example, I will use the wildcard to remove all files whose name starts with “file”, independently of the name continuation.

find . -name "file*" | xargs rm

Until now, this article explained how to deal with files. Now, let’s see how to use find and xargs with directories. In the new scenario, there are five new directories: dir1, dir2, dir3, dir4, and dir5, as shown in the following image:

ls

Assuming you want to remove all directories with names starting with “dir”, run the same command. However, after the rm command, add the -r flag used to deal with directories, as shown in the following figure:

find . -name "dir*" | xargs rm -r

Find and Delete Files Based on Permissions

This section describes finding files by specific permissions and changing them using xargs.

As you can see below, files something1.txt, something2.txt, and something3.txt have full permissions for everyone (777).

ls -l

The syntax to find files by permission and change them is shown below:

find <SourceDir> -perm <Permissions> | xargs chmod <NewPermissions>

To find all files with full permissions to everyone (777) in the current directory and change them to full permissions for the owner and read and execute permissions for group users and others (755), run the command executed in the following:

find . -perm 777 | xargs chmod 755

Conclusion

As you can see, the xargs command, when combined with the find command, is handy for bulk tasks or specific tasks when you don’t know a file location. Both commands are easy to implement and can be incorporated by new users to ease their experience with the Linux terminal. Find and xargs are basic Linux commands any Linux user must know how to apply. Xargs can be combined with other basic commands like ls. The instructions explained above are helpful for all Linux distributions.

Check out more articles for more Linux tips and tutorials.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.