LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-08-2016, 01:06 PM   #1
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 84

Rep: Reputation: Disabled
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
 
Old 01-08-2016, 01:33 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,933

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by mangya View Post
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.
 
1 members found this post helpful.
Old 01-08-2016, 02:07 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
I would have gone with find also:
Code:
find . -maxdepth 1 -type f -name '*target*'
 
2 members found this post helpful.
Old 01-08-2016, 03:07 PM   #4
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 84

Original Poster
Rep: Reputation: Disabled
@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.
 
Old 01-08-2016, 03:38 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,933

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by mangya View Post
@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.
 
Old 01-08-2016, 04:16 PM   #6
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 84

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
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
 
Old 01-08-2016, 04:18 PM   #7
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,166
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
"next time" try
Code:
type ls
to see if it's aliased "funny".
 
Old 01-08-2016, 04:21 PM   #8
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 84

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
"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
 
Old 01-08-2016, 04:26 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by mangya View Post
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.
 
Old 01-08-2016, 04:37 PM   #10
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 84

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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.
 
Old 01-08-2016, 04:39 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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
 
Old 01-08-2016, 05:02 PM   #12
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 84

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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
 
Old 01-08-2016, 05:08 PM   #13
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
Old 01-08-2016, 05:18 PM   #14
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 84

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Globbing Lindner79 Linux - Newbie 2 09-17-2013 10:49 PM
[SOLVED] globbing in a script frrobert Programming 3 01-14-2011 04:27 PM
File Globbing BlaqkStarSunday Linux - Newbie 5 09-13-2010 07:35 PM
Need Help With File Globbing jcofell Linux - Newbie 10 10-26-2009 09:22 PM
how to enable globbing ravidangaych Linux - General 2 10-26-2009 11:11 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration