LinuxQuestions.org
Help answer threads with 0 replies.
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-01-2016, 01:03 PM   #1
hamatro
LQ Newbie
 
Registered: Feb 2016
Posts: 6

Rep: Reputation: Disabled
Wink [Shell Script] How can I do this recursive search in a directory?


I'm making a script that returns the file number of each file on a directory recursively, and the names of these files.

I did this but I don't know how to use find for the recursive search.

PHP Code:
#!/bin/bash

echo The directory is `pwd`:
file  -* | tr -" " |cut -d" " -f2 |sort uniq -
But unfortunately I only get the files in that directory and I need recursive search.

The final output must be this:

The classification of the files in the diretory XXXX is:
- There is/are X file/s with the extension/pdf: X1, X2... Xt
- There is/are Y file/s with the extension/jpeg: X1, X2... Xv
etc.

Thank youu for your help
 
Old 02-01-2016, 01:26 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Replace the "file -i *" part of the command with "find . -print0 | xargs -0 file -i", the rest can stay the same.

Your current script does not produce output matching your requirements, though. It also will break on files with spaces in the name.

Last edited by suicidaleggroll; 02-01-2016 at 01:29 PM.
 
Old 02-01-2016, 01:57 PM   #3
hamatro
LQ Newbie
 
Registered: Feb 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Replace the "file -i *" part of the command with "find . -print0 | xargs -0 file -i", the rest can stay the same.

Your current script does not produce output matching your requirements, though. It also will break on files with spaces in the name.
Thank your for your answer. I tried but doesn't work.

I was thinking to do like this but I don't know how can I do it:

  • A procedure that make a recursively list with the files and their types. Using Find and File (and surely while) commands.
  • One show only types. This will call to the last one to have a list of types and use cut and sort to take types and remove duplicates.
  • One show files of a particular type that receives as a parameter. It will use the first method, grep, cut and sort.

*Combining the last two procedures (along with other commands as while and wc).
 
Old 02-01-2016, 02:01 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by hamatro View Post
Thank your for your answer. I tried but doesn't work.
"doesn't work" how? What command are you running, what does it do, and why do you say it doesn't work?

What I posted will do exactly what your previous command does, but recursively. Is that not what you asked for?
 
Old 02-01-2016, 02:13 PM   #5
hamatro
LQ Newbie
 
Registered: Feb 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
"doesn't work" how? What command are you running, what does it do, and why do you say it doesn't work?

What I posted will do exactly what your previous command does, but recursively. Is that not what you asked for?

Ok. I didn't say anything lol... I copied it wrong.
 
Old 02-01-2016, 04:29 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by hamatro View Post
Ok. I didn't say anything lol... I copied it wrong.
me thinks he's not typing it right
this is what I got:

Code:
find ~/Pictures  -print0 | xargs -0 file -i| tr -s " "  | cut -d " " -f2 | sort | uniq -c
results:
Code:
 40 image/jpeg;
      8 image/png;
      3 image/x-xcf;
      1 inode/directory;

Last edited by BW-userx; 02-01-2016 at 04:30 PM.
 
Old 02-06-2016, 08:29 AM   #7
hamatro
LQ Newbie
 
Registered: Feb 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Any help?
 
Old 02-06-2016, 08:36 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
you've been given answers --- more input here besides what you just wrote... savy?
 
1 members found this post helpful.
Old 02-06-2016, 08:56 AM   #9
hamatro
LQ Newbie
 
Registered: Feb 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
you've been given answers --- more input here besides what you just wrote... savy?
Id like to have a method that make a recursively list with the files and their types, using Find and File (and surely while) commands.
Then, one that show only types, calling to the last one to have a list of types and use cut and sort to take types and remove duplicates.
After that one that show files of a particular type that receives as a parameter. It will use the first method, grep, cut and sort, combining the last two procedures (along with other commands as while and wc).

Sorry for my English, I'm from Spain.
 
Old 02-06-2016, 09:18 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by hamatro View Post
Id like to have a method that make a recursively list with the files and their types, using Find and File (and surely while) commands.
Then, one that show only types, calling to the last one to have a list of types and use cut and sort to take types and remove duplicates.
After that one that show files of a particular type that receives as a parameter. It will use the first method, grep, cut and sort, combining the last two procedures (along with other commands as while and wc).

Sorry for my English, I'm from Spain.
R U Taking Bash Scripting in School? Es esta tarea ???

to help you with your english:

Quote:
I'd like to have a method that creates a recursive list with the files and their types, using Find and File (and surely using a while loop ) commands.

Then, one that shows only file types, calling to the last one <-- what does the word one represent, the last file, or function call?


to have a list of types and use cut and sort to take the file types and remove duplicates. or can be said or written as such, and remove duplicate files. : for clarity.

but still that last statment calls for a question:

to remove duplicate files from the list or to remove the duplicate files off the hard drive themselves?


After that one (one being what?) that show files of a particular type that receives as <- remove a parameter.

After that process, then have it show files of a particular type that receives a parameter telling it to show only the matching type.


then you'd have to explain how is it going to receive this parameter. off the command line, or within the script itself. is it to be written to give the user a list to pick from, or does the user have to go into the script and modify it first himself? before going into this part of it,

It will use the first method, grep, cut and sort, combining the last two procedures (along with other commands as while and wc).

this last statment projects to may variables in what it is that actually needs to be done. furthermore, is this to be written all within one script?

is it to be put into functions then called from that, one after the other, or witten in a Procedural programming style?


Last edited by BW-userx; 02-06-2016 at 09:45 AM.
 
Old 02-06-2016, 09:30 AM   #11
hamatro
LQ Newbie
 
Registered: Feb 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
R U Taking Bash Scripting in School? Es esta tarea ???
I'm at the university, and no, this is not my tarea xD.

I'm studying for an exam, and teachers recommended us some exercises about bash scripts, not mandatory to do. I just want to have the highest mark.

Thank you anyway 😁
 
Old 02-06-2016, 09:51 AM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by hamatro View Post
I'm at the university, and no, this is not my tarea xD.

I'm studying for an exam, and teachers recommended us some exercises about bash scripts, not mandatory to do. I just want to have the highest mark.

Thank you anyway
another quicker way is to google it section by section in what it is you are wanting to remember so you can impress your teacher and hopefully get a better grade, but if you do not actully learn the how to's then it is still just a waste for your time, and mine. because for me to even try and figure it all out for you, one I have no use of this information, and two it will be very time consuming for me to do for you. maybe others know a quick way to do all of this in a short manner.

I'd try your luck googling how to's and reading up on the answers other give others in the other fourms all over the world and kept in this internet.

because if you are taking any kind of programming course it is all the same only the syntex is different. even with OOP

List files recursively

Last edited by BW-userx; 02-06-2016 at 10:03 AM.
 
  


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
Bash or Perl Script - recursive search. vinnat Linux - Newbie 1 10-06-2011 08:51 AM
[SOLVED] Grep Recursive search directory depth option? timdvtemp Linux - Software 1 02-28-2011 03:49 PM
[SOLVED] Shell script for recursive delete required. bikbjrt Linux - Newbie 2 07-01-2010 12:25 AM
Recursive directory search janel10 Linux - Newbie 2 08-26-2008 06:28 AM
recursive search of a string in a directory ?! realos Linux - Software 4 11-27-2002 04:49 AM

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

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