A
A
Alexander Kryuchkov2015-04-10 17:48:46
Perl
Alexander Kryuchkov, 2015-04-10 17:48:46

Bash rename files. How to add negation to regular expression?

Task:
there is a folder, it contains a large number of files like 9878970.txt and 8798_8798.txt
New files are constantly added to this folder, matches often come across. It is necessary that all versions of files, even identical ones, be saved.
It was decided to write a script that will check the folder once every 1 minute, rename the files according to the mask, adding an underscore and the date to the beginning of the file.
The script is written, but it also renames already renamed files.
How to add to the regular expression so that it does NOT rename files that start with _ ?
To do this, I even created the DATA1 variable.

[email protected]:/home/slowpoke/scripts/RRR# rm *.txt | touch 555.txt 666.txt
[email protected]:/home/slowpoke/scripts/RRR#
[email protected]:/home/slowpoke/scripts/RRR# ls
555.txt 666.txt rrr
[email protected]:/home/slowpoke/ scripts/RRR#

#!/bin/bash
cd /home/slowpoke/scripts/RRR/

DATA=`date +%Y-%d-%m`
DATA1=_$DATA

A='s/(^.*)\.txt$/'
B='_$1\.txt/'

STRING=$A$DATA1$B
rename -v $STRING *.txt

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2015-04-10
@kruchkov-alexandr

Instead of filtering out the excess, do not take the excess at all:
rename -v $STRING [0-9]*.txt

K
Kirill Penzin, 2015-04-10
@kir_vesp

It would be more correct to put the question this way: "How to exclude files that have the given substring in the name." And here everything is already simple. Specify that the characters of the given substring DO NOT occur in your string. You can use a construction like: "[^some_characters]".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question