Re: Learning Bash Questions: the MV command



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

--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list



Relevant Pages

  • Re: Learning Bash Questions: the MV command
    ... I need to move all the photos from a huge, ... Is this too complex for Bash? ... in this case it overwrites! ... and that should list of commands that you could try one at a time to see ...
    (Fedora)
  • Re: Learning Bash Questions: the MV command
    ... I need to move all the photos from a huge, complex tree to one big ... in this case it overwrites! ... and that should list of commands that you could try one at a time to see ...
    (Fedora)
  • Re: Learning Bash Questions: the MV command
    ... I need to move all the photos from a huge, complex tree to one big ... case of duplicate file names, to append something to the end of the ... in this case it overwrites! ...
    (Fedora)
  • Re: Syncing a file
    ... A overwrites B. ... change between syncs, you could use two "rsync -u" commands, one in each ...
    (comp.unix.questions)