You are not logged in.

#1 2021-09-25 13:01:27

TheConfiguringNeverEnds
Member
Registered: 2021-08-06
Posts: 10

In what order does "rm *" delete files?

Hello. So I was in my home directory and I ls'ed a subdir I was using to test a program. The test files were fine; everything looked in order, and so I wanted to remove all the contents of that subdir with

rm *

Unfortunately - as you probably noticed - I didn't actually cd into the subdir, so I ran "rm *" on my home directory. Absolutely horrifying when I realized it. Luckily, it ran into a write protected file before important things got deleted.

Sob-story aside, it still deleted some files. The scary thing is I can't tell what got deleted. Files I needed for my job (and forgot to move to my Documents folder) are fine, and nothing critical seems to be gone since I can reboot and use all my software still. I would like to know what could have been deleted though, so if I knew in what order rm chewed through my home directory, I might be able to determine what it deleted. The man pages weren't helpful, and the archwiki just redirects to them, so here I am. I have a feeling it's alphabetical, but I just want to make absolutely sure.

And before you tell me to go ' alias rm="rm -I" ', I already did that.

Offline

#2 2021-09-25 13:27:55

mpan
Member
Registered: 2012-08-01
Posts: 1,630
Website

Re: In what order does "rm *" delete files?

rm itself, in general, has no defined removal order.⁽¹⁾ Specific implementations of rm may use some particular order and it’s likely to be the order in which they are passed in the command line.

But in this invocation it is irrelevant, because it’s not rm that handles “*”. It’s your shell that expands the glob pattern to a list of file names that match the pattern. So even if rm explicitly orders removals, it’s the shell that decides in what order they are passed. Depending on the shell that may be fixed, may be dependent on your locale or may not be defined at all.

A side note: beware of using globs to generate arguments in scripts. If there is too many files, the invocation fails. For scripts consider find instead, with -delete as its final(!) argument. See -maxdepth and -name for filtering.
____
⁽¹⁾ IEEE 1003.1-2017 → rm

Last edited by mpan (2021-09-25 13:28:18)

Online

#3 2021-09-25 13:32:41

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,265

Re: In what order does "rm *" delete files?

https://serverfault.com/a/122743

"*" is shell globbing, try "echo *" to see the expansion at work.
It typically doesn't match hidden files and sort alphabetically, but that depends on your shell/config.

Plain "rm" will also not delete recursively, ie. it won't dive into subdirs (check "type rm" for the alias)

Offline

Board footer

Powered by FluxBB