Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing part of the file names

I have a bunch of files like this:

file123.txt.452
file456.txt.098
file789.txt.078

How can I remove the second dot and the numbers at the end from the file names?

I tried using rename but I don’t think my regex is correct:

rename 's/txt.*/txt/' *
like image 687
user2189312 Avatar asked Nov 25 '25 18:11

user2189312


1 Answers

Try rename 's/txt\..+/txt/' *.

\. is for the literal dot and .+ for anything after it.

The actual name of the command depends on your system. It could be rename, file-rename, perl-rename or something else. The rename command that the above code works with has this in its man entry:

NAME
       rename - renames multiple files

SYNOPSIS
       rename [-bfilnv] [-B prefix] [-S suffix] [-V method] [-Y prefix] [-z suffix]
       [--backup] [--basename-prefix=prefix] [--dry-run] [--force] [--help] [--no-stdin]
       [--interactive] [--just-print] [--link-only] [--prefix=prefix] [--suffix=suffix]
       [--verbose] [--version-control=method] [--version] perlexpr [files]...

DESCRIPTION
       rename renames the filenames supplied according to the rule specified as the first
       argument.  The argument is a Perl expression which is expected to modify the $_ string
       for at least some of the filenames specified.  If a given filename is not modified by
       the expression, it will not be renamed.  If no filenames are given on the command
       line, filenames will be read via standard input.
like image 132
Sebastian Simon Avatar answered Nov 27 '25 14:11

Sebastian Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!