JM Calil
JM Calil

Reputation: 9

Extglob - Move all files not in a pattern but keep a specific one

Let's say I have a folder named /folder1/ with the following files:

file.txt
log-2018-01-22.log
log-2018-01-21.log
log-2018-01-20.log

I want to move to /folder2/ all files that aren't a .log with the current date, except file.txt. Since today is 2018-01-22 (in my timezone), I want to keep only log-2018-01-22.log and file.txt in /folder1.

The script that I'm using (seen below) doesn't keep file.txt in /folder1/, like I intend to, instead considering it as one of the files that aren't a .log with the current date and moving it to /folder2/.

shopt -s extglob
currentDate=$(date +"%Y-%m-%d")
mv /folder1/!(*$currentDate.log) /folder2/

Is there anything I can change in the !($d.log) part to make the command ignore file.txt?

Upvotes: 0

Views: 114

Answers (1)

Cyrus
Cyrus

Reputation: 88889

I suggest:

mv /folder1/!(log-$currentDate.log|file.txt) /folder2/

Upvotes: 2

Related Questions