1

I want to move the photos of my collection into an "/home/Bruno/Archiv/Bilder" directory, which is parent to the "/home/Bruno/Bilder" Directory.

I started of with:

find  -ctime +365 -exec echo mv '{}' ../Archiv/Bilder/'{}' \; | bash

But find includes the dot for current directory in the {}. So I get:

mv ./2013/01/19/DSC_0141.JPG ../Archiv/Bilder/./2013/01/19/DSC_0141.JPG
mv: das Verschieben von »./2013/01/23/IMG_0052.JPG“ nach »../Archiv/Bilder/./2013/01/23/IMG_0052.JPG“ ist nicht möglich: Datei oder Verzeichnis nicht gefunden

So I stripped the dot of using sed:

find  -ctime +577 -exec echo mv '{}' ../Archiv/Bilder/'{}' \;  | sed 's/\/Bilder\/\.\//\/Bilder\//' | bash

But I get the same error:

mv: das Verschieben von »./2013/01/10/IMG_0141.JPG“ nach »../Archiv/Bilder/2013/01/10/IMG_0141.JPG“ ist nicht möglich: Datei oder Verzeichnis nicht gefunden

So I wanted to swich to rsync to do the job:

find  -ctime +577 -exec echo rsync -n --remove-source-files '{}' ../Archiv/Bilder/'{}' \;  | sed 's/\/Bilder\/\.\//\/Bilder\//' | bash

And rsync fails, but it is a lot more verbose about it:

rsync: change_dir#3 "/home/Bruno/Bilder//../Archiv/Bilder/2013/01/10" failed: No such file or directory (2)
rsync error: errors selecting input/output files, dirs (code 3) at main.c(712) [Receiver=3.1.0]

What am I doing wrong here?

1

Your find command runs into the problem that the intermediate directories (in this case /home/Bruno/Archiv/Bilder/2013/01/23/) has not been created yet. That has nothing to do with the (harmless) '.' in your path.

You either have to first create the whole directory structure to the target or make a small script that you call instead of mv that first creates the target directory and then does the move.

mkdirmv:

#!/bin/sh
mkdir -p -- "$(dirname $2)"
mv -- "$1" "$2"
  • Thank you very much! I am anyways working in a script. So I can just call them after one another by using find twice. "dirname" was new to me :-) – Bruno Oct 5 '14 at 13:32
  • @Bruno Without looking at your script, that probably implies creating empty directories that might not (yet) get filled (that does not have to be a real problem). You might want to do a find . -depth -type d -exec rmdir {} \; it will complain about non-empty directories, but also remove any directories no longer filled. – Anthon Oct 5 '14 at 13:38
1

For future reference - you can use install to do this directly:

install -D ./2013/01/10/IMG_0141.JPG ../Archiv/Bilder/2013/01/10/IMG_0141.JPG

Note: you need to append the file path in the second argument for this to work. In other words:

Incorrect:

find . -ctime +365 -exec install -D '{}' ../Archiv/Bilder/ \;

Correct:

find . -ctime +365 -exec install -D '{}' '../Archiv/Bilder/{}' \;

According to man page of install:

-D create all leading components of DEST except the last, then copy SOURCE to DEST

  • I looked into the manpage of install. It does not seem to have an option to delete the source files. But the task was to move files into another directory. – Bruno Oct 5 '14 at 19:59
  • Ah yes! Sorry I can't add comments yet - so had to post an answer. My answer was only for suggesting how to create complete directory on destination. You will have to add call rm explicitly using another -exec option to find. – Srinidhi Oct 5 '14 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.