What is The Difference Between exec and xargs

June 18th, 2010

There are very small things in linux based systems which often consfuse the users. Here in this article I’m going to discuss the specific use of find command and difference between exec and xargs.

What Does “find” Command Do?

The find command is used to find files in unix based systems. The command is versatile in nature and provides you with a lot of options to work with this.

What is Important About “find” Command?

The find command not only finds the files, but also allows you to take action on the files found. Now that you’ve found some specific files based on certain criteriae from your filesystem. I am going to explain this with the help of a few examples.

Give Me An Example

Let’s take an example.

  1. We’ll find the files of type “file”.
  2. We’ll find the files of type “directory”.
  3. We’ll change the “file” permissions to 644.
  4. We’ll change the “directory” permissions to 755.

Let’s say we want to work with home direcoty of user ganesh.

We’ll find the files by the command:

find /home/ganesh -type f

We’ll find the directories by the command:

find /home/ganesh -type d

Now we want to change the permissions of the files and directories found. The good news is that, we can do this on the fly.

Let me show you how. I’ll accomplish steps #1 and #3 togather.

find /home/ganesh -type f -exec chmod 644 {} \;

And to accomplish steps #2 and #4 togather.

find /home/ganesh -type d -exec chmod 755 {} \;

A Little explaination

find commands does the following:

  1. finds the files of type “f”, that means it finds regular files.
  2. Then it changes the file permissions by executing chmod.
  3. -exec tells find command that you want to run chmod on the files found out.
  4. {} acts as a place holder for the file found out. Let me tell you how.Let’s say it finds files employees, designations and few others. So, this step acts as:

    chmod 644 employees

    chmod 644 designations


    chmod 644 the_last_file_found_in_your_directory

    So, when you place {}, {} automatically replaces files employees, designations etc.

  5. \; acts as the end of the command for that particular file.

That all means, when it finds a file, it runs a chmod command for every file in the home directory.

Same applies to directory permissions as well.

Now let’s say you’ve got about 10000 files and 2000 directories. In that case above method runs chmod command 10000 times for files and 2000 times for directories. So, chmod runs in total 10000+2000=12000 times.

Very inefficient. Isn’t it? :)

Now let’s make it efficient. Here xargs comes to our rescue.

Let me show you how this acts.

find /home/ganesh -type f | xargs chmod 644

This means now first find finds all files then, at the end xargs changes permissions of all files at once. That means, it runs only one time. 10,000 times faster!!! Great.

Similarly for directory permission changes.

find /home/ganesh -type d | xargs chmod 755

This helps to change permissions of all directories found in a single step. 2000 times faster!!! good to see.

Tags: ,
Posted in Tips and Tricks | 1 Comment »


One Response to “What is The Difference Between exec and xargs”

  1. Ole Tange Says:

    Your use of xargs is dangerous. To see why read: http://en.wikipedia.org/wiki/Xargs#The_separator_problem

    Consider using GNU Parallel http://www.gnu.org/software/parallel/ instead. Watch the intro video to GNU Parallel at http://www.youtube.com/watch?v=OpaiGYxkSuQ

Leave a Reply

 Comment Form 


More articles from the category: Tips and Tricks

What is The Difference Between exec and xargs

There are very small things in linux based systems which often consfuse the users. Here in this article I’m going to discuss the specific use of find command and difference between exec and xargs.

Understanding The diff Command In Unix

It’s not an uncommon requirement for Unix system administrators to know the difference between two files. The diff command in Unix serves the purpose. Here I am going to discuss the diff command. It’s quite common but little understood command. I hope after reading this article, the Unix visitors will be able to understand the usage properly and benefit from it. Here you go…

How To Find Common Lines Between Two Text Files In Unix

Those working in Unix environment must be aware of the requirement to find common
lines between two Unix files. In this article I’m gonna introduce you to solve this problem.

How To Join Two Files Vertically

In Unix, sometimes we come across situations where we need to join two files’ output side by side vertically. This is also referred to as vertical joining of files. In such situations, paste command comes handy. With the help of this command you not only can vertically join the files, but insert some delimiter as well….

How To Recover A Superblock

If fsck or mount commands give errors then pretty good chances are there that the superblock is corrupt. The dd command comes to rescue. We know that the superblock resides in 31st block also. So, to recover that we use the following command: # dd count=1 bs=4k skip=31 seek=1 if=/dev/my_lv  of=/dev/my_lv count: How many blocks […]

Ways To Zip The Directory Structure of Unix

Often its the requirement of system administrators to move the directories in between the servers. Also the confusion arises which utility to use for the best results. Normally zip, tar, cpio utilities are used the most. I’m writing a short introduction of zip and tar.

How To Split or Cut A File Vertically(Column wise)

In Unix environment it’s often required to print specific columns from a file. If the rows of file are having some proper delimiters then other Unix command like awk come into picture and quite handy. But if say you want to print out the 5th character and then 7th to 16th character, then other commands fail and in such situations the cut command comes to rescue. Here is short primer of cut command.