[Wylug-discuss] Copy files by date

linux at sh2515.plus.com linux at sh2515.plus.com
Sat Jan 28 10:19:19 UTC 2012


> The Perl one-liner I gave just outputs yesterday's
> date, so you can embed it in $(...) and pass it to -newermt just as with
> the date command.
and
> However ... I still think that for what you're doing you'd be much
> better off using rsync than trying to devise your own copying routine.

for a temporary solution the IT people will not allow any installation of
other software. If we persisted they would drag their heels until it would
be almost too late as our department would then move. It's the way they
work and interact with other IT services. I think they all want to be 1 IT
service rather than split into sections/departments. Our senior management
have backed the least hassle - for them - solution, for the time being
that is.

>   if [[ "$FILE" == *AA*.txt ]]

Brilliant information

>
> Though note that that pattern will match things like these, which you
> might've meant to categorize under ZZ rather than AA:
>
>   ZZ_AARDVARK.txt
>   AAAARGH/ZZ.txt
>

Noted, each file has a unique reference number, AA, BA etc is the first 2
letters before the reference number linking it to a section.

>
> Using mkdir -p solves both those problems, creating any directories that
> are needed (perhaps none).
>
> Also I'd recommend using cp -p, to copy across the ownership and
> timestamps
> on the files, otherwise they'll get timestamps of the time you make the
> copies.

noted and in use now.


> (Also if you're naming directories after dates I'd really recommend you
> use the %F (yyyy-mm-dd) format rather than putting the day first. It's
> much easier to deal with them and find things when they are sorted in
> date order.)

I see why this would be beneficial over time, however, we are moving
within a couple of months and the %d-%m-%y seems to be well received.


> That's going to get very tedious if you have to copy and paste it 26 *
> 26 times.
>

It's only 7 variations, so would I put something like:

if
else
  if
  else
     if
     else
        if
        else
        fi
     fi
   fi
fi

Can't find multiples of if else if on the web to check if fi is needed at
the end of every else.


> If what you actually want is to get the first two letters of the
> filename then I'd try something along these lines:
>
>   basename="${FILE##*/}"
>   subdir=~/test/"${basename:0:2}/$today"
>   mkdir -p $dir
>   cp -p "$FILE" $dir
>
> The ## removes everything up to the final / in the full filename, to set
> just the basename. Then the :0:2 gets just the first two characters of
> that.
>
> In bash(1) search for "##" and ":length" for details.

I think this is probably the way I will want to go, the original one that
is currently working is the date example you gave in your first response. 
I will be testing these out next week.

>
> If you want to copy each file to a different subdirectory then yes each
> file is going to need its own cp command; having xargs group them all
> together into a single cp will only allow for a single destination
> directory.
>
> You can use xargs -n 1, to force xargs to issue a separate command for
> each file. Obviously that doesn't give any performance advantage of
> reducing the number of cp commands run, but if you'd prefer to use xargs
> for some other reason (perhaps to be able to use -0) then it's possible.
>
After your explanation I think you are correct in saying xargs wouldn't be
of any benefit to me in the above examples.

I am absolutely astounded by the information and help you have given, I am
truly grateful for this. I just hope you are not charging me by the word.
Hope I can return the help one day.

Scott




More information about the Wylug-discuss mailing list