I am currently trying to modify 'ls.c' source file in order to hide files and folders containing the word hidden. After doing some research, I've found that this could be possible by adding this code into 'ls.c' source file:

            char attr_command[1024] = "attr -Lqg hidden "; // Oh, dear. That's bad
            int attr_code;
            strcat(attr_command, d->d_name);
            strcat(attr_command, " >/dev/null 2>&1");
            attr_code = system(attr_command);
            if (!attr_code)

This code would have to be added after the following line:

while ((d = readdir(dp))) {

We can get the coreutils source files (including ls.c) via 'git clone git://git.suckless.org/sbase'

So after running 'make' with the modified 'ls.c' source file, the file 'hidden' is still showing up.

I need to modify 'ls.c' to really hide files and folders.

  • Thank you for your quick reply. I am actually using the freshly built 'ls' I've built with 'make' by doing ./ls. And no I do not use the original 'ls' binary that was originally built with my Ubuntu for testing if file 'hidden' is really hidden or not. – Gerald Feb 10 at 20:18
  • 2
    err, do you mean to hide files with the string "hidden" in their names? Or something else? – ilkkachu Feb 10 at 20:37
  • yes this is exactly what I'm trying to do; hide files with the string 'hidden'. Thanks – Gerald Feb 10 at 20:42

The attr command deals with extended attributes, particularly in relation to the XFS filesystem. (It also works with ext4, but I suppose getfattr and setfattr are meant for general, filesystem-independent use.)

That is to say, attr -Lqg hidden "$filename" doesn't check anything about the file's name, but it checks if an extended attribute called hidden is set on the file.

If you want to check if the file name contains a particular string, you should probably use the strstr() function.

Since d->d_name contains the name of the file being processed, something like this might work.

if (strstr(d->d_name, "hidden") == 0) { ... 

That doesn't mean you couldn't base file hiding on extended attributes... But even if you do, it might be better to look up the actual system calls used for that. system() forks off a shell and the external process, and with a long directory listing, that may be noticeably slow.

Also, note that modifying ls will do nothing to other programs that can also give a file listing. They may be as simple as find, or printf "%s\n" *.

  • I've tried adding system("grep -v hidden"); but it doesn't work unfortunately. Do you have an idea of the correct code to add with system() ? Thanks – Gerald Feb 10 at 21:51

Allright so I've kinda found a workaround to "hide" a string for 'ls' by adding the following line in 'ls.c' after line 261 (thanks to ilkkachu for his answer below):

                if (strstr(d->d_name, "HIDDEN") == 0)
                    return 0;

Probably not the best solution but hey it works :)

If anyone out there would like to suggest a better option/solution, please do it, I would love to have more options and a better one if possible !


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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