How to move only files in Unix

19

7

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.

shorty

Posted 2009-11-04T15:26:06.167

Reputation:

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 – 2009-11-04T16:34:08.650

Answers

30

You can try

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

Mereghost

Posted 2009-11-04T15:26:06.167

Reputation: 592

DO NOT COPY-PASTE THIS INTO YOUR SHELL. The semicolon will cause it to execute immediately. :) – Sherwood Callaway – 2019-09-08T23:32:32.070

@SherwoodCallaway The semicolon is to mark the end of -exec – dstonek – 2019-10-19T20:38:39.730

2Wow, 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 – 2009-11-06T04:10:12.850

4

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

chris

Posted 2009-11-04T15:26:06.167

Reputation: 296

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 – None – 2009-11-04T16:12:17.337

1This 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 – 2009-11-04T17:27:01.867

1And 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 – 2009-11-04T17:28:22.783

2shorty: 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 – 2009-11-04T20:59:45.587

3

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

teancum144

Posted 2009-11-04T15:26:06.167

Reputation: 31

0

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

Shawor

Posted 2009-11-04T15:26:06.167

Reputation: 1

-1

you can use find

find * -maxdepth 1 -type f -exec mv {} ~ \;

John T

Posted 2009-11-04T15:26:06.167

Reputation: 149 037

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 – None – 2009-11-04T15:44:59.250

That's impossible with maxdepth as 1. – John T – 2009-11-04T15:48:21.207

fixed so it will not copy hidden files. – John T – 2009-11-04T15:53:44.177

1The 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 – 2009-11-05T15:11:52.820

that's what it's intended to do. It grabs all files except hidden ones as well. – John T – 2009-11-05T15:21:45.483

1Find 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 – 2009-11-06T04:07:46.457

-2

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

Cm57201

Posted 2009-11-04T15:26:06.167

Reputation: 1

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 – 2013-03-08T20:19:21.533