I’ve just “mv”ed a 49GB directory to a bad file path, is it possible to restore the original state of the files

administrationdata-recoveryfilesmv

I have (well, I had) a directory:

/media/admin/my_data

It was approximately 49GB in size and had tens of thousands of files in it.
The directory is the mount point of an active LUKS partition.

I wanted to rename the directory to:

/media/admin/my_data_on_60GB_partition

I didn't realise at the time, but I issued the command from home directory so I ended up doing:

~% sudo mv /media/admin/my_data my_data_on_60GB_partition

So then the mv program started to move /media/admin/my_data and its contents to a new directory ~/my_data_on_60GB_partition.

I used Ctrl + C to cancel the command part way through, so now I have a whole bunch of files split across directories:

~/my_data_on_60GB_partition    <---  about 2GB worth files in here

and

/media/admin/my_data           <---- about 47GB of orig files in here    

The new directory ~/my_data_on_60GB_partition and some of its subdirectories are owned by root.
I'm assuming the mv program must have copied the files as root initially and then after the transfer chown'ed them back to my user account.

I have a somewhat old backup of the directory/partition.
My question is, is it possible to reliably restore the bunch of files that were moved?

That is, can I just run:

sudo mv ~/my_data_on_60GB_partition/*  /media/admin/my_data

or should I give up trying to recover, as the files are possibly corrupted and partially complete, etc.?

  • OS – Ubuntu 16.04
mv --version  
mv (GNU coreutils) 8.25

Best Answer

When moving files between filesystems, mv doesn't delete a file before it's finished copying it, and it processes files sequentially (I initially said it copies then deletes each file in turn, but that's not guaranteed — at least GNU mv copies then deletes each command-line argument in turn, and POSIX specifies this behaviour). So you should have at most one incomplete file in the target directory, and the original will still be in the source directory.

To move things back, add the -i flag so mv doesn't overwrite anything:

sudo mv -i ~/my_data_on_60GB_partition/* /media/admin/my_data/

(assuming you don't have any hidden files to restore from ~/my_data_on_60GB_partition/), or better yet (given that, as you discovered, you could have many files waiting to be deleted), add the -n flag so mv doesn't overwrite anything but doesn't ask you about it:

sudo mv -n ~/my_data_on_60GB_partition/* /media/admin/my_data/

You could also add the -v flag to see what's being done.

With any POSIX-compliant mv, the original directory structure should still be intact, so alternatively you could check that — and simply delete /media/admin/my_data... (In the general case though, I think the mv -n variant is the safe approach — it handles all forms of mv, including e.g. mv /media/admin/my_data/* my_data_on_60GB_partition/.)

You'll probably need to restore some permissions; you can do that en masse using chown and chmod, or restore them from backups using getfacl and setfacl (thanks to Sato Katsura for the reminder).