0

ls -l list out the files and folders in the directory and nice shows the programs or files based on priority if I'm not wrong. However, what does nice -999 ls -l do? I have tried the command in terminal and getting result like below:

total 16
-rw-r--r-- 1 muu muu   41 Jul 11 00:47 file.txt
drwx------ 2 muu muu 4096 May  4 17:12 Jam
-rw-r--r-- 1 muu muu 1841 May  4 17:18 Jam.zip
-rwxrwxrwx 1 muu muu  197 Jul 10 23:28 t.sh

But I don't understand what's exactly is it doing? Can someone please explain? TIA

1

nice: nice - run a program with modified scheduling priority

Format: nice [OPTION] [COMMAND [ARG]...]

So nice -999 ls -l should be nice -9 ls -l or nice 10 ls -l or so, what it would do here is to set the nice level of the ls command to the nice value given. But in your case you use a nice value of -999 its range is -20 to 19.

The purpose of the nice command is to make sure a given command runs at a set priority other than that of its default priority. So say ls command should run with a nice priority of 10 then running it with nice 1 ls will make it more favourable to other processes (i.e. it will give way to others with less favourable niceness, so to speak)

Usage:

Modify niceness:

nice -10 ls -l

Modify to negative niceness:

nice --10 ls -l
# Note the use of "--"

Modify an already running process' niceness:

renice -12 -p <PID> 

Modify an already running process' niceness to negative value:

renice 12 -p <PID>

Source:

man nice

1

Explanation

It's running the ls command using the lowers priority.

  • -999 is equal to -n 999 and because the nice level is a number between -20 to the 19 the 999 will be considered as 19.

so your ls command is being run with the nice level of 19.


Test

Open a terminal and run:

$ ps -o pid,comm,nice | grep bash
8231 bash              0

You can see the nice level of bash is 0 which is the default, now run:

$ nice -999 bash

You'll will be in a new bash. Again run ps -o pid,comm,nice | grep bash:

 8231 bash              0
 8273 bash             19

You can see the nice level of new bash is equal to 19. (Running with the lowest priority).

0

Nice doesn't "show based on priority"; it runs commands with modified priority. The command does exactly what it would do if it started with the ls -l (that is to say, it lists the files in the current directory, including such details as permissions, size, and access times), but at reduced priority, increasing its niceness by 999).

You should also be aware that there is no point using niceness values so large: it'll be clamped to the range -20 to 19 anyway.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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