What happened to my .txt files after accidentally using 'mv *' in UNIX?

In summary, a user accidentally hit 'return' after typing 'mv *' and many .txt files disappeared from the current directory. This is because the * was expanded by the shell into a list of files and the mv command moved all the files to the last one. The solution to undo the mistake is to use backups or alias mv to prompt before overwriting anything. The lesson learned is to use cp and then del instead of mv.
  • #1
cepheid
Staff Emeritus
Science Advisor
Gold Member
5,199
38
Hi,

I was trying to type:

mv *.txt some_other_directory

but I accidentally hit 'return' after just typing:

mv *

Many of the .txt files in my current directory disappeared (but not all of them, strangely enough). Does anyone know what might have happened to them and how to undo the mistake?
 
Computer science news on Phys.org
  • #2
The * is expanded by the shell into a list of files
So the mv command sees

mv a.txt b.txt c.txt d.txt e.txt
It then moves all the files to the last one if the last match is a directory, otherwise it should fail (at least on linux)
 
Last edited:
  • #3
cepheid said:
how to undo the mistake?
Backups. Maybe you have copies of the files lying about in other places.
 
  • #4
the lesson? never mv. cp and then del
 
  • #5
Or alias mv so that it prompts before overwriting anything.
 

1. What is the "mv" command in UNIX?

The "mv" command in UNIX is used to move or rename files and directories.

2. What happens if I mistakenly use the "mv" command instead of "cp"?

If you mistakenly use the "mv" command instead of "cp", the file or directory will be moved or renamed instead of being copied. This means that the original file or directory will no longer exist in its original location.

3. Can I undo a "mv" command mistake?

No, unfortunately you cannot undo a "mv" command mistake. Once a file or directory has been moved or renamed, it cannot be reversed.

4. How can I avoid making a mistake with the "mv" command?

To avoid making a mistake with the "mv" command, it is important to double check the command and its arguments before executing it. You can also use the "mv -i" command to prompt for confirmation before overwriting any existing files.

5. Is there a way to recover a file or directory that was mistakenly moved using the "mv" command?

If you have a backup of the file or directory, you can recover it from there. Otherwise, it is not possible to recover a file or directory that was mistakenly moved using the "mv" command.

Similar threads

  • Computing and Technology
Replies
9
Views
2K
  • Computing and Technology
2
Replies
35
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • STEM Academic Advising
Replies
9
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
Replies
9
Views
1K
  • Science Fiction and Fantasy Media
Replies
10
Views
138
  • Computing and Technology
Replies
6
Views
7K
Back
Top