Sort directories before files and underscore first with ls

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















When using 'ls' i want to sort directories first and then all files AND sort directories/files with underscore first. Here is an example:



_folder2
folder2
_file2
file


I can sort directories first with



ls --group-directories-first --color --ignore=lost+found


but the underscore at the beginning is ignored. Sorting underscore at the beginning works with



ls -d _* [!_]*


but both doesn't work together. I have tried a lot of combinations of the two commands, nothings works as i want it to work. sometimes the color is lost (whats important for me) or i have the subfolders and files of the directores instead of the folder name.



Also LC_COLLATE=C and LC_LOCALE=C doesn't work.










share|improve this question
























  • LC_COLLATE=C ls --group-directories-first should work as long as the LC_ALL env var is not set. LC_ALL=C ls --group-directories-first would always work but cause the error messages to be output in English.

    – Stéphane Chazelas
    Mar 7 at 13:53











  • No luck here. Don't work with LC_COLLATE=C or LC_ALL=C.

    – fmeier
    Mar 7 at 17:14











  • Note that it's not LC_ALL=C; ls ..., it's LC_ALL=C ls ... (or LC_ALL=C; export LC_ALL; ls, but that would mean setting the locale to C for every command, not just ls), or with csh/tcsh/fish env LC_ALL=C ls ...

    – Stéphane Chazelas
    Mar 7 at 17:28












  • ls -d _* [!_]* wouldn't make a difference because ls still sorts the file list. You'd need ls -Ud -- _* [!_]* (assuming GNU ls) to disable that sorting.

    – Stéphane Chazelas
    Mar 7 at 17:32

















1















When using 'ls' i want to sort directories first and then all files AND sort directories/files with underscore first. Here is an example:



_folder2
folder2
_file2
file


I can sort directories first with



ls --group-directories-first --color --ignore=lost+found


but the underscore at the beginning is ignored. Sorting underscore at the beginning works with



ls -d _* [!_]*


but both doesn't work together. I have tried a lot of combinations of the two commands, nothings works as i want it to work. sometimes the color is lost (whats important for me) or i have the subfolders and files of the directores instead of the folder name.



Also LC_COLLATE=C and LC_LOCALE=C doesn't work.










share|improve this question
























  • LC_COLLATE=C ls --group-directories-first should work as long as the LC_ALL env var is not set. LC_ALL=C ls --group-directories-first would always work but cause the error messages to be output in English.

    – Stéphane Chazelas
    Mar 7 at 13:53











  • No luck here. Don't work with LC_COLLATE=C or LC_ALL=C.

    – fmeier
    Mar 7 at 17:14











  • Note that it's not LC_ALL=C; ls ..., it's LC_ALL=C ls ... (or LC_ALL=C; export LC_ALL; ls, but that would mean setting the locale to C for every command, not just ls), or with csh/tcsh/fish env LC_ALL=C ls ...

    – Stéphane Chazelas
    Mar 7 at 17:28












  • ls -d _* [!_]* wouldn't make a difference because ls still sorts the file list. You'd need ls -Ud -- _* [!_]* (assuming GNU ls) to disable that sorting.

    – Stéphane Chazelas
    Mar 7 at 17:32













1












1








1


1






When using 'ls' i want to sort directories first and then all files AND sort directories/files with underscore first. Here is an example:



_folder2
folder2
_file2
file


I can sort directories first with



ls --group-directories-first --color --ignore=lost+found


but the underscore at the beginning is ignored. Sorting underscore at the beginning works with



ls -d _* [!_]*


but both doesn't work together. I have tried a lot of combinations of the two commands, nothings works as i want it to work. sometimes the color is lost (whats important for me) or i have the subfolders and files of the directores instead of the folder name.



Also LC_COLLATE=C and LC_LOCALE=C doesn't work.










share|improve this question
















When using 'ls' i want to sort directories first and then all files AND sort directories/files with underscore first. Here is an example:



_folder2
folder2
_file2
file


I can sort directories first with



ls --group-directories-first --color --ignore=lost+found


but the underscore at the beginning is ignored. Sorting underscore at the beginning works with



ls -d _* [!_]*


but both doesn't work together. I have tried a lot of combinations of the two commands, nothings works as i want it to work. sometimes the color is lost (whats important for me) or i have the subfolders and files of the directores instead of the folder name.



Also LC_COLLATE=C and LC_LOCALE=C doesn't work.







linux directory ls filenames






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 13:47









Jeff Schaller

44.6k1162145




44.6k1162145










asked Mar 7 at 13:32









fmeierfmeier

393




393












  • LC_COLLATE=C ls --group-directories-first should work as long as the LC_ALL env var is not set. LC_ALL=C ls --group-directories-first would always work but cause the error messages to be output in English.

    – Stéphane Chazelas
    Mar 7 at 13:53











  • No luck here. Don't work with LC_COLLATE=C or LC_ALL=C.

    – fmeier
    Mar 7 at 17:14











  • Note that it's not LC_ALL=C; ls ..., it's LC_ALL=C ls ... (or LC_ALL=C; export LC_ALL; ls, but that would mean setting the locale to C for every command, not just ls), or with csh/tcsh/fish env LC_ALL=C ls ...

    – Stéphane Chazelas
    Mar 7 at 17:28












  • ls -d _* [!_]* wouldn't make a difference because ls still sorts the file list. You'd need ls -Ud -- _* [!_]* (assuming GNU ls) to disable that sorting.

    – Stéphane Chazelas
    Mar 7 at 17:32

















  • LC_COLLATE=C ls --group-directories-first should work as long as the LC_ALL env var is not set. LC_ALL=C ls --group-directories-first would always work but cause the error messages to be output in English.

    – Stéphane Chazelas
    Mar 7 at 13:53











  • No luck here. Don't work with LC_COLLATE=C or LC_ALL=C.

    – fmeier
    Mar 7 at 17:14











  • Note that it's not LC_ALL=C; ls ..., it's LC_ALL=C ls ... (or LC_ALL=C; export LC_ALL; ls, but that would mean setting the locale to C for every command, not just ls), or with csh/tcsh/fish env LC_ALL=C ls ...

    – Stéphane Chazelas
    Mar 7 at 17:28












  • ls -d _* [!_]* wouldn't make a difference because ls still sorts the file list. You'd need ls -Ud -- _* [!_]* (assuming GNU ls) to disable that sorting.

    – Stéphane Chazelas
    Mar 7 at 17:32
















LC_COLLATE=C ls --group-directories-first should work as long as the LC_ALL env var is not set. LC_ALL=C ls --group-directories-first would always work but cause the error messages to be output in English.

– Stéphane Chazelas
Mar 7 at 13:53





LC_COLLATE=C ls --group-directories-first should work as long as the LC_ALL env var is not set. LC_ALL=C ls --group-directories-first would always work but cause the error messages to be output in English.

– Stéphane Chazelas
Mar 7 at 13:53













No luck here. Don't work with LC_COLLATE=C or LC_ALL=C.

– fmeier
Mar 7 at 17:14





No luck here. Don't work with LC_COLLATE=C or LC_ALL=C.

– fmeier
Mar 7 at 17:14













Note that it's not LC_ALL=C; ls ..., it's LC_ALL=C ls ... (or LC_ALL=C; export LC_ALL; ls, but that would mean setting the locale to C for every command, not just ls), or with csh/tcsh/fish env LC_ALL=C ls ...

– Stéphane Chazelas
Mar 7 at 17:28






Note that it's not LC_ALL=C; ls ..., it's LC_ALL=C ls ... (or LC_ALL=C; export LC_ALL; ls, but that would mean setting the locale to C for every command, not just ls), or with csh/tcsh/fish env LC_ALL=C ls ...

– Stéphane Chazelas
Mar 7 at 17:28














ls -d _* [!_]* wouldn't make a difference because ls still sorts the file list. You'd need ls -Ud -- _* [!_]* (assuming GNU ls) to disable that sorting.

– Stéphane Chazelas
Mar 7 at 17:32





ls -d _* [!_]* wouldn't make a difference because ls still sorts the file list. You'd need ls -Ud -- _* [!_]* (assuming GNU ls) to disable that sorting.

– Stéphane Chazelas
Mar 7 at 17:32










0






active

oldest

votes












Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504919%2fsort-directories-before-files-and-underscore-first-with-ls%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504919%2fsort-directories-before-files-and-underscore-first-with-ls%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay