How to move only files in Unix

41,694

Solution 1

You can try

find . -maxdepth 1 -type f -exec mv {} destination_path \;

Solution 2

@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 \;

Solution 3

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 * .* 
do
  test -f "$file" && mv "$file" "$HOME"/
done

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...

Share:
41,694

Related videos on Youtube

Scott - Слава Україні
Author by

Scott - Слава Україні

Updated on September 17, 2022

Comments

  • Scott - Слава Україні
    Scott - Слава Україні over 1 year

    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.

    • quack quixote
      quack quixote over 14 years
      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.
  • zod
    zod over 14 years
    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
  • John T
    John T over 14 years
    That's impossible with maxdepth as 1.
  • John T
    John T over 14 years
    fixed so it will not copy hidden files.
  • jeeva
    jeeva over 14 years
    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.
  • jeeva
    jeeva over 14 years
    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.
  • Kyle Brandt
    Kyle Brandt over 14 years
    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.
  • jeeva
    jeeva over 14 years
    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 .
  • John T
    John T over 14 years
    that's what it's intended to do. It grabs all files except hidden ones as well.
  • jeeva
    jeeva over 14 years
    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).
  • jeeva
    jeeva over 14 years
    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 \;
  • slhck
    slhck about 11 years
    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.
  • Sherwood Callaway
    Sherwood Callaway over 4 years
    DO NOT COPY-PASTE THIS INTO YOUR SHELL. The semicolon will cause it to execute immediately. :)
  • dstonek
    dstonek over 4 years
    @SherwoodCallaway The semicolon is to mark the end of -exec