0

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?

JM Calil
  • 9
  • 5

1 Answers1

2

I suggest:

mv /folder1/!(log-$currentDate.log|file.txt) /folder2/
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • you wrote in your post: want to move to /folder2/ all files that aren't a .log with the current date, **except file.txt** – Allan Jan 22 '18 at 05:21
  • @JMCalil: Did you consider that I've inserted `log-`? – Cyrus Jan 22 '18 at 05:22
  • This worked for me: `mkdir testdir; cd testdir; touch file.txt log-2018-01-22.log log-2018-01-21.log log-2018-01-20.log; shopt -s extglob; echo !(log-$currentDate.log|file.txt)`. Output: `log-2018-01-20.log log-2018-01-21.log` – Cyrus Jan 22 '18 at 06:32