How can I move all files except one? I am looking for something like:
'mv ~/Linux/Old/!Tux.png ~/Linux/New/'
where I move old stuff to new stuff -folder except a Tux.png. !-sign represents a negation. Is there some tool for the job?
How can I move all files except one? I am looking for something like:
'mv ~/Linux/Old/!Tux.png ~/Linux/New/'
where I move old stuff to new stuff -folder except a Tux.png. !-sign represents a negation. Is there some tool for the job?
How about you move everything in the folder and then just move Tux.png back?
I can't think of any shell syntax to say "everything except ..." offhand.
A quick way would be to modify the tux filename so that your move command will not match.
For example:
mv Tux.png .Tux.png
mv * ~/somefolder
mv .Tux.png Tux.png
If you use bash and have the extglob
shell option set (which is usually the case):
mv ~/Linux/Old/!(Tux.png) ~/Linux/New/
mv `find Linux/Old '!' -type d | fgrep -v Tux.png` Linux/New
The find command lists all regular files and the fgrep command filters out any Tux.png. The backticks tell mv to move the resulting file list.
For bash, sth answer is correct. Here is the zsh (my shell of choice) syntax:
mv ~/Linux/Old/^Tux.png ~/Linux/New/
Requires EXTENDED_GLOB
shell option to be set.
I would go with the traditional find & xargs way:
find ~/Linux/Old -maxdepth 1 -mindepth 1 -not -name Tux.png -print0 |
xargs -0 mv -t ~/Linux/New
-maxdepth 1
makes it not search recursively. If you only care about files, you can say -type f
. -mindepth 1
makes it not include the ~/Linux/Old
path itself into the result. Works with any filenames, including with those that contain embedded newlines.
One comment notes that the mv -t
option is a probably GNU extension. For systems that don't have it
find ~/Linux/Old -maxdepth 1 -mindepth 1 -not -name Tux.png \
-exec mv '{}' ~/Linux/New \;
Back in the late 1980s, I had a DOS tool that would have done the trick: it was called "no.exe". The syntax was simple: "no Tux.png move * \somefolder". I realize that doesn't help much here, but just in case someone walks through with a time machine...
The following is not a 100% guaranteed method, and should not at all be attempted for scripting. But some times it is good enough for quick interactive shell usage. A file file glob like
[abc]*
(which will match all files with names starting with a, b or c) can be negated by inserting a "^" character first, i.e.
[^abc]*
I sometimes use this for not matching the "lost+found" directory, like for instance:
mv /mnt/usbdisk/[^l]* /home/user/stuff/.
Of course if there are other files starting with l I have to process those afterwards.
Put the following to your .bashrc
shopt -s extglob
It extends regexes.
You can then move all files except one by
mv !(fileOne) ~/path/newFolder