Linux命令---- mv移動或重命名文件/目錄

Linux 下查看幫助說明,可以用mv –help或man mv, 永遠是最快捷的方式

@ubuntu:~$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
  -f, --force                  do not prompt before overwriting
  -i, --interactive            prompt before overwrite
  -n, --no-clobber             do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -Z, --context                set SELinux security context of destination
                                 file to default type
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'

mv 的最後一個目標文件一定是目錄,除非用 -t 參數先指定了目錄。

1、移動一文件夾(目錄及目錄下的所有文件)到另一目錄
下面案例是爲了保存已下載代碼的.repo文件夾並移到另一個目錄中, 已後下載基於現有的來下載。

@ubuntu:~$ mv MSM8953/.repo/  work/MSM8909/

@ubuntu:~$ ls -a MSM8953/
.         build        device      libcore          platform_testing  tools
..        build.sh     docs        libnativehelper  prebuilts         vendor
abi       cts          external    Makefile         sdk
art       dalvik       frameworks  ndk              shortcut-fe
bionic    developers   hardware    packages         system
bootable  development  kernel      pdk              toolchain

@ubuntu:~/work$ cd MSM8909/
@ubuntu:~/work/MSM8909$ ls -a 
.  ..  .repo

再看一案例,移動已下載的MSM8953文件到work工作目錄

@ubuntu:~$ ls
bin      Documents  examples.desktop  Music     Public  Templates  work
Desktop  Downloads  MSM8953           Pictures  repo    Videos
@ubuntu:~$ mv MSM8953/ work/
@ubuntu:~$ ls
bin      Documents  examples.desktop  Pictures  repo       Videos
Desktop  Downloads  Music             Public    Templates  work
@ubuntu:~$ cd work
@ubuntu:~/work$ ls
MSM8909  MSM8953

2、移動一文件或多個文件到目錄

@ubuntu:~/work$ ls
directory  MSM8909  MSM8953  test
@ubuntu:~/work$ mv test directory/
@ubuntu:~/work$ ls
directory  MSM8909  MSM8953
@ubuntu:~/work$ ls directory/
test
@ubuntu:~/work$ ls
directory  MSM8909  MSM8953  test1  test2
@ubuntu:~/work$ mv test1 test2 directory/
@ubuntu:~/work$ ls directory/
test  test1  test2

3、重命名文件

@ubuntu:~/work/directory$ ls
test  test1  test2
@ubuntu:~/work/directory$ mv test test3
@ubuntu:~/work/directory$ ls
test1  test2  test3

目錄也可以重命名跟重命名文件一樣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章