Deepak jha
Deepak jha

Reputation: 308

Unix command ls -lrt giving unexpected results

So I was doing my work and I needed to list some particular files in a folder. I typed ls -lrt *ABCD*201604* and it returned *ABCD*201604* not found.

Then to see if such files actually were there or not I did ls -lrt and found that there were such files.

So next I copied ABCD and 201604 from listed files while typing command ls -lrt *ABCD*201604* and that worked!

I compared both of my commands to see if I did any mistake but found that both were exactly same. Any explanation for such behavior?

One which I wrote:

$ ls -lrt *RFPTDW*20160425*
*RFPTDW*20160425* not found

One which I copied:

$ ls -lrt *RFPTDW*20160425*
RFPTDW107_01_01_20160425_1526.txt
RFPTDW059_01_01_20160425_1527.txt

In second output I have omitted other details but filename.

Upvotes: 0

Views: 354

Answers (2)

wyrm
wyrm

Reputation: 317

The shell is responsible for expanding the globs on the command line. The command executed doesn't see those wildcard characters: it sees the expanded arguments.

When you want to see how those are expanded, try prepending the command you want with echo. Like such:

$ touch ABCD
$ touch 201604
$ touch foo_ABCD_bar_201604_baz
$ echo ls -lrt *ABCD*
ls -lrt ABCD foo_ABCD_bar_201604_baz
$ echo ls -lrt *201604*
ls -lrt 201604 foo_ABCD_bar_201604_baz
$ echo ls -lrt *ABCD*201604*
ls -lrt foo_ABCD_bar_201604_baz
$ rm foo_ABCD_bar_201604_baz
$ echo ls -lrt *ABCD*201604*
ls -lrt *ABCD*201604*

Notice that on the last line, the glob no longer expands to a filename, so the ls command would be handed the argument *ABCD*201604*, which names a file that does not exist.

Edit:

If you prepend echo to your actual commands, I expect that your result will be

$ echo ls -lrt *RFPTDW*20160425*
ls -lrt *RFPTDW*20160425*

in the first case, and

$ echo ls -lrt *RFPTDW*20160425*
ls -lrt RFPTDW107_01_01_20160425_1526.txt RFPTDW059_01_01_20160425_1527.txt

in the second case. In the first case, the files do not exist, so the glob does not expand to anything. In the second case, some files match the glob, and so the shell can expand it.

Upvotes: 3

Günther Jena
Günther Jena

Reputation: 3776

Sometimes character encoding is the problem. There are characters in Unicode which are looking quite the same as the ASCII ones (so called homoglyphs). So maybe there are "special" characters which only work when you copy paste them.

Also ASCII characters are sometimes hard to distinguish like 0-O (Zero, Letter O), or 1-l (One and Letter L)

Upvotes: 2

Related Questions