0

I am trying to find particular directory in one path and move the directory in other path using find.

Source directory looks like below,

/Dir1/country1/month1/date1,date2,date3
/Dir1/country2/month1/date1,date2,date3
...
.
.
/Dir1/country10/month1/date1,date2,date3

I am trying to find date1 directory and move to destination

/Backup/source1/Dir1/

I am using below command

[root@mytest Dir1]# find -name "date1" -exec mv {} /Backup/source1/Dir1/{} \;

and I am getting below error.

mv: failed to preserve ownership for `/Backup/source1/Dir1/./country1/month1/date1/date1/date1_1.txt': Operation not permitted

Not sure how to remove duplication of date1 Directory in destination and "./" is also creating issue.

Best Wishes, KJ

1
  • 1
    The error you are getting implies either not running with the correct permissions or the target filesystem is not capable of handling the permissions. Why not rejig your find command to do a cp and rm.
    – davidgo
    Dec 8, 2013 at 7:38

2 Answers 2

0

The Linux Man page states:

mv - move (rename) files

Thus you cannot use mv to move a directory. You may however, first copy it then erase the old copy.This command

   SRC_DIR=$(find . -type d -name date1 -print); echo $SRC_DIR

will store the relative path to the directory you are interested in, in a convenient variable. Now we can make the directory in the new position,

   mkdir /Backup/source1/$SRC_DIR

provided you have the authority to do so: in fact, the error message above is about your having insufficient proivileges to create a new file (it should have been a folder, but became a file because of the error above). Are you sure the new /Backup/dir1 belongs to you? You should check that, and fix it.

Now you can recursively cp all content to the new directory:

   cp -R $SRC_DIR/* /Backup/dir1/$SRC_DIR
   cp -R $SRC_DIR/.[a-Z]* /Backup/dir1/$SRC_DIR

and lastly, you caan erase the old copy, if you have to:

   cd $SRC_DIR
   rm -fR *
   rm -fR .[a-Z]* 

and now the directory is empty and can be erased.

2
  • thanks for the inputs. I am running the command as root user and destination directory is own by root only. I have many directory which i need to move and of huge size, so was looking a process to automate
    – KumarJohn
    Dec 8, 2013 at 8:33
  • @KJT Do the commands I suggested work? Dec 8, 2013 at 8:37
0

Rather than using move which is a Blunt tool, check out rsync which can shift directory trees in archive mode which preserves permissions and both soft & hard symbolic links.

Also if you type updatedb as root it will update the locate database of files that are in the various path trees of installed packages. Binaries, man pages and Source trees. Searching with whereis or locate is much faster than doing the actual disk search each time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .