Search code examples
bashubuntutomcat8

Ubuntu terminal removing multiple partial files using wildcard


This maybe easy on linux users, but I am having a hard time figuring out how to delete multiple files (partial files) using wildcard.

sudo rm logs/archived/remove_me.2022.* or sudo rm logs/archived/remove_me.2022.? seems not to work.

I am getting the error rm: cannot remove 'logs/archived/remove_me.*': No such file or directory

I am currently on /var/lib/tomcat8/ trying to remove these logs inside logs/archived.

I am remove them one by one but there are a lot of files to remove .. (example. from 2020 and there are daily and in partial files).

Example I am inside /var/lib/tomcat8/logs/archived/ and I want to remove all log files starting with remove_me.2021.*

Below are the sample list of files that I want to remove.There are also other files in this directory that should not be removed.

remove_me.2022-03-02.1.log.gz
remove_me.2022-03-02.2.log.gz
remove_me.2022-03-02.3.log.gz
remove_me.2022-03-02.4.log.gz
remove_me.2022-03-02.5.log.gz
remove_me.2022-03-03.1.log
remove_me.2022-03-03.2.log
remove_me.2022-03-03.3.log
remove_me.2022-03-03.4.log
remove_me.2022-03-03.5.log
remove_me.2022-03-03.6.log
remove_me.2022-03-03.7.log
remove_me.2022-03-03.8.log
remove_me.2022-03-03.9.log
remove_me.2022-03-03.10.log

Solution

  • I believe the issue here is that the asterisk (*) is resolved by the current user, i.e., before becoming the superuser. Hence, it resolves to nothing, because the current user is not able to even read that directory.

    Solve this by becoming superuser first, and then doing everything normally:

    sudo -i
    cd /var/lib/tomcat8/logs/archived/
    rm remove_me.2022.*