I have this situation where I want to exclude a directory while using the mv
command. I saw this example where the syntax would be !(exclude_dir)
, but when I set up a scenario to test it I get results that I don't fully understand.
Now I have created three folders: f1
,f2
and f3
. Now I use the command in this way:
mv -t f3/ !(f1) f2
This produces this error:
mv: cannot move 'f3' to a subdirectory of itself, 'f3/f3'
mv: cannot stat 'f2': No such file or directory
Now funny thing is the structure of the folder is now:
.
├── f1
└── f3
└── f2
3 directories, 0 files
It does what I want but why the error messages. Obviously I am not using that command correctly.
This doesn't have anything to do with
mv
, but is abash
feature, citingman bash
:!(f1)
matchesf2 f3
in your example, so effectively you're doing:Then, it successfully moves
f2
tof3
, raises the first error trying to movef3
to itself, and raises the second error when trying to movef2
tof3
again because it was already moved and is no longer in the current directory.To achieve your goal you should rather do:
This expression matches everything except
f1
andf3
.This also works with
*
,?
and[…]
:!(f1)
is an extended glob expression, so (provided theextglob
shell option is set) it expands to a list of (non)-matching files. In other words, if your directory originally containedf1
,f2
,f3
thenexpands as
The first error should be obvious; the second is because it attempts to move
f2
twice - and fails the second time.As an alternative you can also use
rsync
:rsync -arv --exclude=.ccache --exclude=build /home/ben /media/ben/thumbdrive/
https://askubuntu.com/a/320459/585364
If you're looking for a way to backup current files, it would be safer to split the move command into copy & remove. For example: