Skip to content
Advertisement

ls to list matches in specified directory

How do I list matched results in a specified directory?

On my Ubuntu server if I list the contents of a directory it correctly lists it. My working directory is /var/crash.

JavaScript

Let’s say I want to list all files that contain ‘tar’. In this example there should only be one match i.e. start.txt

JavaScript

All’s good so far. However if I include the directory (/var/crash) it lists all files.

JavaScript

I’m guessing my syntax is telling ls to list all matches of tar AND everything in /var/crash. What is the correct syntax to list matches in a specified directory?

Advertisement

Answer

You need to specify the pattern together with the directory:

JavaScript

Otherwise, with ls -l *tar* /var/crash you are telling ls to act against two parameters: /var/crash and *tar*. In fact, *tar* will be expanded before ls reaches it, so there might be more parameters for ls.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement