LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-25-2012, 11:03 AM   #1
sijoune
LQ Newbie
 
Registered: Dec 2012
Posts: 6

Rep: Reputation: Disabled
Find files with the same name under a directory using ls


I want to list all suhosin.ini files under /etc/php5/ directory. The thing is that the 3 of them are one level deeper in the directory tree. I want to combine these two to one command

Code:
# ls  /etc/php5/*/*/suhosin.ini
/etc/php5/apache2/conf.d/suhosin.ini  /etc/php5/cgi/conf.d/suhosin.ini  /etc/php5/cli/conf.d/suhosin.ini
Code:
# ls  /etc/php5/*/suhosin.ini
/etc/php5/conf.d/suhosin.ini
I tried to use ls /etc/php5/ -R and pipe it to grep but i couldnt do what i want which is get a list like that

Code:
/etc/php5/apache2/conf.d/suhosin.ini
/etc/php5/cgi/conf.d/suhosin.ini
/etc/php5/cli/conf.d/suhosin.ini
/etc/php5/conf.d/suhosin.ini
 
Old 12-25-2012, 11:13 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi and welcome to LinuxQuestions!

Why don't you try find instead of struggling with ls?
Code:
find /etc/php5 -name suhosin.ini
 
Old 12-25-2012, 11:16 AM   #3
sijoune
LQ Newbie
 
Registered: Dec 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Because i get only the first one it finds:
Code:
# find /etc/php5 -name suhosin.ini
/etc/php5/conf.d/suhosin.ini
 
Old 12-25-2012, 11:54 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Really strange! Please, can you post the output of the following?
Code:
ls -l /etc/php5/apache2/conf.d/suhosin.ini /etc/php5/cgi/conf.d/suhosin.ini /etc/php5/cli/conf.d/suhosin.ini /etc/php5/conf.d/suhosin.ini
 
Old 12-25-2012, 11:56 AM   #5
sijoune
LQ Newbie
 
Registered: Dec 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Here it is:

Code:
root@vps1:~# ls -l /etc/php5/apache2/conf.d/suhosin.ini /etc/php5/cgi/conf.d/suhosin.ini /etc/php5/cli/conf.d/suhosin.ini /etc/php5/conf.d/suhosin.ini
-rw-r--r-- 1 root root 3654 Δεκ  25 20:34 /etc/php5/apache2/conf.d/suhosin.ini
-rw-r--r-- 1 root root 3654 Δεκ  25 20:34 /etc/php5/cgi/conf.d/suhosin.ini
-rw-r--r-- 1 root root 3654 Δεκ  25 20:34 /etc/php5/cli/conf.d/suhosin.ini
-rw-r--r-- 1 root root 3654 Δεκ  25 20:34 /etc/php5/conf.d/suhosin.ini
 
Old 12-25-2012, 12:07 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nothing strange. What if you do the following?
Code:
find /etc/php5 -mindepth 3 -name suhosin.ini
It should find only the three "missing" files.
 
Old 12-25-2012, 12:12 PM   #7
sijoune
LQ Newbie
 
Registered: Dec 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
i thought about it but strangely for mindepth>2 i get nothing!

Code:
root@vps1:~# find /etc/php5 -mindepth 1 -name suhosin.ini
/etc/php5/conf.d/suhosin.ini
root@vps1:~# find /etc/php5 -mindepth 2 -name suhosin.ini
/etc/php5/conf.d/suhosin.ini
root@vps1:~# find /etc/php5 -mindepth 3 -name suhosin.ini
root@vps1:~# find /etc/php5 -mindepth 4 -name suhosin.ini
root@vps1:~# find /etc/php5 -mindepth 5 -name suhosin.ini
root@vps1:~#
 
Old 12-25-2012, 12:16 PM   #8
sijoune
LQ Newbie
 
Registered: Dec 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
I think i fount out why this is happening.

Code:
root@vps1:~# ls /etc/php5/ -Rl
/etc/php5/:
total 16
drwxr-xr-x 2 root root 4096 Δεκ  23 17:13 apache2
drwxr-xr-x 2 root root 4096 Δεκ  23 17:13 cgi
drwxr-xr-x 2 root root 4096 Δεκ  23 17:13 cli
drwxr-xr-x 2 root root 4096 Δεκ  23 17:41 conf.d

/etc/php5/apache2:
total 68
lrwxrwxrwx 1 root root     9 Σεπ  12 23:13 conf.d -> ../conf.d
-rw-r--r-- 1 root root 68428 Σεπ  12 23:12 php.ini

/etc/php5/cgi:
total 68
lrwxrwxrwx 1 root root     9 Σεπ  12 23:13 conf.d -> ../conf.d
-rw-r--r-- 1 root root 68428 Σεπ  12 23:12 php.ini

/etc/php5/cli:
total 68
lrwxrwxrwx 1 root root     9 Σεπ  12 23:13 conf.d -> ../conf.d
-rw-r--r-- 1 root root 68105 Σεπ  12 23:12 php.ini

/etc/php5/conf.d:
total 44
-rw-r--r-- 1 root root   54 Σεπ  12 23:13 curl.ini
-rw-r--r-- 1 root root   50 Σεπ  12 23:13 gd.ini
-rw-r--r-- 1 root root   54 Σεπ  20  2011 imap.ini
-rw-r--r-- 1 root root   58 Φεβ  22  2011 mcrypt.ini
-rw-r--r-- 1 root root   57 Σεπ  12 23:13 mysqli.ini
-rw-r--r-- 1 root root   56 Σεπ  12 23:13 mysql.ini
-rw-r--r-- 1 root root   52 Σεπ  12 23:13 pdo.ini
-rw-r--r-- 1 root root   60 Σεπ  12 23:13 pdo_mysql.ini
-rw-r--r-- 1 root root 3654 Δεκ  25 20:34 suhosin.ini
-rw-r--r-- 1 root root   59 Σεπ  12 23:13 xmlrpc.ini
-rw-r--r-- 1 root root   52 Σεπ  12 23:13 xsl.ini
the conf.d's are symlinks! I feel so stupid...
Sorry for the trouble...

Last edited by sijoune; 12-25-2012 at 12:19 PM.
 
Old 12-25-2012, 12:21 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This puzzles me! There are no hard or symbolic links and they all seem regular and existing files. Which version of find (coreutils) is this and which system are you running on?

In the meanwhile, to answer to the last part of your original post, you can get the desired result by running
Code:
ls -R /etc/php5/ | awk '/^suhosin.ini$/{sub(/:/,"/",dir);print dir $1}{dir=$1}'
This is just a workaround and doesn't solve the real issue, that may cause problems in the future.
 
Old 12-25-2012, 12:24 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well done! I was focused on the files and didn't thnk about the containers as symbolic links. So the command
Code:
find -L /etc/php5 -name suhosin.ini
should reveal them all.
 
1 members found this post helpful.
Old 12-25-2012, 01:23 PM   #11
sijoune
LQ Newbie
 
Registered: Dec 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
I forgot to post the result.

Code:
root@vps1:~# find -L /etc/php5 -name suhosin.ini
/etc/php5/cli/conf.d/suhosin.ini
/etc/php5/apache2/conf.d/suhosin.ini
/etc/php5/conf.d/suhosin.ini
/etc/php5/cgi/conf.d/suhosin.ini
Thanks colucix!
 
Old 12-25-2012, 01:27 PM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome!
 
Old 12-26-2012, 12:25 PM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
But did you notice that the symlinks are all "../conf.d"?

This means that all the links point back to the same directory, and so there's only a single suhosin.ini file. It's just available through multiple paths.

Of course, if what you want is a list of all the paths, then great!

Speaking of which, with bash's new globstar feature (v.4+), you should be able to do this with a simple globbing pattern. I just mocked up the above directory tree and ran a test:

Code:
$ shopt -s globstar
$ printf "$PWD/%s\n" **/suhosin.ini
/home/david/testdir/apache2/conf.d/suhosin.ini
/home/david/testdir/cgi/conf.d/suhosin.ini
/home/david/testdir/cli/conf.d/suhosin.ini
/home/david/testdir/conf.d/suhosin.ini
The **/ does recursive matching of all subdirectories. You do have to be a bit cautious with it though. Since it always follows directory symlinks like this, it can get stuck in eternal loops if there are any self-referencing ones.
 
1 members found this post 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
Find directory older than x and mv directory with sub files command ajhart Linux - Newbie 4 07-15-2011 05:24 AM
find files with pattern, then copy those files to another directory ncsuapex Programming 4 08-13-2010 03:38 PM
find the files in a directory ust Linux - Newbie 11 12-03-2008 12:24 AM
Listing files in a directory without ls and find? szahri Linux - Newbie 11 03-23-2007 03:12 AM
How to find the date for all the files in a directory Uday123 AIX 6 02-23-2006 08:26 PM

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

All times are GMT -5. The time now is 07:51 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