5

Possible Duplicate:
Can I keep . and .. out of .* expansion?

I know about

mv * ~/whatever
mv .* ~/whatever

but that tries to move . and .., which just caused me a bunch of pain. Is there a way to mv all the files, including the .* files, but exclude the current directory and it's parent?

sprugman
  • 243

5 Answers5

8

From the Bash manual, shopt section:

dotglob     If set, bash includes filenames beginning with a `.' in the results
            of pathname expansion.

shopt can be used to set certain "shell options" as explained in the manual. See man bash.

Thus

shopt -s dotglob
mv * ~/whatever

does what you want. Test it with e.g.

shopt -s dotglob
ls -d *

to see which files are matched.

Since it is the de facto Bash way as per the manual, no hidden surprises should turn up regarding files with tricky names. If it does, it is by definition a bug in Bash.

7

Just tell bash there is the second character and it is not a dot. Will not move files named like ..file, though.

mv .[^.]* somewhere/
choroba
  • 20,299
0

Try this:

mv .[a-zA-Z0-9]* ~/whatever

Unless you have dot files that start with ._ or other non-alphanumeric characters, it should get them all.

Fran
  • 5,511
0

If you don't want to move directories, this will move all files to ./foo/:

$ find . -type f  | while read n; do mv "$n" foo/; done

If you do need to do directories also, try:

$ find . | grep -vP '^\.$' | while read n; do mv "$n" foo/; done
terdon
  • 54,564
0

Use find which excludes . and ..:

find . -name ".*" -mindepth 1 -maxdepth 1

and check the result. To move the files

find . -name ".*" -mindepth 1 -maxdepth 1 -exec mv {} ~/whatever \;
Matteo
  • 8,097
  • 3
  • 47
  • 58