[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Use 'find' to locate files matching regular expression

Asked by DrNikon224 in Regular Expressions, Bourne-Again Shell (bash), Shell Scripting

Tags: regex bash linux shell find

I'm trying to write a `find` command to locate files/directories within a certain directory matching a certain regular expression. As it is written now, I'm running a simple find, piping over to `grep`, then piping over to `xargs` to move the file.

The problem arises when a directory matches the expression. Due to the way that `find` handles directories and their contents, the directory is listed first in the result, causing it to be moved, but then the `xargs`command tries to execute the `mv` command on the directory contents (because the pattern matches the parent directory which is part of the `find` output) which are no longer where they were before the command was issued, causing an error ("mv: cannot stat `/path/to/file': No such file or directory") to be generated. This command is part of a script that is called by cron, and these errors generate annoying emails to me. The end result is what I want, but there has to be a better way of writing this line to prevent these errors.

I want to incorporate the regex and the mv into the find command itself, using the -regex and -exec options, but I haven't figured out how to do it yet. The -regex is what is holding me up - probably something to do with the -regextype, but I'm lost there as well. Haven't been able to find anything documenting -regex and -regextype online.

This is running on RedHat Enterprise Linux 5 as part of a BASH script.
1:
2:
3:
4:
5:
# The command as it is run now
find /home/USER/files/received/ | grep -E "/[Cc]64-[0-9]{1,2}-[A-Za-z]{2,5}-[0-9]{1,2}|/[Cc]64-[0-9]{1,2}-[A-Za-z]{2,5}-[A-Za-z]-[0-9]{1,2}" | xargs -I "%" mv "%" /home/USER/files/sorted/
 
# I want to use something like this to prevent the "mv: cannot stat" errors
find /home/USER/files/received/ -regex "/[Cc]64-[0-9]{1,2}-[A-Za-z]{2,5}-[0-9]{1,2}|/[Cc]64-[0-9]{1,2}-[A-Za-z]{2,5}-[A-Za-z]-[0-9]{1,2}" -exec mv "{}" /home/USER/files/sorted/ \;
[+][-]09/14/09 07:13 PM, ID: 25331023Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Regular Expressions, Bourne-Again Shell (bash), Shell Scripting
Tags: regex bash linux shell find
Sign Up Now!
Solution Provided By: Tintin
Participating Experts: 2
Solution Grade: B
 
[+][-]09/13/09 06:44 PM, ID: 25322591Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/13/09 06:56 PM, ID: 25322639Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/13/09 07:11 PM, ID: 25322674Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/13/09 07:16 PM, ID: 25322685Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/13/09 07:22 PM, ID: 25322696Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/13/09 07:23 PM, ID: 25322698Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/13/09 07:40 PM, ID: 25322758Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/13/09 08:08 PM, ID: 25322817Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/14/09 05:05 AM, ID: 25324567Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625