I have following heapdump files
AppSrv01]# ls -ltr *heapdump* -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3436 -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3435 -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3434 -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3433 -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3432 -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3431 -rw-r--r-- 1 root root 0 Sep 13 17:03 heapdump.dfsdf
now if I use
ls -ltr| grep heapdump*
I get following output
AppSrv01]# ls -ltr |grep heapdump* -rw-r--r-- 1 root root 0 Sep 13 17:03 heapdump.dfsdf
But I use
ls -ltr |grep *heapdump*
I get no output. Could anybody help where is my mistake and how to search the heapdump files.
My expected output is
ls -ltr | grep *heapdump*
should give me
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3436 -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3435 -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3434 -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3433 -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3432 -rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3431 -rw-r--r-- 1 root root 0 Sep 13 17:03 heapdump.dfsdf
due to some reason I could not use
ls -ltr *heapdump*
Advertisement
Answer
It looks like you’re confusing glob patterns with regular expressions, which is what grep uses.
The correct way to print a list of files containing the word heapdump is like this:
printf '%sn' *heapdump*
Here, *heapdump* is glob-expanded by the shell to match anything followed by “heapdump” followed by anything else. All the files that match the pattern are passed to printf, which prints each one followed by a newline.
If you want to filter the output of ls -ltr, then you can use:
ls -ltr *heapdump*
The same list of files will be passed to ls, which will print information about them.
Bear in mind that it is not recommended to attempt to parse the output of ls.