How can I move only the plain files (not the directories) from one folder in Linux to another folder using the mv command?

I have tried mv * ~/, but it copied everything including the directories.

  • your comments mention an additional restriction: the command shouldn't move hidden files from the current working directory. if this is correct, could you please edit your question to mention this? saying "move only files" makes us think "move all the files", which includes hidden files. – quack quixote Nov 4 '09 at 16:34

You can try

find . -maxdepth 1 -type f -exec mv {} destination_path \;
| improve this answer | |
  • 2
    Wow, someone who knows how to use find. For completeness you should use -name [^.]* in the find, ie --- find . -maxdepth 1 -type f -name [^.]* -exec mv {} path \; – chris Nov 6 '09 at 4:10
  • DO NOT COPY-PASTE THIS INTO YOUR SHELL. The semicolon will cause it to execute immediately. :) – Sherwood Callaway Sep 8 '19 at 23:32
  • @SherwoodCallaway The semicolon is to mark the end of -exec – dstonek Oct 19 '19 at 20:38

I'm a "use a hammer for everything" kinda guy so I use bourne shell programs for stuff others use external programs for...

for file in * .* 
  test -f "$file" && mv "$file" "$HOME"/

Some people like to get things done in as little typing as possible but I'm a pretty quick typist and I've got stuff like this built into my brain so it's not too much of a pain to do this instead of looking up the exact arguments to find and exec and all that.

YMMV, though...

| improve this answer | |
  • thanks for that i will try it. but i still need the other answer because its for a project i am doing in linux course. thanks alot – shorty Nov 4 '09 at 16:12
  • 1
    This is an answer to your question. mv has no understanding of files or directories or fifos or other special files. mv only understands directory entries and inodes, and as far as it is concerned, those are all the same regardless of file type. To allow mv to only move a specific type of file, you need something like test or find that can actually look at the inode to see what the file is. In other words, you're being asked a trick question if you are being asked to do it only with mv. – chris Nov 4 '09 at 17:27
  • 1
    And I say that mv understands inodes only because it is able to move a file from one filesystem to another, which is technically a cp then an rm, not an mv (which is really just a ln then rm). Look hardlinks and directory entries. – chris Nov 4 '09 at 17:28
  • 2
    shorty: This is a good answer, you have to using something besides the mv command (at least the shell). This is about a basic as it gets without programming. – Kyle Brandt Nov 4 '09 at 20:59

@Mereghost is very close. Here's what I get to move all files (including hidden files), but not directories:

find . -maxdepth 1 -type f -name '*' -exec mv -n {} /destination_path \;

The . after find assumes you current directory is the source of the files you want to move. If not, the command can be revised, as follows:

find /source_path -maxdepth 1 -type f -name '*' -exec mv -n {} /dest_path \;

If you want to move only regular files and not hidden files:

find . -maxdepth 1 -type f -name '[!.]*' -exec mv -n {} /dest_path \;

If you want to move only hidden files and not regular files:

find . -maxdepth 1 -type f -name '.*' -exec mv -n {} /dest_path \;
| improve this answer | |

If you have no folders with name with dot in it you can move all of them simply:

mv . ~/destinationfolder

(My assumption is all your files are something.something (in my case all are usually))

BUT thanks to other answers - very helpfull. THX

| improve this answer | |

you can use find

find * -maxdepth 1 -type f -exec mv {} ~ \;
| improve this answer | |
  • yeah well that worked but not 100% it copied all the files from directories and sub directories to ~/ location. i need just something very basic that copies the files without the hidden files from the dir i am in to another dir. thats it. thanks for the advice anyway – shorty Nov 4 '09 at 15:44
  • That's impossible with maxdepth as 1. – John T Nov 4 '09 at 15:48
  • fixed so it will not copy hidden files. – John T Nov 4 '09 at 15:53
  • 1
    The syntax on find is find then a directory then a description of operations and things to match. One would typically not ever use a wildcard such as find * without protecting it from globbing (ie find /path/to/directory -name goo* -type f . – chris Nov 5 '09 at 15:11
  • 1
    Find only works properly if you hand it a single path to examine. In your example, for instance, if there are any files that begin with a - find will explode unexpectedly. You're always much better off having only one program do the directory entry reading, and in your example, you're kinda using two different programs (find and the shell). – chris Nov 6 '09 at 4:07

mv `find ./sourcedir/* -type f` ./destdir

| improve this answer | |
  • 2
    This will fail for any files that contain whitespace in their name. The output of commands that print filenames should never be parsed unless it can be guaranteed it is properly delimited. See Why you shouldn't parse the output of ls(1) for the main idea. Using find's -exec option is the best way to avoid the problem. – slhck Mar 8 '13 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy