Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove files with wildcard

Tags:

go

I am trying to delete files with a wildcard like shell scripts like:

c:\del 123_*

My trial as below was failed.

os.RemoveAll("/foo/123_*")
os.Remove("/foo/123_*")

I guess I need to use some library to use a wildcard.
What is good practice for deleting files with a wildcard?

like image 450
sungyong Avatar asked Jan 03 '18 06:01

sungyong


People also ask

How do I delete a wildcard file in Linux?

To remove multiple directories at once, use the rm -r command followed by the directory names separated by space. Same as with files, you can also use a wildcard ( * ) and regular expansions to match multiple directories.

How do I delete multiple files in rm?

To delete multiple files at once, simply list all of the file names after the “rm” command. File names should be separated by a space. With the command “rm” followed by multiple file names, you can delete multiple files at once.

How do you delete a csv file in Linux?

To remove or delete a file or directory in Linux, FreeBSD, Solaris, macOS, or Unix-like operating systems, use the rm command or unlink command.


1 Answers

As people mentioned wildcard is a feature of shell (e.g. Windows cmd.exe) not OS and usually programming languages don't provide equivalent of del xyz*. You should use Glob function to find files you want to delete.

files, err := filepath.Glob("/foo/123_*")
if err != nil {
    panic(err)
}
for _, f := range files {
    if err := os.Remove(f); err != nil {
        panic(err)
    }
}
like image 94
Arman Ordookhani Avatar answered Nov 10 '22 22:11

Arman Ordookhani