up vote 2 down vote favorite
share [g+] share [fb]

HOw can i move only the files without the directories from a folder in linux to another folder
using the mv command?

i have tried mv * ~/ but it coped everything including the directories. Please help

link|improve this question
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
feedback

4 Answers

You can try

find . -maxdepth 1 -type f -exec mv {} destination_path \;
link|improve this answer
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
feedback

you can use find

find * -maxdepth 1 -type f -exec mv {} ~ \;
link|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
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
that's what it's intended to do. It grabs all files except hidden ones as well. – John T Nov 5 '09 at 15:21
show 1 more comment
feedback

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

link|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
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
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
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
feedback

http://www.basicconfig.com/linux/mv - hope it'll help

link|improve this answer
sorry its just the basic commands . it didnt help me – shorty Nov 4 '09 at 15:38
feedback

Your Answer

 
or
required, but never shown