Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to

  1. find specific folders
  2. echo their name
  3. ls -l their contents

My problem is that I don't know how to combine an echo {} and an ls -l {} in one find command.

line for echo:

sudo find / -type d -name 'yt' -exec echo {} \;

line for ls:

sudo find / -type d -name 'yt' -exec ls -l {} \;

How can I combine these two?

note: I prefer not to use xargs if possible, as it has problems with escaping filenames properly.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Why not just put two different -exec options in the same find command?

This works for me:

$ find --version
find (GNU findutils) 4.4.2
$ find . -type d -name mydir -exec echo {} \; -exec ls -l {} \;
./mydir
total 0
-rw-rw-r-- 1 daniel daniel 0 févr. 25 15:32 myfile
share|improve this answer
    
thanks, i didn't know it's possible –  zsero Feb 25 at 15:11

This is not generic solution but will work for you since you search for directories:

sudo find / -type d -name 'yt' -execdir readlink -f {} \; -exec ls -l {} \;

The execdir executes a command on the subdirectory matching the found file (which, since you search for the directories) is the directory itself (and readlink -f gives it's full path). The second -exec will on the other side match the directory again and do the "ls -" on them.

Using jsut echo on the first execdir wouldn't write you the full path but just the last directory which may not be enough.

share|improve this answer

You can use the print command here easily.

$sudo find / -type d -name 'bin' -print  -exec ls -l {} \;

For example,

  1. find bin folders
  2. echo their name or print their names
  3. ls -l their contents - list their contents.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.