Does rm work alphabetically?

4

I had many files in a folder in which I accidentally ran rm * before interrupting.

It looks like files starting with a, b, and c are gone, but I am not sure, as I didn't know the name of all the files.

Can I rely on that rm works alphabetically so that I can know exactly which files that have been removed?

rajat

Posted 2012-10-03T14:00:54.360

Reputation: 339

Off topic a bit, but if you are looking to recover the files you can use TestDisk or the solutions given in Where do files go when the rm command is issued?

– Karthik T – 2012-10-05T04:44:27.007

Answers

7

rm does not necessarily work alphabetically, but it works in the order you supply its arguments (with -r it becomes more complicated, but that doesn't matter here). If you wrote exactly as you said, the * will expand to all file names in an alphabetical manner.

So yes, you have removed files alphabetically.

Try executing

echo *

in a directory to see in which order things show up. This is the same order that rm * would remove the files.

Daniel Andersson

Posted 2012-10-03T14:00:54.360

Reputation: 20 465

1

Yes they work alphabetically..

you can use strace command to check the sequence and how your kernel is working internally. Below is the strace command I used for rm command it translate * to alphabetic sequence.

surendra@linuxnix.com:~/scripts/temp/temp$ strace rm -rf *

execve("/bin/rm", ["rm", "-rf", "a.txt", "b.txt", "c.txt", "d.txt"], [/* 43 vars */]) = 0

<--I have clipped the output-->

Where ever * is there, your bash shell will try to give all the combinations in this order. Some other commands which support this are cp, mv, find etc.

Surendra Anne

Posted 2012-10-03T14:00:54.360

Reputation: 36