Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move all files except one

Tags:

linux

bash

glob

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 Tux.png. !-sign represents a negation. Is there some tool for the job?

like image 569
Léo Léopold Hertz 준영 Avatar asked Mar 22 '09 02:03

Léo Léopold Hertz 준영


People also ask

How do I select all files except one?

Click the first file or folder you want to select. Hold down the Shift key, select the last file or folder, and then let go of the Shift key. Hold down the Ctrl key and click any other file(s) or folder(s) you would like to add to those already selected.

How do I move everything into a folder?

Using mv Command The mv command is used to move files and directories from one place to another. We can also use it to rename files and directories. This will move all the files from /path/subfolder to /path/ except for hidden files and directories.

How do I move multiple files from one file to another?

Windows PC: Left-click the first item, then hold down the Ctrl key while you left-click on each additional file. Let go of the Ctrl key, and you can left-click anywhere on one of the blue selected files to drag it to another location or right-click anywhere in the list to cut or copy.


2 Answers

If you use bash and have the extglob shell option set (which is usually the case):

mv ~/Linux/Old/!(Tux.png) ~/Linux/New/ 
like image 79
sth Avatar answered Sep 17 '22 18:09

sth


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 

Exceptions in relation to other commands

Note that, in copying directories, the forward-flash cannot be used in the name as noticed in the thread Why extglob except breaking except condition?:

cp -r !(Backups.backupdb) /home/masi/Documents/ 

so Backups.backupdb/ is wrong here before the negation and I would not use it neither in moving directories because of the risk of using wrongly then globs with other commands and possible other exceptions.

like image 21
Léo Léopold Hertz 준영 Avatar answered Sep 21 '22 18:09

Léo Léopold Hertz 준영