1

I'm using the following code to sort recordings and captures from my CCTV into dated sub directories that are more than a day old.

Is there any way to reference the results of the echo grep date section of the IF code as a variable so that it can be used when creating a folder?

I could just create a variable on a separate line with another use of grep (or define the variable and reference it in the IF statement) but would prefer if it were all on one line.

File names are similar to 'MDAlarm_20160417-120925.jpg' so the contents of the variable would be 2016-04-17.

for f in ~/FI9803P_00626E5755DE/**/*;
do
    if [[ `echo $f | grep -oP '\d{8}' | date -f - +'%Y-%m-%d'` != `date +'%Y-%m-%d'` ]]; then
        mkdir -p $(dirname $f)/$varFromIF; echo "Made it" #mv $f $(dirname $f)/$varFromIF
    fi
done
3
  • You might want to consider using find with option -mtime to find files older than a day. That's way faster than using date on each file: find ~/FI9803P_00626E5755DE/ -type f -ctime +1 | while read f; do mkdir …; done. Commented Apr 20, 2016 at 12:42
  • Using two lines and using a descriptive variable name instead of one line typically makes the code more readable for the next developer, having to maintain the code. Commented Apr 20, 2016 at 12:43
  • Good point Alfe. This is only for home, if it were for work, I'd be sure to make it a lot more self explanatory. Commented Apr 20, 2016 at 13:36

1 Answer 1

1

Yes. You can do it like this:

for f in ~/FI9803P_00626E5755DE/**/*;
do
    if [[ "${varFromIF=$(echo $f | grep -oP '\d{8}' | date -f - +'%Y-%m-%d')}" != $(date +'%Y-%m-%d') ]]; then
        mkdir -p $(dirname $f)/$varFromIF; echo "Made it" #mv $f $(dirname $f)/$varFromIF
    fi
done
Sign up to request clarification or add additional context in comments.

7 Comments

Ahh thanks, I assumed it would be something like this although it's not working for me at the moment. Should I be using some backticks similar to my original IF ?
Strange. It works for me with really simple example, like: if [[ "${varFromIF=$(date +'%Y-%m-%d')}" == date +'%Y-%m-%d'` ]]`
You can try use backticks to check if it works. You can also print the variable to debug it.
Ok, it appears that the varFromIF variable is only set once then stays the same. eg. if the first date it gets is 2016-04-17, then the variable will stay as that even if the file name changes. Likewise, if I create a directory in the search path and that is processed first (with no grep match) the varFromIF will be empty for the duration of the script. This is likely something that I've done incorrectly at my end.
Instead of backticks you should use $(…) instead. That is more modern and you can easily nest it. With backticks you would have to quote the nested backticks. And furthermore on SO you can talk about $(…) better as @AvihooMamka comment above displays ;-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.