Linux rename file number

Rename Files with increment Numbers

I have files in /root/test/access.log.1 to 999 and /root/test/error.log.1 to 999 . I would like to rename access.log.1 to access.log.2 and move to /var/log/archives/ , and same way error.log.1 to error.log.2 . I tried something like below

#!/bin/bash NEWFILE=`ls -ltr |grep error |tail -2 | head -1 |awk ''` for i in `ls -ltr |grep error |tail -1`; do mv "$i" /var/log/archives/"$NEWFILE" done 

That script is way more complicated than what you’re asking, so please edit the question to clarify exactly what you need.

To clarify, this fits your description, but it’s trivial: mv /root/test/access.log.1 /var/log/archives/access.log.2; mv /root/test/error.log.1 /var/log/archives/error.log.2

3 Answers 3

for file in .log.; do echo "$file" "/path/to/dest/$$(($+1))"; done 
  • $ , this removes digits and keep alphabet only which will result error.log. or access.log. part.
  • $ , this removes alphabet and dots only (I write a-z. because of your files name patterns), which will result numeric part; and
  • $(($+1)) will increment one to the numeric part as produced by above.

This will rename files as below and move to /path/to/dest/ :

access.log.999 --> /path/to/dest/access.log.1000 access.log.998 --> /path/to/dest/access.log.999 . error.log.999 --> /path/to/dest/error.log.1000 error.log.998 --> /path/to/dest/error.log.999 . 

Note that replace echo for dry-run with mv to have rename on files.

file moving as like below access.log.52 —> /path/access.log.5253 access.log.53 —> /path/access.log.5354

We could run something along the lines of

#! /usr/bin/env bash # exit on error set -e # increase the numbers of the old archives (mv -i avoids accidental overwrite) for ((i=999; i >= 2; i--)); do for name in access error; do if [[ -e /var/log/archives/$name.log.$i ]]; then mv -i "/var/log/archives/$name.log.$i" "/var/log/archives/$name.log.$((i+1))" fi done done # move current files to archives for name in access error; do mv -i "/root/test/$name.log.1" "/var/log/archives/$name.log.2" done 

Источник

Читайте также:  Ufo linux alien invasion

Changing numbers in name of the file using terminal

I have a large number of files with names file00.txt, file01.txt etc in a folder. Is it possible to rename all where the numbers will change to (4000+200*number) in terminal? I want the name of the final files as file4000.txt, file4200.txt etc. Thanks!!

Please edit your question and tell us what operating system you are using so we can know what tools you have available. For example, there are many tools called rename and if we don’t know your OS, we won’t know which one you have.

Your question is essentially the same as How to rename files with sequential names to an another sequence using Terminal?, with slightly different arithmetic and without the subdirectory.

3 Answers 3

If you have zsh installed.

autoload zmv zmv -n '(file)()(.txt)' '$1$((4000+$2*200))$3' 

remove the -n if you’re happy with the result.

I would normally use rename which makes use of perlre

$ rename -v 's/^(file)(\d+)(\.txt)$/$1 . (4000+200*$2) . $3/e' *.txt file00.txt renamed as file4000.txt file01.txt renamed as file4200.txt 

or as Glenn Jackman suggests in the comments;

$ rename -v 's/(\d+)/4000 + 200*$1/e' file*.txt 

This uses the s/foo/bar/ syntax which substitutes foo with bar .

foo is ^(file)(\d+)(.txt) . So the expression will match any file starting with foo , containing 1 or more digits, followed by a .txt extension and nothing else. file is saved in capture group 1, the digits in capture group 2, and .txt in capture group 3.

Then we replace bar with our new filename: $1 . (4200 + $2) . $3 . We are adding 4200 to capture group 2 (the digits) and concatenating that with $1 and $3 .

Читайте также:  Linux change file owner recursively

We need the e modifier in s/foo/bar/e to ensure (4000 + 200*$2) and the concatenation is actually evaluated.

I added -v so it prints what is changed, but while testing, you’ll want to use -n so it doesn’t change anything until you are happy with what it will actually do.

The last argument to the rename command is *.txt . This is simply the list of files to evaluate. Since we do a check for the extension in perlre , we could just use a * here. Anything that doesn’t match ( foo ) will not be changed.

This answer uses file-rename from Debian’s package rename which uses update-alternatives to provide /usr/bin/rename . If you don’t have that package installed, you may have rename.ul from util-linux. rename.ul uses different expression system and will not work with what I’ve described here.

Источник

How to rename files in bash to increase number in name?

for 80 variations of the first two words (80 different species), i would like to rename all of these files such that the number is increased by 100 — for example:

Vibrio_cholerae_3900_nanopore_trim_reads.fasta 
Vibrio_cholerae_4000_nanopore_trim_reads.fasta 
Cyprinus_carpio_300_nanopore_trim_reads.fasta 
Cyprinus_carpio_400_nanopore_trim_reads.fasta 

Unfortunately I can’t work out how to get to rename them, i’ve had some luck with following the solutions on https://unix.stackexchange.com/questions/40523/rename-files-by-incrementing-a-number-within-the-filename But i can’t get it to work for the inside of the name, i’m running on Ubuntu 18.04 if that helps

Is there just one number embedded within the filename? Or might there be more? Is it always the 3rd part, or might it be elsewhere?

4 Answers 4

If you can get hold of the Perl-flavoured version of rename , that is simple like this:

Читайте также:  Which linux is mint based on

Sample Output

'Ciprianus_maximus_11_fred.fasta' would be renamed to 'Ciprianus_maximus_111_fred.fasta' 'Ciprianus_maximus_300_fred.fasta' would be renamed to 'Ciprianus_maximus_400_fred.fasta' 'Ciprianus_maximus_3900_fred.fasta' would be renamed to 'Ciprianus_maximus_4000_fred.fasta' 

If you can’t read Perl, that says. «Do a single substitution as follows. Wherever you see a bunch of digits next to each other in a row ( \d+ ), remember them (because I put that in parentheses), and then replace them with the evaluated expression of that bunch of digits ( $1 ) plus 100.».

Remove the -n if the dry-run looks correct. The only «tricky part» is the use of e at the end of the substitution which means evaluate the expression in the substitution — or I call it a «calculated replacement».

Источник

Оцените статью
Adblock
detector