0

I've a folder with thousands of files with this pattern: YYYYMMDD_HH24MISS_4DIGITSEQUENCE.

Example: 20180626_210123_0001.

Now for example while I'm deleting via rm -f *20180626_1* command its working fine.

My question is: 1. Which file is being deleted first? 2. Is it being picked randomnly? 3. If it maintains any sequence while choosing, then which one and how it decides?

ilkkachu
  • 138,973

2 Answers2

1

The shell expands the expression before it executes rm, and that expansion is not specific to the command, so if you do:

echo *20180626_1*

Then the first thing that echos will be the first thing that the shell passes to rm. The order isn't random, it's alphabetical. From the bash man page:

After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.

Andy Dalton
  • 13,993
1

Globs are expanded by the shell in lexicographic order (in the current locale), and the rm implementation is likely to remove the files it gets as arguments in the same order it got them. So in your case the files would be removed starting with the oldest.

Sorting the glob results is required by POSIX. On a quick test, at least GNU rm removes files given on the command line in the order listed, and doesn't sort files found during recursive operation.

ilkkachu
  • 138,973