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?
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.
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.
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.
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With