LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 12:06 PM   #1
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

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, 12:33 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
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, 01:07 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 02:07 PM   #4
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

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, 02:38 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
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, 03:16 PM   #6
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

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, 03:18 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

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

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, 03:26 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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, 03:37 PM   #10
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

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, 03:39 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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, 04:02 PM   #12
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

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, 04:08 PM   #13
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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, 04:18 PM   #14
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

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



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 09:49 PM
[SOLVED] globbing in a script frrobert Programming 3 01-14-2011 03:27 PM
File Globbing BlaqkStarSunday Linux - Newbie 5 09-13-2010 06:35 PM
Need Help With File Globbing jcofell Linux - Newbie 10 10-26-2009 08:22 PM
how to enable globbing ravidangaych Linux - General 2 10-26-2009 10:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:31 PM.

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
Open Source Consulting | Domain Registration