how can I highlight just one item from the ls output

rbrt

real beginner in Unix commands so not sure if the following is actually possible but here goes. Is it possible to highlight just one item in a ls output?

I.e.: in a directory I use the following

ls -l --color=auto

this lists 4 items in green

file1.xls
file2.xls
file3.xls
file4.xls

But I want to highlight a specific item, in this case file2. Is this possible?

Thomas Dickey

The ls program will not do this for you. But you could filter the results from ls through a custom script which modifies the text to highlight just one item. It would be simpler if no color was originally given; then you could match on the given filename (for example as the pattern in an awk script, or in a sed script) and modify just that one item, adding colors.

That is, certainly it is possible. Writing a sample script is a different question.

How you approach the problem depends on what you want from the output. If that is (literally) the output from ls with a single filename in color, then a script would be the normal approach. You could use grep as suggested in the other answer, which raises a few issues:

  • commenting on ls -l --color=auto makes it sound as if you are using GNU ls, hence likely using Linux. An appropriate tag for the question would be linux rather than unix. If you ask for unix, the answers should differ.
  • supposing that you are using Linux. Then likely you have GNU grep, which can do colors. That would let you do something like this:

    ls -l | grep --color=always file2 |less -R

  • however, there is a known bug in GNU grep's use of color (see xterm FAQ "grep --color" does not show the right output).
  • using grep like this shows only the matching lines. For ls that might be a good choice. For matches in a manual page -- definitely not.

Alternatively, less (which is found more often on Unix systems than GNU grep) also can highlight matches (not in color) and would show the file you are looking for in context. You could do this:

ls -l | less -p file2

(Both grep and less use patterns aka regular expressions, but I left the example simple — read the documentation to learn more).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can you get a multiple output in json in just one loop?

From Dev

How can I keep Jekyll from adding whitespace in highlight?

From Dev

How can i create from 2 functions just one?

From Dev

How can I highlight a specific div from a hash in the URL?

From Dev

how I can insert just one row?

From Dev

How can I use LINQ to just return one row from a selection?

From Dev

How can I prevent my while loop from printing out just one result?

From Dev

how can I extract item from row to another one with bootstrap?

From Dev

how to get just one item from array of strings if the other is the same

From Dev

How can I manually highlight an item in a JQuery UI menu?

From Dev

How do I select a field/column from the output of `ls -l`?

From Dev

How can I get different data types from the keyboard in just one line?

From Dev

How can I get just one column from a table?

From Dev

How can i create from 2 functions just one?

From Dev

How can i highlight a particular item on selection in a navigation tree

From Dev

How can I write an alias for cd such that it will fire ls after going into the directory to which I just cd'ed into?

From Dev

How can I capture two columns of ls output in a bash script

From Dev

How can I use the output of ls/grep as input for pdfunite

From Dev

How can I remove more than one item from a listbox?

From Dev

How can I see a history of the terminal output, not just commands entered?

From Dev

How to make a list of one item just that item

From Dev

How can I get just the "free swap" value from the output of the "free" command?

From Dev

How Can i control the 'total' output in 'ls -l' command?

From Dev

How can I get the 'ls' output to a variable in grub2?

From Dev

How can I know the type of each element in the output of ls -l?

From Dev

How can I use LINQ to just return one row from a selection?

From Dev

How can I loop through a jquery array and then output each item (starting at index 0) one at a time?

From Dev

how can i select item from option menu if i have one item

From Dev

How can I select from items from two tables with one item only have one value

Related Related

  1. 1

    How can you get a multiple output in json in just one loop?

  2. 2

    How can I keep Jekyll from adding whitespace in highlight?

  3. 3

    How can i create from 2 functions just one?

  4. 4

    How can I highlight a specific div from a hash in the URL?

  5. 5

    how I can insert just one row?

  6. 6

    How can I use LINQ to just return one row from a selection?

  7. 7

    How can I prevent my while loop from printing out just one result?

  8. 8

    how can I extract item from row to another one with bootstrap?

  9. 9

    how to get just one item from array of strings if the other is the same

  10. 10

    How can I manually highlight an item in a JQuery UI menu?

  11. 11

    How do I select a field/column from the output of `ls -l`?

  12. 12

    How can I get different data types from the keyboard in just one line?

  13. 13

    How can I get just one column from a table?

  14. 14

    How can i create from 2 functions just one?

  15. 15

    How can i highlight a particular item on selection in a navigation tree

  16. 16

    How can I write an alias for cd such that it will fire ls after going into the directory to which I just cd'ed into?

  17. 17

    How can I capture two columns of ls output in a bash script

  18. 18

    How can I use the output of ls/grep as input for pdfunite

  19. 19

    How can I remove more than one item from a listbox?

  20. 20

    How can I see a history of the terminal output, not just commands entered?

  21. 21

    How to make a list of one item just that item

  22. 22

    How can I get just the "free swap" value from the output of the "free" command?

  23. 23

    How Can i control the 'total' output in 'ls -l' command?

  24. 24

    How can I get the 'ls' output to a variable in grub2?

  25. 25

    How can I know the type of each element in the output of ls -l?

  26. 26

    How can I use LINQ to just return one row from a selection?

  27. 27

    How can I loop through a jquery array and then output each item (starting at index 0) one at a time?

  28. 28

    how can i select item from option menu if i have one item

  29. 29

    How can I select from items from two tables with one item only have one value

HotTag

Archive