iwvwi iwvwi - 1 month ago 16
Bash Question

why command mv remove all the file?

I want to copy git.sh to ever file below /root
Then I use this

mv git.sh /root/

Is a mirecle that all the file below /root has been delete.

Could you explain what happen after the command?
mv git.sh /root/*
Could you give me the right command to copy a file to ever file below /root/?

enter image description here


Command mv moves files. When file in destination exists, it will be replaced. The right command to copy file is cp. It's used same way as mv.

Command mv git.sh /root/* will substitute wildcard char * with all names the directory contains. Then there are a few cases:

  1. /root contains multiple files or directories: command will fail
  2. /root contains one file: git.sh will be moved and replace the file in directory /root
  3. /root is empty: git.sh will be moved as git.sh to /root

mv will remove source but cp keeps it.

It's better to have destination path with filename, for example mv git.sh /root/git.sh. When moving multiple files use destination path just to directory without filename or wildcard chars, for example mv dir/* /root/. When you want to move directory with subdirectories add -R argument.

Use man mv or man cp for more information.