World is Welcome To The World of Linux

Journey into the world of linux

Menu
  • About
  • Welcome
Menu

What is The Difference Between exec and xargs

Posted on June 18, 2010 by Ganesh Sharma

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

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.

2 thoughts on “What is The Difference Between exec and xargs”

  1. Ole Tange says:
    July 25, 2010 at 03:15

    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

    Reply
  2. Ambika says:
    October 24, 2011 at 14:57

    nice…..

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Date Command Tutorial(Video)
  • grep Command Tutorial – 1(Video)
  • Introduction To ls Command(Video)
  • Chapter 3
  • Set Position Of Poll Module Into Joomla

Recent Comments

  • Kansas City Trailer Proz on Physical Volume In AIX – A Primer
  • Create volume group in AIX | myunixsheet on How To Create Volume Group
  • Restore of AIX backup on other Unix system - TecHub on What is mksysb And What Are Its Components
  • Firewall Unleashed - InfoSec Institute on Packet Filtering Firewall: An Introduction
  • Manwendra on Proxy Firewall and Gateway Firewall: Introduction

Archives

  • January 2019
  • June 2010
  • May 2010
  • February 2010
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008

Categories

  • 30 Days To Joomla WebSite Setup
  • A Journey To The World of Linux System Administration
  • Aix
  • Backups
  • Books
  • Firewalls
  • Introduction
  • Joomla
  • Joomla Backup
  • Joomla Web Technology
  • Linux
  • LPAR and Virtualization
  • LVM
  • Pluggable Authentication Modules
  • section navigator pro
  • Security
  • Security Knowledge Base
  • Tips and Tricks
  • Uncategorized
  • World is Welcome Products

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
©2023 World is Welcome To The World of Linux | WordPress Theme by SuperbThemes