Moving all files Except one or few.
You may come to situation where you have to move file from current folder to some where else. But you also don't want to move all files. Here is how you can do it.
$ mv $(ls | grep -v exceptfile.any) ~/destinationfolder/
for Multiple files:
$ mv $(ls | grep -v 'exceptfile.any\|exceptfile2.any') ~/destinationfolder/
There could be case your destination folder is current directory. As you will encounter error:
mv: cannot move 'destinationfolder' to a subdirectory of itself, 'destinationfolder/destinationfolder'
For this you add the destinationfolder in grep parameter as well:
$ mv $(ls | grep -v 'exceptfile.any\|exceptfile2.any\|destinationfolder') ~/destinationfolder/
This will work. But incase re-run this command you like to hit output:
mv: missing destination file operand after 'developer_survey_2020'
Try 'mv --help' for more information.
The above out can be overlooked as its correct since there is no out put for command: $(ls | grep -v 'exceptfile.any\|exceptfile2.any\|destinationfolder') due to which mv complaints about missing destination as the "~/destinationfolder/" moves to first parameter for mv command.
Further you can also use find to move file from there current location to new with except as below.
$ find -maxdepth 1 -mindepth 1 -not -iname 'exceptfile1' -print0 |xargs -0 mv -t ~/destinationfolder
by add -iname search is case insensitive, you can add -type to restrict move to file or directory.
Thank... Hope this helps.
Comments
Post a Comment