AustinA2
Posts: 12
Joined: Mon Mar 04, 2019 7:12 pm

Finding and moving files based on year

Thu Mar 26, 2020 7:54 pm

I guess this is more of a linux question than a raspberry pi question.

I am trying to do something i thought would be simple, but i have not found a solution that works.

I am trying to go through old "syslog" files and move anything from, for example, the year 2019 to a file called 2019.arc. I want it to be based on the date the file was created and/or last edited (the date that shows up when you do 'ls -last'.

So something like find <all logs from the year 2019> and mv them to /new/directory/2019.arc

or just mv <anything from the year 2019> to /new/directory/2019.arc


Thanks in advance

jahboater
Posts: 5226
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Finding and moving files based on year

Thu Mar 26, 2020 8:05 pm

A job for "find" by the look of it.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

User avatar
B.Goode
Posts: 9627
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Finding and moving files based on year

Thu Mar 26, 2020 8:07 pm

AustinA2 wrote:
Thu Mar 26, 2020 7:54 pm
I guess this is more of a linux question than a raspberry pi question.

I am trying to do something i thought would be simple, but i have not found a solution that works.

I am trying to go through old "syslog" files and move anything from, for example, the year 2019 to a file called 2019.arc. I want it to be based on the date the file was created and/or last edited (the date that shows up when you do 'ls -last'.

So something like find <all logs from the year 2019> and mv them to /new/directory/2019.arc

or just mv <anything from the year 2019> to /new/directory/2019.arc


Thanks in advance


There is a standard Linux tool named find that might be just what you need...

(A me-too echo... but I'll leave it to show that I concur with the preceding reply... )

AustinA2
Posts: 12
Joined: Mon Mar 04, 2019 7:12 pm

Re: Finding and moving files based on year

Thu Mar 26, 2020 9:18 pm

jahboater wrote:
Thu Mar 26, 2020 8:05 pm
A job for "find" by the look of it.
I was hoping for a little bit more than that. As stated in my initial post, i already knew find was the right command.... But what is the entire anatomy of the command?

Thanks,
Austin


AustinA2
Posts: 12
Joined: Mon Mar 04, 2019 7:12 pm

Re: Finding and moving files based on year

Thu Mar 26, 2020 10:48 pm

B.Goode wrote:
Thu Mar 26, 2020 9:45 pm

Code: Select all

man find
or http://man7.org/linux/man-pages/man1/find.1.html



For tutorial-level guidance, an internet search tool brings up results like
https://www.computerhope.com/unix/ufind.htm
https://www.booleanworld.com/guide-linux-find-command/
Maybe this is how this forum works i guess... so if I am in the wrong, my apologies.

I have looked at the man pages, im having trouble finding a way to do this correctly. I appreciate you pointing me to the source material, but if you know what to do I would appreciate if you just told me. If you don't know for sure if find will work and that's why you're referencing the man pages, then thanks for doing what you can....

User avatar
DougieLawson
Posts: 37763
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Finding and moving files based on year

Thu Mar 26, 2020 11:03 pm

find /path/to/files* -mtime +365 -exec somecommandhere {}

For 2020 you may need +366 for the mtime value.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

bls
Posts: 454
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: Finding and moving files based on year

Fri Mar 27, 2020 12:00 am

If you want to move files from a random year (not necessarily just last year), you need something a bit more complex. Here's a recursive script that takes a 4-digit year as input and will identify all files created in that year. You'll have to modify it to do whatever you want it to do with the found files.

Code: Select all

#!/bin/bash

if [ "$1" == "process" ]
then
    if [ -f $2 -a $2 -ot $3 ]   # Is file in $2 a regular file and older than the "latest" file
    then
        # Modify script here to do something with file in symbol $2
        # e.g., rm -f $2
        echo "Would do something with file $2 "
    fi
else
    if [ "$1" == "help" -o "$1" == "" ]
    then
        echo ""
        echo "Specify the year as a 4-digit number (e.g., $0 2018)"
        echo "This script will do something with each file created in that year"
        echo ""
        exit
    fi
    ef=$(mktemp "$(whoami)XXX" --suffix ".earliest")
    lf=$(mktemp "$(whoami)XXX" --suffix ".latest")
    [ -f $ef ] && rm -f $ef
    [ -f $lf ] && rm -f $lf
    touch -t "$1"01010000 $ef
    touch -t "$1"12311159 $lf
    find . -newer $ef -exec $0 process "{}" $lf \;
    rm -f $ef
    rm -f $lf
fi

HTH

AustinA2
Posts: 12
Joined: Mon Mar 04, 2019 7:12 pm

Re: Finding and moving files based on year

Fri Mar 27, 2020 2:56 pm

DougieLawson wrote:
Thu Mar 26, 2020 11:03 pm
find /path/to/files* -mtime +365 -exec somecommandhere {}

For 2020 you may need +366 for the mtime value.
Thank you!

AustinA2
Posts: 12
Joined: Mon Mar 04, 2019 7:12 pm

Re: Finding and moving files based on year

Fri Mar 27, 2020 2:57 pm

bls wrote:
Fri Mar 27, 2020 12:00 am
If you want to move files from a random year (not necessarily just last year), you need something a bit more complex. Here's a recursive script that takes a 4-digit year as input and will identify all files created in that year. You'll have to modify it to do whatever you want it to do with the found files.

Code: Select all

#!/bin/bash

if [ "$1" == "process" ]
then
    if [ -f $2 -a $2 -ot $3 ]   # Is file in $2 a regular file and older than the "latest" file
    then
        # Modify script here to do something with file in symbol $2
        # e.g., rm -f $2
        echo "Would do something with file $2 "
    fi
else
    if [ "$1" == "help" -o "$1" == "" ]
    then
        echo ""
        echo "Specify the year as a 4-digit number (e.g., $0 2018)"
        echo "This script will do something with each file created in that year"
        echo ""
        exit
    fi
    ef=$(mktemp "$(whoami)XXX" --suffix ".earliest")
    lf=$(mktemp "$(whoami)XXX" --suffix ".latest")
    [ -f $ef ] && rm -f $ef
    [ -f $lf ] && rm -f $lf
    touch -t "$1"01010000 $ef
    touch -t "$1"12311159 $lf
    find . -newer $ef -exec $0 process "{}" $lf \;
    rm -f $ef
    rm -f $lf
fi

HTH


Thank you!

Return to “Beginners”