I accidentally forgot to specify destination before hitting the Return key. Where does mv ./* without specifying destination move the files and directories under current directory to?
5 Answers
If the last argument was a directory, you just moved all of the files and directories in your current working directory (except those whose names begin with dots) into that directory. If there were two files, the first file may have overwritten the second file.
Here are some demonstrations:
More than two files and the last argument is a file
$ mkdir d1 d2 d3 $ touch a b c e $ mv * mv: target 'e' is not a directory More than two files and the last argument is a directory
$ mkdir d1 d2 d3 $ touch a b c $ mv -v * 'a' -> 'd3/a' 'b' -> 'd3/b' 'c' -> 'd3/c' 'd1' -> 'd3/d1' 'd2' -> 'd3/d2' Two files
$ touch a b $ mv -v * 'a' -> 'b' Further explanation
The shell expands the glob (*) into arguments for mv. The glob is usually expanded in alphabetical order. mv always sees a list of files and directories. It never sees the glob itself.
The command mv supports two types of moving. One is mv file ... directory. The other is mv old-file-name new-file-name (or mv old-file-name directory/new-file-name).
First I'll make a test base - 5 files and one folder:
touch file1 file2 file3 file4 file5 mkdir folder Next I'll run a test command. The -v option specifies that I want every command the shell executes to be printed to stderr. The -x option specifies that I want the same printed to stderr - but I want it done after the command is evaluated but before the shell runs it.
sh -cxv 'echo mv *' OUTPUT
echo mv * + echo mv file1 file2 file3 file4 file5 folder mv file1 file2 file3 file4 file5 folder So you see that the command I feed the shell is echo mv * and the command the shell executes after * is expanded is echo mv followed by all of those files and the folder.
By default the shell will expand globs like:
sh -cxv 'echo file[1-5]' OUTPUT
echo file[1-5] + echo file1 file2 file3 file4 file5 file1 file2 file3 file4 file5 This is a result of the set [+-]f glob function:
sh -cxvf 'echo file[1-5]' OUTPUT
echo file[1-5] + echo 'file[1-5]' file[1-5] So when you run a command in a shell configured with default options like mv * the shell expands into the * word an argument list of all files in the current directory sorted according to locale. It does the syscall exec(ve) for mv (essentially) with this argument list appended. So mv gets all of the arguments as the shell globs them and sorts them. Besides doing strace to see these effects, you can use the debug out again like:
sh -s -- mv * <<\SCRIPT sed -n l /proc/$$/cmdline echo "$@" SCRIPT OUTPUT
sh\000-s\000--\000mv\000file1\000file2\000file3\000file4\000file5\000folder\ \000$ mv file1 file2 file3 file4 file5 folder And portably:
( PS4= IFS=/; set -x mv *; : "/$*/" ) 2>&1 OUTPUT
: /mv/file1/file2/file3/file4/file5/folder/ Basically, the shell executes mv with the contents of the directory (if it is not empty and not including files/folders with names beginning with .) as its argument list. mv is POSIX specified to interpret its final argument as a directory if it is invoked with more than two arguments - in the same way ln is (because, in fact, they're incredibly similar tools in underlying function).
Enough echoes though:
sh -cxv 'mv *' ; ls OUTPUT
mv * + mv file1 file2 file3 file4 file5 folder folder/ All of the files were moved into the final argument - because it is a folder. Now what if it is not a folder?
sh -cxv 'cd *; mv *'; ls . * OUTPUT
cd *; mv * + cd folder + mv file1 file2 file3 file4 file5 mv: target ‘file5’ is not a directory .: folder/ folder: file1 file2 file3 file4 file5 This is how POSIX specifies mv should behave in that case:
mv [-if] source_file target_file mv [-if] source_file... target_dir In the first synopsis form, the
mvutility shall move the file named by the source_file operand to the destination specified by the target_file. This first synopsis form is assumed when the final operand does not name an existing directory and is not a symbolic link referring to an existing directory. In this case, if source_file names a non-directory file and target_file ends with a trailing/slashcharacter,mvshall treat this as an error and no source_file operands will be processed.In the second synopsis form,
mvshall move each file named by a source_file operand to a destination file in the existing directory named by the target_dir operand, or referenced if target_dir is a symbolic link referring to an existing directory. The destination path for each source_file shall be the concatenation of the target directory, a single/slashcharacter if the target did not end in a/slash, and the last pathname component of the source_file. This second form is assumed when the final operand names an existing directory.
So if * expands to:
two files
- You should have only one file, which is the first
renamedto the second after the second isunlinked.
- You should have only one file, which is the first
one or more files followed last by a directory or a link to one
- You should have only one directory or a link to one, which is where all its parent's previous contents have just been moved.
anything else
- You should have an error message and a satisfying sigh of relief.
- 1yes, using the old
shthingy, I forgot about debugging with that, but regarding my original question, it means that the problem is the shell notmv? That's nasty, it's simpler than I originally thought but it's a real trap .user2485710– user24857102014-08-22 03:38:29 +00:00Commented Aug 22, 2014 at 3:38 - 5@user2485710, the key point is that in Unix, the shell is responsible for expanding wildcards before passing the command line arguments to the command. In Windows, each command has to expand wildcards itself. This allows Unix shells to offer advanced wildcard facilities that work with any command.cjm– cjm2014-08-22 03:41:34 +00:00Commented Aug 22, 2014 at 3:41
- 2@mikeserv given the fact that those files were not that important I rushed myself into cleaning up and skip to the next step, but I can confirm things as they now appear in my edit of my first post/question.user2485710– user24857102014-08-22 04:20:05 +00:00Commented Aug 22, 2014 at 4:20
- 6@mikeserv tl;dr,
*is not a single argument? Right? :)Bernhard– Bernhard2014-08-22 06:09:50 +00:00Commented Aug 22, 2014 at 6:09 - 1@Bernhard - actually, that's the one case I didn't cover - because it might be.mikeserv– mikeserv2014-08-22 06:24:31 +00:00Commented Aug 22, 2014 at 6:24
First the shell expands ./* to all files in the current directory (except files starting with a dot).
- if there is no or only one file:
mvfails - if there are two file: the first one is moved to the second (which therefore get lost)
- if there are more than two files:
- if the last one is a directory: all files are moved into this directory
- otherwise
mvfails.
- 1Thanks. how to tell which subdirectory is " the last one"?Tim– Tim2013-07-23 21:58:00 +00:00Commented Jul 23, 2013 at 21:58
- 7Just call
echo ./*do see which order your shell uses (usually alphabetical).jofel– jofel2013-07-23 22:00:13 +00:00Commented Jul 23, 2013 at 22:00 - If it fails with one file why doesn't it say so?Noumenon– Noumenon2017-02-03 21:47:32 +00:00Commented Feb 3, 2017 at 21:47
- @Noumenon I do not understand your comment.
mvreturns an error message if it is called with only one argument.jofel– jofel2017-02-06 12:13:52 +00:00Commented Feb 6, 2017 at 12:13 - You are right. The same command from my history now gives
missing destination file operand after *filename*even though yesterday it didn't.Noumenon– Noumenon2017-02-06 12:19:12 +00:00Commented Feb 6, 2017 at 12:19
When you type mv ./*, your shell will expand ./* before executing mv.
A few things to note:
- If
./*is expanded into less than 2 arguments,mvwill, logically, produce an error. ./*will usually expand into every file (including directory) present in the current directory and not starting with a dot.- You can control what
./*expands into by reading the documentation of your shell (man 7 globis an entry point to the topic). Different shells will have different options.
What does
mv *do?
Here's a shorter answer:
The shell expands the wildcard * to a list of directory contents. Then the shell passes that full list to the command. The command never sees *.
The command mv file1 file2 ... filen directory will move file1 ... filen into directory.
Example
Here I make a test directory containing three files
$ mkdir t $ cd t $ echo a>a; echo b>b; echo c>c $ ls a b c You can't move multiple files into a single file
$ mv * mv: target `c' is not a directory Let's add a subdirectory
$ mkdir d You can move multiple files into a subdiretory
$ mv * $ ls d $ ls d a b c - The command
mv file1 file2 ... filen directoryis very little likely to have anything at all to do with*.mikeserv– mikeserv2014-08-25 13:11:21 +00:00Commented Aug 25, 2014 at 13:11 - @Mike: My answer points out that the last stage of shell expansion effectively transforms the latter into the former. Not knowing this appears to be the root of the OP's confusion.RedGrittyBrick– RedGrittyBrick2014-08-25 14:11:08 +00:00Commented Aug 25, 2014 at 14:11
- yes, but my point is the shell would not expand
*intofile dirunless there is some locale in whichdfollowsf.mikeserv– mikeserv2014-08-25 14:50:30 +00:00Commented Aug 25, 2014 at 14:50 - @mikeserv: There is such a locale, my example is a cut & paste from Putty connecting to a CentOS 5.6 GNU/Linux system.RedGrittyBrick– RedGrittyBrick2014-08-25 15:17:14 +00:00Commented Aug 25, 2014 at 15:17
- which locale is that? your example is
a b c dwhich isn'tfile ... dir. I only commented at all because you don't mention the sort anywhere and say only that*becomesfile ... dirwhich doesn't happen.mikeserv– mikeserv2014-08-25 16:30:34 +00:00Commented Aug 25, 2014 at 16:30
mvaccepts 1 argument" It doesn't. It accepts all arguments the shell passed to it after expanding the*.