LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-29-2008, 07:54 AM   #1
jukebox55
Member
 
Registered: Aug 2007
Distribution: slackware 11
Posts: 101

Rep: Reputation: 15
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
 
Old 02-29-2008, 07:59 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 02-29-2008, 08:24 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 02-29-2008, 08:38 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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"
 
Old 02-29-2008, 09:06 AM   #5
jukebox55
Member
 
Registered: Aug 2007
Distribution: slackware 11
Posts: 101

Original Poster
Rep: Reputation: 15
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
 
Old 02-29-2008, 09:11 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 02-29-2008, 09:37 AM   #7
jukebox55
Member
 
Registered: Aug 2007
Distribution: slackware 11
Posts: 101

Original Poster
Rep: Reputation: 15
yes this works, thanks

Quote:
Originally Posted by druuna View Post

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?
 
Old 02-29-2008, 10:49 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 02-29-2008, 11:13 AM   #9
jukebox55
Member
 
Registered: Aug 2007
Distribution: slackware 11
Posts: 101

Original Poster
Rep: Reputation: 15
yes, its interesting, thankyou
 
  


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
change permissions of all folders and sub folders cad Linux - General 3 01-28-2007 10:21 AM
symlinks from folders to folders? vbsaltydog Linux - Newbie 4 04-03-2006 01:51 PM
Samba can create new files and folders but access denied in any new folders k.king Linux - Networking 2 01-15-2006 06:14 AM
proftpd not listing files and folders Robin01 Linux - Software 2 12-29-2004 09:55 AM
listing of programs squeaks_27 Slackware 4 02-04-2003 01:46 PM

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

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