LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find files with the same name under a directory using ls (https://www.linuxquestions.org/questions/linux-newbie-8/find-files-with-the-same-name-under-a-directory-using-ls-4175442902/)

sijoune 12-25-2012 11:03 AM

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


colucix 12-25-2012 11:13 AM

Hi and welcome to LinuxQuestions!

Why don't you try find instead of struggling with ls?
Code:

find /etc/php5 -name suhosin.ini

sijoune 12-25-2012 11:16 AM

Because i get only the first one it finds:
Code:

# find /etc/php5 -name suhosin.ini
/etc/php5/conf.d/suhosin.ini


colucix 12-25-2012 11:54 AM

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

sijoune 12-25-2012 11:56 AM

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


colucix 12-25-2012 12:07 PM

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.

sijoune 12-25-2012 12:12 PM

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:~#


sijoune 12-25-2012 12:16 PM

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...

colucix 12-25-2012 12:21 PM

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.

colucix 12-25-2012 12:24 PM

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.

sijoune 12-25-2012 01:23 PM

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! :)

colucix 12-25-2012 01:27 PM

You're welcome! :)

David the H. 12-26-2012 12:25 PM

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.


All times are GMT -5. The time now is 04:17 PM.