Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 17/18 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11th July 2012, 12:55 PM
agentsmith23 Offline
Registered User
 
Join Date: Jan 2006
Location: Lackland AFB, TX
Posts: 40
windows_xp_2003ie
How many file types are in a directory

I am not going to try to deceive any one this question is for a homework assignment, so if you're unwilling to help that is ok.

The issue is that I am trying to find out if there is a way to use the find utility to show how many different file types there are in a specific directory. The book I have actually gives the answer to even numbered questions and this one is answered but I want to know how to get to ha answer in the easiest way. The directory that is in question is /usr/bin and the answer the book gives is "Approximately 20". This directory has hundreds of files (I haven't looked at the exact number). Is there a simple way to find the answer or is it a matter of just using the find utility and manually scrolling through and counting the different file types?

Thanks in advance for any possible help.

Here is a link to the questions and I am working on number 10:
http://www.sobell.com/RH6/answers/05.util.ans.even.pdf
Reply With Quote
  #2  
Old 13th July 2012, 01:11 AM
Dutchy Offline
Registered User
 
Join Date: Aug 2011
Posts: 697
linuxfirefox
Re: How many file types are in a directory

I think you have misread the question.
They want you to do it with the "file" command and not the "find" command
I quickly hacked the following together which should do what they want:
PHP Code:
TYPES="$(uniq -c <<< "$(file -/usr/bin/* | cut -d, -f1 | cut -d: -f2 | sort)" | grep -v symbolic; uniq -cw 10 <<< "$(file -N /usr/bin/* | cut -d, -f1 | cut -d: -f2 | sort)" | grep symbolic | cut -d'`' -f1)"; paste <<< "$TYPES ..."; echo -en "\ndifferent types of files: "; echo "$TYPES" | wc -l 
Output:
PHP Code:
[Dutchy@Dutchy-PC ~]$ TYPES="$(uniq -c <<< "$(file -/usr/bin/* | cut -d, -f1 | cut -d: -f2 | sort)" | grep -v symbolic; uniq -cw 10 <<< "$(file -N /usr/bin/* | cut -d, -f1 | cut -d: -f2 | sort)" | grep symbolic | cut -d'`' -f1)"; paste <<< "$TYPES ..."; echo -en "\ndifferent types of files: "; echo "$TYPES" | wc -l
     30  Bourne-Again shell script
      1  C++ source
      6  ELF 32-bit LSB executable
   1477  ELF 64-bit LSB executable
     97  ELF 64-bit LSB shared object
      1  executable
      1  Palm OS dynamic library data "#!/bin/sh"
     86  Perl script
    263  POSIX shell script
    111  Python script
      2  setgid ELF 64-bit LSB executable
      1  setgid ELF 64-bit LSB shared object
      5  setgid executable
      3  setuid ELF 64-bit LSB executable
      9  setuid ELF 64-bit LSB shared object
      4  setuid executable
      1  setuid setgid ELF 64-bit LSB executable
    260  symbolic link to  ...

different types of files: 18 
Reply With Quote
  #3  
Old 13th July 2012, 01:49 AM
agentsmith23 Offline
Registered User
 
Join Date: Jan 2006
Location: Lackland AFB, TX
Posts: 40
windows_7chrome
Re: How many file types are in a directory

Wow that's awesome! And yes I did mean file not find...ooops

Thanks a lot, now I just need to spend some time dissecting what all of that does so i can fully understand it.
Reply With Quote
  #4  
Old 14th July 2012, 01:47 AM
Dutchy Offline
Registered User
 
Join Date: Aug 2011
Posts: 697
linuxfirefox
Re: How many file types are in a directory

Glad I could help you out a bit.
The code is quite a quick and dirty solution and might indeed not be easy to understand.
It actually consist out of 3 parts.

First it looks up the file types through the use of magic paterns of all the files in /usr/bin. Then it it cuts out everything behind the "," (comma) and then cuts out everything before the ":" (colon). The mess is nicely sorted so that uniq can make something of it. The output of this all is put into uniq which will look for duplicate lines and merge them so every line is unique, it will also count the duplicates. After that grep throws away all the lines with "symbolic" in it (because that are links to other files and do not play nice with "uniq").
PHP Code:
uniq -<<< "$(file -N /usr/bin/* | cut -d, -f1 | cut -d: -f2 | sort)" grep -v symbolic 
Now it will do the same thing again but this time the symbolic links are taken into account (the file output is unique for every symbolic link because it involves the location of the file it points to).
PHP Code:
uniq -cw 10 <<< "$(file -N /usr/bin/* | cut -d, -f1 | cut -d: -f2 | sort)" grep symbolic cut -d'`' -f1 
The past two commands are put into the $TYPES variable.
With the paste command the output of $TYPES gets some trailing dots (otherwise it would look a bit weird) and is printed to the screen.
PHP Code:
paste <<< "$TYPES ..." 
Then this will print a blank line (the -e enables the interpretation of special characters (in this case \n, newline) and the -n switch turns the trailing new line off (so the next command can be printed on the same line).
PHP Code:
echo -en "\ndifferent types of files: " 
Lastly the wc (word count) command counts the amount of lines in the $TYPES variable
PHP Code:
echo "$TYPESwc -
If you bump into trouble again and need some more info on bash you can take a look over here:
http://www.tldp.org/LDP/abs/html/index.html
Reply With Quote
  #5  
Old 14th July 2012, 07:29 AM
glennzo's Avatar
glennzo Online
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Posts: 13,929
linuxfirefox
Re: How many file types are in a directory

How would one expand on this to include subfolders of the specified path?
__________________
Glenn
The Bassinator © ®


Laptop: Toshiba Satellite / Intel Core 2 Duo 1.73 GHz / 2GB / 160GB / Intel Mobile 945GM/GMS/GME/943/940GML Integrated Graphics
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / 1TB SATA / 500GB SATA / EVGA GeForce 8400 GS 1GB
Reply With Quote
  #6  
Old 14th July 2012, 11:57 AM
george_toolan Offline
Registered User
 
Join Date: Dec 2006
Posts: 1,717
linuxfirefox
Re: How many file types are in a directory

Try the find command ;-)
Code:
TYPES="$(uniq -c <<< "$(find /usr/bin | file -N -f - | cut -d, -f1 | cut -d: -f2 | sort)" | grep -v symbolic; uniq -cw 10 <<< "$(find /usr/bin | file -N -f - | cut -d, -f1 | cut -d: -f2 | sort)" | grep symbolic | cut -d'`' -f1)"; paste <<< "$TYPES ..."; echo -en "\ndifferent types of files: "; echo "$TYPES" | wc -l
This will add another file type "directory".
Reply With Quote
  #7  
Old 16th July 2012, 12:58 AM
stevea's Avatar
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,300
linuxfirefox
Re: How many file types are in a directory

The OPs honesty is refreshing, pleasing and says something of character - welcome agentsmith23
Dutchy make masterful use of bash redirection - kudos - but let me play devil's advocate ....

The result falls short in several respect IMO. If the issue is 'file types' and not 'file metadata categories then we have a different problem than the one answered. 'symlinks' describe metadata, as do 'sticky', 'setuid' ,'setgid' . Also ", with very long lines" is pure baloneum and has no place in determining file type.

So something like ....

Code:
# file -bNL -e elf  /usr/bin/* | sed -e 's/setuid //;s/setgid //;s/sticky //;s/, with very long lines//' | sort | uniq -c
     33 Bourne-Again shell script, ASCII text executable
      5 Bourne-Again shell script, UTF-8 Unicode text executable
      1 ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux)
      4 ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV)
     30 ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux)
   1574 ELF 64-bit LSB executable, x86-64, version 1 (SYSV)
      1 ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux)
     94 ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)
      1 Palm OS dynamic library data "#!/bin/sh"
     78 Perl script, ASCII text executable
      1 Perl script, ISO-8859 text executable
      1 Perl script, UTF-8 Unicode text executable
    257 POSIX shell script, ASCII text executable
      1 POSIX shell script, ISO-8859 text executable
      9 POSIX shell script, UTF-8 Unicode text executable
     88 Python script, ASCII text executable
      7 Python script, UTF-8 Unicode text executable
'flle' certainly may misidentify 'Palm OS' libs 'C++ source' and likely makes poor distinctions between ASCII, ISO-8859, UTF-8 Unicode, but that's out of scope.

The OS ABI "version 1 (GNU/Linux)" vs "version 1 (SYSV)" is an important distinction of file type and should not be ignored.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe

Last edited by stevea; 16th July 2012 at 01:10 AM.
Reply With Quote
  #8  
Old 16th July 2012, 07:50 PM
Dutchy Offline
Registered User
 
Join Date: Aug 2011
Posts: 697
linuxfirefox
Re: How many file types are in a directory

That one looks indeed way more sane, is better thought through and last but not least it is a lot shorter too
Reply With Quote
  #9  
Old 16th July 2012, 08:15 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,103
linuxfirefox
Re: How many file types are in a directory

Guys - save a process... they are rare. If you are going to sort, why not use "sort -u" to eliminate duplicates?

granted, the uniq utility allows counting the number of duplicates.
Reply With Quote
  #10  
Old 17th July 2012, 06:10 PM
Gareth Jones Online
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 30
Posts: 1,707
linuxfirefox
Re: How many file types are in a directory

Depending how interested you are in the specifics of the types and context, you could simplify it further to something like:

Code:
$ file -b --mime /usr/bin/* | sort -u
application/x-executable; charset=binary
application/x-sharedlib; charset=binary
executable, regular file, no read permission
inode/symlink
inode/symlink; charset=binary
regular file, no read permission
text/plain; charset=utf-8
text/x-c++; charset=us-ascii
text/x-perl; charset=iso-8859-1
text/x-perl; charset=us-ascii
text/x-perl; charset=utf-8
text/x-python; charset=us-ascii
text/x-python; charset=utf-8
text/x-ruby; charset=us-ascii
text/x-shellscript; charset=iso-8859-1
text/x-shellscript; charset=us-ascii
text/x-shellscript; charset=utf-8
text/x-tex; charset=us-ascii
Reply With Quote
Reply

Tags
directory, file, types

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I open RAR file types? darksnow Using Fedora 10 10th November 2009 07:47 PM
File types Lost_in_Linux Linux Chat 1 19th May 2009 01:55 AM


Current GMT-time: 15:12 (Tuesday, 21-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat