0

I did something stupid on a remote linux server:

  1. my working directory was /some/directory
  2. I ran for dir in $(ls | grep "a_pattern"); do mv $d/* ./my_subdirectory; done
  3. Now /some/directory/my_subdirectory contains things like bin boot dev and home, which are definitely not supposed to be there

Feel free to point out how stupid #2 was. If I understand my error correctly, $d was empty (I meant to type $dir -- maybe even with that fix it would be a poorly-crafted command, but I should close this parenthesis), so basically the command I ran was mv /* ./my_subdirectory.

When I realized what I'd done I hit ctrl-c ctrl-c, so the command was interrupted before it finished.

My question has two parts: first, what should I do to fix this; and second, did my command actually move things out of /, or did it just copy them?

I did run mv, do I'd expect it moved things, and yet I don't see anything under ./my_subdirectory which is missing from /. Let me know if I'm being unclear here. For example, /some/directory/my_subdirectory/bin contains bash, cat, mount and mkdir, but those programs are still present under /bin, as if they were copied and not moved. Why is that? Can I safely delete everything under /some/directory/my_subdirectory, or would that be a terrible mistake?


Following tad's suggestion in the comments, here is some of the output from find ./my_subdirectory -type f:

./my_subdirectory/bin/umount
./my_subdirectory/bin/date
./my_subdirectory/bin/sort
./my_subdirectory/bin/red
./my_subdirectory/bin/dumpkeys
./my_subdirectory/bin/sleep
./my_subdirectory/bin/readlink
./my_subdirectory/bin/mv
...
./my_subdirectory/etc/security/pam_env.conf
./my_subdirectory/etc/security/chroot.conf
./my_subdirectory/etc/security/access.conf
./my_subdirectory/etc/security/namespace.init
./my_subdirectory/etc/security/group.conf
./my_subdirectory/etc/security/console.handlers

I checked and those files still exist under /etc and /bin. When I run diff ./my_subdirectory/etc /etc I get common subdirectories, some that are only in /etc, but nothing that is only in ./my_subdirectory/etc. So it seems that things were copied rather than moved. Is that to be expected given that I did not run the mv command as sudo?

  • You have almost certainly really moved stuff. It may not look like it because you stopped it before it was done, so the original directories are still around. For example, you probably have files in /bin and ./my_subdirectory/bin. The easiest way to assess the damage is probably find ./my_subdirectory -type f which will show you what was moved. – tad Jun 8 '16 at 15:57
  • @tad thank you for your comment, I'll edit my question with some output from find. I did not run my mv command as sudo, by the way, in case that's relevant. – Adrian Jun 8 '16 at 16:05
2

Assuming you did not run this command as root, I would assume the data was just copied. One quick way to tell would be to run diff for comparing the directories.

  • I did not run the mv command as root, that's correct. – Adrian Jun 8 '16 at 16:06
  • I just need a little more reassurance before I mark this as accepted: is it true that a non-sudo command could never move things out of /, only copy them? Can I safely delete everything in ./my_subdirectory? – Adrian Jun 8 '16 at 16:17
  • I completely agree with you on that point. Have you compared the directories yet? <code> [cent66-01 /]# ls /blah/ blah.txt [cent66-01 /]# ls ~/blah/ blah.txt [cent66-01 /]# [cent66-01 /]# diff /blah/ ~/blah/ [cent66-01 /]#'</code> – Kitterman Jun 8 '16 at 16:30
  • 1
    mv as a regular user can still move files if the permissions allow. Typically, you would not be able to mv anything, but there are no real guarantees. Are the two directories on different filesystems? mv behaves differently across filesystems. I would suggest confirming that the files are identical (via md5sum or sha1sum), then cp the my_subdirectory verion to ./backup_of_files_that_might_really_want_later/ cp will make sure if something goes wrong, you still have it. In the end, you are most likely OK, but better safe than broken... – tad Jun 8 '16 at 17:14
  • Agreed but likely you would not be able to move important system files as a regular user. With that being said, Tad is 100% correct - there are no guarantees. – Kitterman Jun 8 '16 at 17:30

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.