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 10-14-2017, 08:22 AM   #1
bui89
LQ Newbie
 
Registered: Aug 2014
Posts: 3

Rep: Reputation: Disabled
Unhappy learning find command. confused


so I learn find command and decided to test it and find cron in etc directory. I use Ubuntu 17.04.

And I find out that I can't find all instances of cron in that directory as I can find using ls. why outputs are so different?

Code:
tux@tux-MM061:~$ find /etc -name "cron"
/etc/default/cron
/etc/init.d/cron
/etc/pam.d/cron
find: ‘/etc/ssl/private’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
tux@tux-MM061:~$ ls /etc -al | grep cron
-rw-r--r--   1 root root     401 დეკ 29  2014 anacrontab
drwxr-xr-x   2 root root    4096 აგვ  1 15:33 cron.d
drwxr-xr-x   2 root root    4096 ოქტ  2 07:12 cron.daily
drwxr-xr-x   2 root root    4096 აგვ  1 15:19 cron.hourly
drwxr-xr-x   2 root root    4096 აგვ  1 15:33 cron.monthly
-rw-r--r--   1 root root     722 აპრ  6  2016 crontab
drwxr-xr-x   2 root root    4096 ოქტ  2 07:12 cron.weekly
 
Old 10-14-2017, 08:25 AM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,627

Rep: Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198
Welcome to LQ!

Try adding wildcards to your find command.
Code:
find /etc -name "*cron*"
 
1 members found this post helpful.
Old 10-14-2017, 08:30 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,607
Blog Entries: 3

Rep: Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851
You're searching for the exact string "cron" as the name. If you want to find all files with "cron" in the name, then you'll have to use a pattern. See the manual for find in the section for "-name" for the details.

Code:
man find
man 7 glob
man 7 regex

find /etc/ -name '*cron*' -print
find /etc/ -type d -name '*cron*' -print
find /etc/ -type f -name '*cron*' -print
You can also get rather complex patterns rolling with -regex instead of -name.
 
Old 10-14-2017, 08:30 AM   #4
bui89
LQ Newbie
 
Registered: Aug 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
allend
thank you for your reply. It works better now.

One more question please. Are there any parameters for find to not searching in sub directories?

Thanks in advance
 
Old 10-14-2017, 08:32 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,607
Blog Entries: 3

Rep: Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851
Yes. You can limit the depth with -maxdepth. It has to come next after the path before the other options.

Code:
find /etc/ -maxdepth 1 -name '*cron*' -print
Again, see the reference manual using "man find"
 
1 members found this post helpful.
Old 10-14-2017, 08:34 AM   #6
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,627

Rep: Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198
Yes.
Run 'man find' and look at the -maxdepth option. But if you do not want to look in subdirectories, then you are better off using ls.
 
1 members found this post helpful.
Old 10-14-2017, 08:35 AM   #7
bui89
LQ Newbie
 
Registered: Aug 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Yes. You can limit the depth with -maxdepth. It has to come next after the path before the other options.

Code:
find /etc/ -maxdepth 1 -name '*cron*' -print
Again, see the reference manual using "man find"
Thats great. It worked like a charm. Though I don't know what the -print does.
 
Old 10-14-2017, 08:40 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,607
Blog Entries: 3

Rep: Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851
The -print is implied in some versions of find but it is good to include it so as not to take it for granted. You could just as easily use -print0 for when you encounter nasty file names or -printf when you want specific information or a particular structure to that information. Other popular conclusions to a search are -exec \; or -exec + or even -delete.

One thing to remember is that there is an implied -and between the options you use. If you explicitly add an -or then you'll also have to pay attention to precedence.

Code:
find /var/www/site1/htmldocs/ -type f \( -name '*.html' -o -name '*.css' \) -print
 
  


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
Confused about learning iptables recent module veeruk101 Linux - Newbie 1 11-01-2011 11:22 AM
learning java input output: tutorials for a confused student sought! titanium_geek Programming 9 12-10-2008 11:27 PM
Confused by command "find" chen666 Linux - Newbie 4 11-11-2006 05:43 PM
compiling error : compiler cannot find something I have :confused: itz2000 Linux - Newbie 7 05-09-2006 08:57 AM
Learning Exercises - Where can I find them? BenChase Slackware 3 12-15-2003 07:45 AM

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

All times are GMT -5. The time now is 05:20 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
Open Source Consulting | Domain Registration