CA BD NY
  • Categories

  • Recent Posts

  • RSS MySQL Hacker

  • RSS Apache Hacker

  • RSS MiniCTO

  • Meta

  • Keeping the find command in current directory only

    Published January 6th, 2009

    Problem Statement

    When using the find command, it has the ability to find whatever you are looking for in sub-directories. For example, find . -type f -name "*2008*.txt" -ls will find all files with 2008 and .txt in the name in the current directory or any of its sub-directories. This is good but if you are trying to move such files into a sub-directory that already has files with this pattern, you are going to find it difficult to use find. Here is a short tick that you can use to hack this.

    Limiting nosy find’s interest to current directory only

    To limit find to your current directory, just use the -maxdepth 1 in the command-line. For example:

    $ find . -maxdepth 1 -name "*2008*.txt"

    This command will tell find to look for files with 2008 and .txt in the name but limit the scope to only one level of depth, which is equal to the current directory.

    In our real-world scenario, we wanted to move these files to a sub-folder which is also called 2008 so here is what we ran:

    $ find . -maxdepth 1 -name "*2008*.txt" -exec mv {} 2008 \;

    Worked like charm!

    Get a Trackback link

    No Comments Yet

    Be the first to comment!

    Leave a comment

    Comment Policy: First time comments are moderated. Please be patient.