Re: Learning Bash Questions: the MV command

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tuesday 28 February 2006 21:18, Dotan Cohen wrote:
> On 2/28/06, Paul Howarth <paul@xxxxxxxxxxxx> wrote:
> > On Tue, 2006-02-28 at 22:01 +0200, Dotan Cohen wrote:
> > > On 2/28/06, Paul Howarth <paul@xxxxxxxxxxxx> wrote:
> > > > On Tue, 2006-02-28 at 09:26 +0200, Dotan Cohen wrote:
> > > > > I am trying to learn as I go with bash on a FC4 install. I need to
> > > > > do two things that are confounding me:
> > > > >
> > > > > 1) I need to move all the photos from a huge, complex tree to one
> > > > > big directory. Easy:
> > > > > $ find . -name "*.jpg" -exec mv '{}' /home/dotancohen/big_directory
> > > > > \; However, there are a few photos that have duplicate file names
> > > > > in different directories. In this case, it overwrites. That is bad!
> > > > > I tried adding the -i flag to prompt me, but it takes the next 'mv'
> > > > > as a response to the prompt, and the next 'mv' fails. Is there a
> > > > > way to have it not fail the next mv, yet not overwrite? Or better
> > > > > yet, in the case of duplicate file names, to append something to
> > > > > the end of the file name, so that it will not be duplicate? Of
> > > > > course, if _that_ file name is taken, it should append something
> > > > > else, etc. Is this too complex for Bash?
> > > >
> > > > find . -name "*.jpg" -exec mv --backup=numbered \
> > > >         '{}' /home/dotancohen/big_directory \;
> > > >
> > > > > 2) I will then be left with a huge tree with mostly empty
> > > > > directories. I need to remove the empty directories, but leave
> > > > > those in place that do contain files. Is there a way to check if a
> > > > > directory is populated before 'rm'ing it? If the directory contains
> > > > > another directory that _is_ empty, then of course they should both
> > > > > be deleted. Er, is this possible?
> > > >
> > > > find . -depth -mindepth 1 -type d \
> > > >         -exec rmdir --ignore-fail-on-non-empty '{}' \;
> > > >
> > > > Paul.
> > >
> > > Thanks, Paul. Although "man mv" describes ---backup=numbered as doing
> > > what I had described, in this case it overwrites! I just tried it with
> > > the code that you provided (in case I was doing something syntaxly
> > > wrong), however, it did overwrite!
> > >
> > > For some reason, instead of giving numbered backups, it overwrites. Is
> > > this a bug?
> >
> > There's certainly something strange going on there, as it certainly
> > worked for me when I tested it before posting.
> >
> > As useful way of trying to debug find commands is to put "echo " in
> > front of the command you're trying to run, e.g.
> >
> > find . -name "*.jpg" -exec echo mv --backup=numbered \
> >         '{}' /home/dotancohen/big_directory \;
> >
> > and that should list of commands that you could try one at a time to see
> > what's going on.
> >
> > Paul.
>
> Thank you Paul. I appreciate your time in writing and testing the
> code. That is real dedication!
>
> I will play with the echo command that you suggest. Some googleing led
> me to beleive that I would be better off trying to learn perl than
> bash, if moving and parsing files is what I expect to be doing mostly.
> I'm no syadmin, just a home user.
>
> Thank you.
>
> Dotan Cohen

Dotan

I haven't tried to test this, as Paul did, (I'm on a production machine, so I 
don't experiment here) but I wonder if you've got the no-clobber setting OFF.

Its ages since I worked with bash, but if I remember correctly, if you switch 
off the no-clobber flag, then mv doesn't bother with backups, it just 
over-writes.

Someone correct me if I'm wrong please!

TD

-- 
Tony


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux