Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want a folder called DELETED to be first in my output of ls.

Is there a symbol I can add to the front that can cause this? I've tried ^ and - and neither works.

share|improve this question
    
I am not sure if I understand your question correctly. Are you by any chance looking for ls DELETED? –  Ramesh Mar 25 at 20:44
    

2 Answers 2

up vote 9 down vote accepted

^, in ASCII order, follows the uppercase letters. - precedes all the letters and digits, but follows several other punctuation characters.

The ASCII printable characters, in order, are:

 !"#$%&'()*+,-./0123456789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
`abcdefghijklmnopqrstuvwxyz{|}~

But this doesn't necessarily help you, since the order in which ls lists files may be affected by the current locale.

You could start the file name with a space or ! character, but that's going to make it difficult to refer to the file without quoting it -- and again, locale settings could interfere. Most of the other ASCII characters that precede the decimal digits will have similar problems.

Your best bet, assuming you don't have other files with odd characters in their names, might be to start the file name with a string of 0s:

 000_DELETED

(I've used a similar technique to name e-mail folders in Thunderbird and Outlook.)

share|improve this answer

On GNU systems at least in most locales other than C/POSIX, there are a few characters that are ignored with regards to sorting. That's the case for instance of _, ., space or 0x1.

That's why .a, .c, __b, _d sort as .a, __b, .c, _d for instance.

For strings that consist only of those ignored characters, the sort order is defined in the locale, ASCII SPC comes generally first. So a single space character would be the file that sorts first (even before 0x1 (the first one in C locale), . and ..), then 2 spaces... etc. However, <SPC>DELETED would sort after DELETED, so while a file called ___ would sort before most other files, putting ___ in front of some other string doesn't make it sort sooner.

Among the non-ignored characters, ASCII 0 is generally the character that sorts first in non-C locales.

So a file called 0 would sort before any other file than contains non-ignored characters. Prepending 0 to a file that contains non-ignored characters would make it sort earlier in non-C locales.

So a file called $'\01\01\01_000_DELETED' would sort very early in the C locale (because 0x1 is the first character so sorts first), and also in other locales (because all the 0x1, and _ are ignored, but then the 0s are the non-ignored characters that sort first).

In non-C locales, it would still sort after . or ...

Another option could be to write DELETED with only ignored characters like " ₫€£€₮€₫". Those currency symbols are ignored wrt sorting. SPC also sorts quite early in the C locale, so it should sort before . both in C and non-C locales.

$ touch $'   \u20ab\u20ac\u00a3\u20ac\u20ae\u20ac\u20ab' $'\01' ' ' 0normal-file $'\01\01\01_000_DELETED'
$ ls -1a

   ₫€£€₮€₫
./
../
?
???_000_DELETED
0normal-file
$ LC_ALL=C ls -1a
?
???_000_DELETED

   ????????????????????
./
../
0normal-file
$ LC_COLLATE=C ls -1a
?
???_000_DELETED

   ₫€£€₮€₫
./
../
0normal-file
share|improve this answer
    
What is 0x1? Is it the ASCII SOH? –  terdon Mar 26 at 5:21
    
@terdon: Yes, it's SOH (character code 1). The character with ASCII code 0 (NUL) is not allowed in Unix file names; 1 is the next character code in numerical order after it. It has no other particular significance in most modern computer systems. –  Ilmari Karonen Mar 26 at 5:29
    
@IlmariKaronen so it is SOH as I said? I'm not conversant in hex. –  terdon Mar 26 at 5:31
    
@terdon, what matters for the C locale is that it is the first code point allowed in a filename, that it is the 1 byte or U+0001 unicode character. That it is the Star of Header character has little significance here. –  Stéphane Chazelas Mar 26 at 7:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.