Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
@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.
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
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.
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.
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
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.