LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   listing folders recursivley (http://www.linuxquestions.org/questions/linux-newbie-8/listing-folders-recursivley-624729/)

jukebox55 02-29-2008 08:54 AM

listing folders recursivley
 
hello,

can anyone help with a problem im having. im trying to list all files of type .png on my system, but i cant seem to do it.

i thought this command would work, but it seems it does not
Code:

ls -R /*png

druuna 02-29-2008 08:59 AM

Hi,

In cases like this I tend to use the find command.

find . -type f -name "*png" should do the trick

This looks from the current dir (the dot ) for files (-type f) that end with png (-name "*png").

Hope this helps.

pixellany 02-29-2008 09:24 AM

ls -R will list recursively, beginning at the current directory

ls -R /dirname does the same thing, starting in /dirname---thus your command tries to find a directory at the root level whose name ends in png.

The find command will give you the location of the files. You can also do ls -R <dirname>|grep png. This will find all files (and folders) with png in the name.

druuna 02-29-2008 09:38 AM

Hi,

2 additions to pixellany's post:

1) The ls -R part works (recursive listing). ls -R *pnd doesn't work because the shell first expands *png and gives the output to ls -R.

2) ls -R | grep png has one (possible) drawback: It will only list the file names, not the dir(s) they reside in. If the path is also needed use find, otherwise use ls -R | grep "*png"

jukebox55 02-29-2008 10:06 AM

interesting, thanks for that information. i used

Code:

find / -name *png
now can i ask for further help, i would like to cp those .png files that were found, into a directory in my home folder, i tried this

Code:

find / -name *png | cp /home/user/foundpngdir
but that wouldnt work, so i tried this

Code:

cp /home/user/foundpngdir $(find / -name *png)
but this gives me this error

Quote:

cp:target 'found.png' is not a directory

druuna 02-29-2008 10:11 AM

Hi,

find / -type f -name "*png" -exec cp {} ~ \;

This will look for *png files, starting at the root dir. This part you probably recognized.
The -exec part copies all that is found (represented by the {} to your homedir (I'm lazy, so I used ~, /home/user/ is also allowed). The \; at the end is needed to close/end the commdn.

Hope this helps.

jukebox55 02-29-2008 10:37 AM

yes this works, thanks

Quote:

Originally Posted by druuna (Post 3073903)

find / -type f -name "*png" -exec cp {} ~ \;

out of interest, is '-exec' an option to the find command? and why does the *png need to be double quoted?

druuna 02-29-2008 11:49 AM

Hi,

-exec .... \; is indeed one of the many options that can be used with find. It makes it possible to use other linux/unix commands within the find command.

About the quotes. Officially you need to use quotes (single or double, whatever you need) and I always use them. This is also true for other commands. I have to admit that most of the time the commands will work if you don't use the quotes, but it is 'good style' to use them ;)

Hope this clears things up a bit.

jukebox55 02-29-2008 12:13 PM

yes, its interesting, thankyou :)


All times are GMT -5. The time now is 04:10 AM.