1

I have the following script setup at the moment:

#!/bin/bash
while true;
 do
  echo "Type in keyword & press enter..."
  read KEYWORD
  HERE=$(grep -i "$KEYWORD" */*/webvirtualmx)
  echo $HERE
  ANSWER2="y";read -p "Do you want to move to old? y or n?" ANSWER2;
  if [ "$ANSWER2" = "y" ]
  then mv -i -v $HERE /u1/OLD
  fi
  ANSWER="n";read -p "Do you have more keywords? y or n?" ANSWER;
    if [ "$ANSWER" = "n" ]
    then break
    fi
 done

Now the output of the echo part of the script looks like this:

> u/umind/webvirtualmx:servingtruth.org

I basically need to cut the "webvirtualmx:servingtruth.org" portion off from the path, so that the part of the code that runs the mv command, moves the entire directory, not just the file.

How would I go about telling to ignore the entire path, and only grab the directories path and apply it to the variable $HERE?

I.E

mv -i -v u/umind/ /u1/OLD/

I have about a hundred directories like this, obviously all named differently but they all follow this pattern:

letter/name/webvirtualmx:filename

another example:

l/laicc/webvirtualmx:si2tech.com

so on and so forth.

1

If $HERE holds a filename, then I think the simplest would be to do the following:

mv -i -v `dirname $HERE` /u1/OLD

If you want some safety, then you could do:

DIR=`dirname $HERE`
if test -d "$DIR"; then
  mv -i -v "$DIR" /u1/OLD
fi
  • Well the output of $HERE will always be different. The problem is that grep -i "$KEYWORD" */*/webvirtualmxwill always point to the correct directory WITH the file name. As I've given an example above. So when the 'mv' command is envoked on the variable "$HERE" it will try to move the output of grep, which is : dir/dir2/file:name I don't want it to run: mv dir/dir2/file:$KEYWORD I need it to run: mv dir/dir2/ – Pietro Aretino Feb 23 '17 at 21:50
  • That's why I assigned the output of dirname $HERE to $DIR, and then used that in the mv command. – Ed Neville Feb 23 '17 at 21:57
  • I see. So would the if statement look like: HERE=$(grep -i "$KEYWORD" */*/webvirtualmx) echo $HERE DIR="dirname $HERE" ANSWER2="y";read -p "Do you want to move to old? y or n?" ANSWER2; if [ "$ANSWER2" = "y" ] && test -d "$DIR"; then mv -i -v $HERE /u1/OLD fi – Pietro Aretino Feb 23 '17 at 22:08
  • No, you have the wrong type of quote mark. It should use backticks, or as you're doing use $( ). – Ed Neville Feb 23 '17 at 22:14
  • Yupp that was it. I'll post the final code above. Much obliged kind sir for your assistance. – Pietro Aretino Feb 23 '17 at 22:30
0

Big thanks to Ed Neville. As you can see below, he suggested passing the "dirname" command to the variable. Here is the final code. The script asks for a keyword, which it uses to grep for in sub-directories (based on where you put the script). It then grabs the directory path & disregards the name of the file, and moves the directory in its entirety to, wherever you want to and asks if you want to continue searching for keywords and moving directories until you reply with "n" i.e. no.

#!/bin/bash
while true;
 do
  echo "Type in keyword & press enter..."
  read KEYWORD
  HERE=$(grep -i "$KEYWORD" */*/webvirtualmx)
  echo $HERE
  DIR=$(dirname "$HERE")
  ANSWER2="y";read -p "Do you want to move to old? y or n?" ANSWER2;
  if [ "$ANSWER2" = "y" ] && test -d "$DIR";
  #then mv -i -v $DIR /u1/OLD
  fi
  ANSWER="n";read -p "Do you have more keywords? y or n?" ANSWER;
    if [ "$ANSWER" = "n" ]
    then break
    fi
 done

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.