I'd like to pref ice with the fact that I am pretty new to really working deeply with bash and Linux, so any help would be immense even just in starting tips that worked for you.

The problem I have is I am working with a bunch directories and sub directories. I am trying the grep out a line.

grep 'Relaxed' */*

It isn't elegant but it gives me a list of directories which contain a file that has the line Relaxed in it(in my case it is an output from an electronic structure prediction calculation). I have about 100+ of these directories so sorting through which ones are done and which aren't is tedious.

What I am hoping for help with is a method to search through each of these directories and when the string for example:

Relaxed atomic coordinates (fractional):

is found within a file(Relaxed is enough of a line to recognize) in that directory use a for loop or something to move that entire directory into done pile(another directory) and leave the incomplete ones in the directory as is. So I will then have a pile of directories that are "done" and untouched ones that I can re-run.

Again any help would be appreciated and I can clarify if anything seems confusing.

Thank you!

  • 1
    You could start with grep options -r (recursive), and -l (it will print only the files names with matches), then parse the output to get the directories list. – Paulo Jan 25 '18 at 2:24
grep -rl 'Relaxed' | xargs dirname | xargs  mv -vt donedirectory

In slow motion:

  • grep -rl 'Relaxed' searches all the files and prints out their names. If they are all in the 1st level subdirectory of your current directory, using grep -l 'Relaxed' */* is also possible.
  • xargs dirname calls dirname with each file of that output to print out the directory name
  • xargs mv -v -t donedirectory moves all the resulting directories to the target directory.

If several files with "Relaxed" can be found in the same directory, you can avoid error messages by making the directory names unique:

grep -rl 'Relaxed' | xargs dirname | sort -u | xargs  mv -vt donedirectory

Note: command above assumes no nasties in files/directory names (spaces, etc...).

  • It ran but I needed to add -l for some reason to the grep command? Any idea why? I can print the error it gives if need be. – Alastair Price Jan 25 '18 at 15:52
  • the command I give above is grep -rl ... which is the same as grep -r -l..., so you must have overlooked something? – xenoid Jan 25 '18 at 16:15
  • Yep I am a dummy sorry for some reason I must have missed that. Thank you! – Alastair Price Jan 25 '18 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.