1

I have a backup script which is creating two files:

a) db_mywebsite_2018-01-10_00-25.sql.gz (150 MB)

b) mywebsite1_2018-01-10_00-01.tar.gz (2 GB)

I want to remove the date (XXXX-XX-XX_XX-XX) from file names. Because the backup is made daily, each file will have a different date in the name. For this, I'm using the following command:

rename -v -n 's/_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}//' *.sql.gz

For testing, I have touched two files db_mywebsite_2018-01-10_00-25.sql.gz and mywebsite1_2018-01-10_00-01.tar.gz

Command:

rename -v -n 's/_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}//' *.sql.gz

Output:

db_mywebsite_2018-01-10_00-25.sql.gz renamed as db_mywebsite.sql.gz

1. The problem is that despite the above message "db_mywebsite_2018-01-10_00-25.sql.gz renamed as db_mywebsite.sql.gz" when I check it via ls -l it looks like that file name hasn't been renamed.

2. Rename command (regex) is not working on file mywebsite1_2018-01-10_00-01.tar.gz.

1 Answer 1

3
  1. For the perl version of rename, the option -n means no action: print names of files to be renamed, but don't rename. If you want the file to be renamed, don't use the -n option. Thus, instead of:

    rename -v -n 's/_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}//' *.sql.gz
    

    Use:

    rename -v 's/_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}//' *.sql.gz
    
  2. Consider:

    rename -v -n 's/_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}//' *.sql.gz
    

    The above command will only attempt to rename files ending in .sql.gz. It will not to rename mywebsite1_2018-01-10_00-01.tar.gz because that file ends with .tar.gz. To rename both files, try:

    rename -v -n 's/_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}//' *.tar.gz *.sql.gz
    
1
  • 1
    +1. -n is for debugging/testing your regex without making potentially irreversible changes to your filenames. i.e. it's a --dry-run option.
    – cas
    Commented Jan 11, 2018 at 6:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .