I would like to find directories that does not contain a file with name OSZICAR and then cd into that directory and do something more...

all i have now is:

find `pwd` -mindepth 2 -maxdepth 2 -type d -exec sh -c "echo {}; cd {}; ls; if [! -f $0/OSZICAR];echo "doing my thing";fi" \;

but there is error, could anyone help? Thank you

My original command without the Criteria of Not having OSZICAR is:

find `pwd` -mindepth 2 -maxdepth 2 -type d -exec sh -c "echo {}; cd {}; ls; cp ../../submit_script_Stampede.sh .; ls;sed -i s/Monkhorst/Gamma/ KPOINTS; cp CONTCAR POSCAR ;sbatch submit_script_Stampede.sh" \; 

2 Answers 2

for dir in $(find ${PWD} -mindepth 2 -maxdepth 2 -type d)
  cd ${dir}
  ls OSZICAR > /dev/null 2>&1; r=${?}
  if [ ${r} -ne 0 ]
    # do your thing here
  • Thanks... seems no easier way to just directly incorporate to one find command:)
    – user40780
    Mar 8, 2016 at 19:26
  • There is a possibility of making it a one liner but it just complicates the process in my opinion. Type these lines on a command prompt one by one. Execute it and then recall the line by up arrow (using bash I assume). You will see the one liner version. If you want to make an alias or something, you can.
    – MelBurslan
    Mar 8, 2016 at 19:30

Make sure that the [ and ] are space separated and remove the " around the argument for the echo command or replace those " by \". Also note that {} must be a separate argument.

So a corrected command would be:

find `pwd` -mindepth 2 -maxdepth 2 -type d -exec sh -c 'echo $1; cd $1; ls; if [ ! -f OSZICAR ];then echo "doing my thing";fi' dummy {} \;
  • Still having a error: dummy: -c: line 0: syntax error near unexpected token fi' dummy: -c: line 0: echo $1; cd $1; ls; if [ ! -f OSZICAR ];echo "doing my thing";fi'
    – user40780
    Mar 8, 2016 at 19:30
  • OK added a then. Whether this does what you like is still not clear but it lists all directories.
    – schily
    Mar 8, 2016 at 19:33

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.