Rename a directory containing many files

9,231

Solution 1

This worked in my test with the paths you gave:

cd /var/www/photos
mv 2012 old
mv old/1007/1007 .
mv 1007 2012

What's happening is you are trying to move a directory over top of an existing directory - and it doesn't like doing that because there's stuff in the directory. By renaming 2012 first, you can move it without problems.

Solution 2

At least two ways I can think of:

1: First rename /var/www/photos/2012/1007 to /var/www/photos/2012/temppath, then mv /var/www/photos/2012/temppath/1007 -> /var/www/photos/2012/, then rmdir /var/www/photos/2012/temppath.

2: Use a simple script to move the contents:

for f in `find /var/www/photos/2012/1007/1007 -mindepth 1`; do mv $f /var/www/photos/2012/1007/1007/../; done

First method it probably a better answer to your question.

Share:
9,231
Nyxynyx
Author by

Nyxynyx

Hello :) These days its web development: PHP, Codeigniter, node.js, Javascript, jQuery, MySQL, mongoDB, HTML/CSS

Updated on September 18, 2022

Comments

  • Nyxynyx
    Nyxynyx over 1 year

    I made a mistake in a rsync and all the files are copied long with its full path. All the files that I copied are at

    /var/www/photos/2012/1007/1007
    

    Attempt 1

    Now I would like to fix the paths, by doing

     mv /var/www/photos/2012/1007/1007 /var/www/photos/2012
    

    This gives the error:

    mv: cannot move `/var/www/photos/2012/1007/1007' to `/var/www/photos/2012/1007': Directory not empty
    

    Attempt 2

     mv /var/www/photos/2012/1007/1007/* /var/www/photos/2012/1007
    

    I get the error:

    -bash: /bin/mv: Argument list too long
    

    Question: What will be the correct way to rename the folder that contains alot of files?


    The files were actually copied to /var/www/photos/2012/1007/home/photos/public_html/2012/1007 but somehow I got them to /var/www/photos/2012/1007/1007

    Rsync cmd:

    rsync -zavrR --rsh="ssh -c arcfour -l root -p 2200" www.mydomain.com:/home/photos/public_html/2012/1007 /var/www/photos/2012/1007

  • Nyxynyx
    Nyxynyx over 11 years
    Awesome!!!! Will need to mv old 2012 before mv 1007 2012. Thanks!
  • por
    por over 11 years
    Too late I was...