0

I understand if I mv foo.file boo.file, I just rename foo.file to boo.file, and if I mv dir1/foo.file dir2/boo.file, I delete dir1/foo.file and make a copy of that to dir2 and rename it as boo.file. But I was looking at the man page here:

NAME
     mv -- move files

SYNOPSIS
     mv [-f | -i | -n] [-v] source target
     mv [-f | -i | -n] [-v] source ... directory

DESCRIPTION
     In its first form, the mv utility renames the file named by the source
     operand to the destination path named by the target operand.  This form
     is assumed when the last operand does not name an already existing direc-
     tory.

     In its second form, mv moves each file named by a source operand to a
     destination file in the existing directory named by the directory oper-
     and.  The destination path for each operand is the pathname produced by
     the concatenation of the last operand, a slash, and the final pathname
     component of the named file.

I have 2 questions: 1. I know that "..." means repeatable argument, so what if I do mv dir1/foo.file dir2/boo.file dir3/woo.file, what happens? 2. I don't quite get the second paragraph about the second form about "The destination path for each operand is the pathname produced by the concatenation of the last operand, a slash, and the final pathname component of the named file.", what does "for each operand" and "the concatenation of the last operand, a slash, and the final pathname component of the named file" means?

Sorry I know they are silly questions

0

4 Answers 4

2

mvhas two forms:

first:

mv [-f | -i | -n] [-v] source target

second:

mv [-f | -i | -n] [-v] source ... directory

To answer your first question, in the second form, you have "..." that means that you can have more than one source , but only one destination that specifically is a directory so you can do this:

mv 1.txt 2.txt 3.txt new

and it will move the three files to a previously created directory called new.


To answer your second question, it simply says that the new path for moved files will be the directory plus the original name of the file. The last operand is the destination directory and the final pathnameis the name of the files,

0

If the last argument passed to mv is an existing directory, it moves the earlier listed file or files into that directory. It has to be an existing directory, since otherwise trying to mv a single file would be unclear whether to change the name, or move into a new directory with the same file name. The language in the man page is somewhat more formal and specific than what I say here (and more of a direct explanation of what the code does), in an effort to be exactly precise for any questions about how mv might work for some odd case with unusual file or directory names.

And no, it isn't a silly question.

1
  • Ah! Thanks for clearing that up. I was really frustrated reading man page
    – RexYuan
    Commented Feb 18, 2015 at 16:20
0
  1. mv tries to move the files dir1/foo.file AND dir2/boo.file to directory dir3/woo.file. This fails since dir3/woo.file is not a directory. (you can easily try it out)

  2. "for each operand" refers to the variable list of arguments (given by ...). Since the second operation form is only valid if the last one is a directory, "the concatenation of the last operand, a slash, and the final pathname component of the named file" means mv just concatenates (joins) the targetDirectory(which is the last argument)+"/"+the filename of the corresponding argument (so, without the folder structure link). I.e., "the final pathname component of the named file" of /a/b/c is c and of b/e/r is r.

0

Briefly:
The command mv moves and/or renames files and/or directory. It doesn't copy and delete them unless the target directory is on a different filesystem. (Not all implementations of mv may support that.)

  1. If file.a and file.b are files or directories and c doesn't exists or it is not a directory or a link to a directory, the command

    mv file.a file.b c
    

    will give you an error and nothing more :)

  2. It means simply that the command

    mv dir1/file.a dir2/file.b C
    

    is the same that to write:

    mv dir1/file.a  C/file.a
    mv dir2/file.b  C/file.b
    

With more words:

The command mv moves (renames) files and, at least in my system, has three forms:

mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

  1. When you want to move and/or rename a single file

    • mv file.a file.b or mv dir.a dir.b #Simple Renaming.
      It renames the file.a with a new name file.b.
      Note: It is the same file. If you perform a ls -i file.a before and ls -i file.b you will notice the same number of inode. It means that normally you are not copying the file and deleting the old one.
    • mv OldPath/file.a NewPath/file.a # Simple moving
      Again the file before and after has the same inode. The same if you are moving/renaming a directory.
    • mv OldPath/file.a NewPath/file.b # Moving and renaming
      It moves and rename the file from the OldPath to NewPath. Again the file before and after has the same inode. The same if you are moving/renaming a directory.
  2. When you want to move many files to a destination directory. E.g.

    mv dir1/file1 dir2/file2 ... /long/path/to/dir     
    

    is like to write:

    mv dir1/file1 /long/path/to/dir/file1
    mv dir2/file2 /long/path/to/dir/file2
    
  3. You specify -t and the destination directory before the list of the file to be moved.
    E.g. mv -t /long/path/to/dir file1 file2 ...

    -t, --target-directory=DIRECTORY
    move all SOURCE arguments into DIRECTORY


BTW No question is a silly question only the answers can be silly :-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .