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.
You can try
find . -maxdepth 1 -type f -exec mv {} destination_path \;
@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 \;
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...
For many use cases with all your files having a ".extension" and your folders without an extension, you can try:
mv source_directory/*.* target_directory/
I am sure this is not a robust solution and am keen to update it to be one, but appears simple and quick for many use cases. Note this will move hidden files with an extension, and ignore those without extensions.
This may be considered bad practice/dangerous, in which case please comment! But how about
cp * ~; rm *
Because the default behaviours of both cp
and rm
are to ignore directories, it should have the desired effect.
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
mv `find ./sourcedir/* -type f` ./destdir
find
's -exec
option is the best way to avoid the problem.
mv *.ext ~/.