LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Globbing in ls... (https://www.linuxquestions.org/questions/linux-newbie-8/globbing-in-ls-4175563423/)

mangya 01-08-2016 12:06 PM

Globbing in ls...
 
Hello

I'm using CentOS 7.

Following is the directory listing:
Code:

[root@c72 /etc/systemd/system]# ls
basic.target.wants                          dbus-org.freedesktop.nm-dispatcher.service  getty.target.wants      sysinit.target.wants
dbus-org.fedoraproject.FirewallD1.service    default.target                              multi-user.target.wants  system-update.target.wants
dbus-org.freedesktop.NetworkManager.service  default.target.wants                        sockets.target.wants

I want to list any file which has 'target' in it.

So I issued the command ls *target*, but its not working.
Code:

[root@c72 /etc/systemd/system]# ls *target*
default.target

basic.target.wants:
firewalld.service  microcode.service

default.target.wants:
systemd-readahead-collect.service  systemd-readahead-replay.service

getty.target.wants:
getty@tty1.service

multi-user.target.wants:
auditd.service  crond.service  irqbalance.service  kdump.service  NetworkManager.service  postfix.service  remote-fs.target  rsyslog.service  sshd.service  tuned.service

sockets.target.wants:
dm-event.socket

sysinit.target.wants:
lvm2-lvmetad.socket  lvm2-lvmpolld.socket  lvm2-monitor.service

system-update.target.wants:
systemd-readahead-drop.service

This guide says it will. But why I am not getting correct results?

Thanks

TB0ne 01-08-2016 12:33 PM

Quote:

Originally Posted by mangya (Post 5475105)
Hello
I'm using CentOS 7. Following is the directory listing:
Code:

[root@c72 /etc/systemd/system]# ls
basic.target.wants                          dbus-org.freedesktop.nm-dispatcher.service  getty.target.wants      sysinit.target.wants
dbus-org.fedoraproject.FirewallD1.service    default.target                              multi-user.target.wants  system-update.target.wants
dbus-org.freedesktop.NetworkManager.service  default.target.wants                        sockets.target.wants

I want to list any file which has 'target' in it. So I issued the command ls *target*, but its not working.
Code:

[root@c72 /etc/systemd/system]# ls *target*
default.target

basic.target.wants:
firewalld.service  microcode.service

default.target.wants:
systemd-readahead-collect.service  systemd-readahead-replay.service

getty.target.wants:
getty@tty1.service

multi-user.target.wants:
auditd.service  crond.service  irqbalance.service  kdump.service  NetworkManager.service  postfix.service  remote-fs.target  rsyslog.service  sshd.service  tuned.service

sockets.target.wants:
dm-event.socket

sysinit.target.wants:
lvm2-lvmetad.socket  lvm2-lvmpolld.socket  lvm2-monitor.service

system-update.target.wants:
systemd-readahead-drop.service

This guide says it will. But why I am not getting correct results?Thanks

You *ARE* getting the correct results. You're seeing EVERYTHING with "target" in it...including directories. There is no option in ls to only show files. There are glob qualifiers that WILL do this, but not in bash...you'll have to use zsh instead. If you want to use bash, I'd suggest a compound-command, using "find" instead:
Code:

find . -maxdepth 1 -type f -print0 | xargs -0r ls | grep target"
Bear in mind that if you leave out the "maxdepth" flag, it will recursively search that tree. You can also do:
Code:

ls -p | grep -v / | grep target
...which is another way to do it.

grail 01-08-2016 01:07 PM

I would have gone with find also:
Code:

find . -maxdepth 1 -type f -name '*target*'

mangya 01-08-2016 02:07 PM

@TB0ne and @grail

Unfortunately none of the commands worked.

Code:

find . -maxdepth 1 -type f -print0 | xargs -0r ls | grep target = No result.
ls -p | grep -v / | grep target                                = Same as ls *target
find . -maxdepth 1 -type f -name '*target*'                    = No result.

Quote:

TB0ne
There is no option in ls to only show files. There are glob qualifiers that WILL do this, but not in bash...you'll have to use zsh instead.
Didn't knew bash wont support it. And with this knowledge i'll figure out some other way to do it.

Thanks anyway.

TB0ne 01-08-2016 02:38 PM

Quote:

Originally Posted by mangya (Post 5475177)
@TB0ne and @grail
Unfortunately none of the commands worked.
Code:

find . -maxdepth 1 -type f -print0 | xargs -0r ls | grep target = No result.
ls -p | grep -v / | grep target                                = Same as ls *target
find . -maxdepth 1 -type f -name '*target*'                    = No result.


I have *NO IDEA* what you're running, but on CentOS (6 and 7), and openSUSE 13.2, ALL of those commands work just fine, and return exactly what you're after. What context are you running this in?? Any details???
Quote:

Didn't knew bash wont support it. And with this knowledge i'll figure out some other way to do it.Thanks anyway.
Good luck...the three ways provided already do work.

mangya 01-08-2016 03:16 PM

Quote:

Originally Posted by TB0ne (Post 5475191)
I have *NO IDEA* what you're running, but on CentOS (6 and 7), and openSUSE 13.2, ALL of those commands work just fine, and return exactly what you're after. What context are you running this in?? Any details???

Good luck...the three ways provided already do work.

I figured it out. They are actually symbolic links. -type l will do. Nevermind, i just removed -type f and got results.

Code:

[root@c72 /etc/systemd/system]# ls -l
total 4
drwxr-xr-x. 2 root root  54 Jan  5 21:40 basic.target.wants
lrwxrwxrwx. 1 root root  41 Jan  5 21:40 dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service
lrwxrwxrwx. 1 root root  46 Jan  5 20:57 dbus-org.freedesktop.NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx. 1 root root  57 Jan  5 20:57 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
lrwxrwxrwx. 1 root root  37 Jan  5 21:00 default.target -> /lib/systemd/system/multi-user.target
drwxr-xr-x. 2 root root  85 Jan  5 20:57 default.target.wants
drwxr-xr-x. 2 root root  31 Jan  5 20:57 getty.target.wants
drwxr-xr-x. 2 root root 4096 Jan  5 20:58 multi-user.target.wants
drwxr-xr-x. 2 root root  28 Jan  5 20:57 sockets.target.wants
drwxr-xr-x. 2 root root  86 Jan  5 20:57 sysinit.target.wants
drwxr-xr-x. 2 root root  43 Jan  5 20:57 system-update.target.wants

[root@c72 /etc/systemd/system]# find -maxdepth 1 -iname '*target*'
./multi-user.target.wants
./getty.target.wants
./default.target.wants
./system-update.target.wants
./sockets.target.wants
./sysinit.target.wants
./basic.target.wants
./default.target

Thanks

Habitual 01-08-2016 03:18 PM

"next time" try
Code:

type ls
to see if it's aliased "funny".

mangya 01-08-2016 03:21 PM

Quote:

Originally Posted by Habitual (Post 5475219)
"next time" try
Code:

type ls
to see if it's aliased "funny".

Code:

[root@c72 /etc/systemd/system]# type ls
ls is aliased to `ls --color=auto'

Its all fine. Thanks

suicidaleggroll 01-08-2016 03:26 PM

Quote:

Originally Posted by mangya (Post 5475218)
I figured it out. They are actually symbolic links. -type l will do. Nevermind, i just removed -type f and got results.

You said in the original post that you only wanted files. If you want everything with "target" in the name just add the "-d" flag to ls to tell it to print the directory names rather than the contents. There's no need for find in this case since you don't care about the type.

mangya 01-08-2016 03:37 PM

Quote:

Originally Posted by suicidaleggroll (Post 5475227)
You said in the original post that you only wanted files. If you want everything with "target" in the name just add the "-d" flag to ls to tell it to print the directory names rather than the contents. There's no need for find in this case since you don't care about the type.

Sorry about that... I should have typed 'list anything'. My apologies. Anyway, that folder contained both directories and files (Actually a link) with word 'target' in them. I just wanted to know why *something* doesn't work. I just chose bad example.

Cheers.

suicidaleggroll 01-08-2016 03:39 PM

Then the only problem was that ls prints directory contents rather than directory names for matches. If you only want the names, add the "-d" flag. From "man ls":
Code:

      -d, --directory
              list directories themselves, not their contents


mangya 01-08-2016 04:02 PM

Quote:

Originally Posted by suicidaleggroll (Post 5475238)
Then the only problem was that ls prints directory contents rather than directory names for matches. If you only want the names, add the "-d" flag. From "man ls":
Code:

      -d, --directory
              list directories themselves, not their contents


Sorry I think I couldn't make my point properly. English is not my native. I chose bad example. Let me illustrate correctly.




Code:

# mkdir linuxkernel CentosLinuxOS TheLinux somefolder1 somefolder2 somefolder3
# touch  linux.kernel Centos-Linux-OS The.Linux file1 file2 file3


# ls -l
total 0
drwxr-xr-x. 2 root root 6 Jan  9 03:23 CentosLinuxOS
-rw-r--r--. 1 root root 0 Jan  9 03:23 Centos-Linux-OS
-rw-r--r--. 1 root root 0 Jan  9 03:23 file1
-rw-r--r--. 1 root root 0 Jan  9 03:23 file2
-rw-r--r--. 1 root root 0 Jan  9 03:24 file3
drwxr-xr-x. 2 root root 6 Jan  9 03:23 linuxkernel
-rw-r--r--. 1 root root 0 Jan  9 03:23 linux.kernel
drwxr-xr-x. 2 root root 6 Jan  9 03:23 somefolder1
drwxr-xr-x. 2 root root 6 Jan  9 03:23 somefolder2
drwxr-xr-x. 2 root root 6 Jan  9 03:24 somefolder3
drwxr-xr-x. 2 root root 6 Jan  9 03:23 TheLinux
-rw-r--r--. 1 root root 0 Jan  9 03:23 The.Linux

# ls *linux*
linux.kernel

linuxkernel:

# find -maxdepth 1 -iname '*linux*'
./linuxkernel
./CentosLinuxOS
./TheLinux
./linux.kernel
./Centos-Linux-OS
./The.Linux

Problem solved.

Thanks ;)

suicidaleggroll 01-08-2016 04:08 PM

So you wanted a case insensitive search? I didn't see you mention anything about case earlier, and indeed none of your examples had any capital letters in the word "target", so it wouldn't have mattered anyway, but...

To do that in bash you would need to use "shopt -s nocaseglob". A quick google search finds this handy little function you can put in your startup scripts:
Code:

$ function i () {
> shopt -s nocaseglob; $*; shopt -u nocaseglob
> }
$ ls *jtweet*
ls: cannot access *jtweet*: No such file or directory
$ i ls *jtweet*
JTweet.pm  JTweet.pm~  JTweet2.pm  JTweet2.pm~

Stick "i" in front of the command and it makes any resulting globbing pattern case insensitive.

mangya 01-08-2016 04:18 PM

Quote:

Originally Posted by suicidaleggroll (Post 5475251)
So you wanted a case insensitive search? I didn't see you mention anything about case earlier, and indeed none of your examples had any capital letters in the word "target", so it wouldn't have mattered anyway, but...

To do that in bash you would need to use "shopt -s nocaseglob". A quick google search finds this handy little function you can put in your startup scripts:
Code:

$ function i () {
> shopt -s nocaseglob; $*; shopt -u nocaseglob
> }
$ ls *jtweet*
ls: cannot access *jtweet*: No such file or directory
$ i ls *jtweet*
JTweet.pm  JTweet.pm~  JTweet2.pm  JTweet2.pm~

Stick "i" in front of the command and it makes any resulting globbing pattern case insensitive.

It always surprises me how stupid I behave sometimes. I don't know why I inserted capital letters. Maybe I am sleepy, its 3:45 AM at my place. I better go to sleep. Just ignore capital letters in my example, you'll understand. I just wanted to know why *something* doesn't work. Got answer from TB0ne.

I wont bother you anymore.

Thanks for your patience. You were very helpful. :)


All times are GMT -5. The time now is 08:10 AM.