Archive for June, 2010

What is The Difference Between exec and xargs

Friday, 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 »