Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

bash script to find file type

Posted on 2009-05-06
7
Medium Priority
?
1,687 Views
Last Modified: 2013-12-26
Hi, I need assistance creating a bash script which main purpose would be to search an specific mount directory /mnt/directories in which it will list the amount of files per directory and its file types as well a total global amount of files and total amount of file types. For example if there are 2 directories marc and john under /mnt/directories it will list their file type under each directory, file size per directory and then it will give the total amount of files for instance .doc or .xls 16 files .xls and 15 files .doc. Also it should provide the total amount of files added based on date per extension.
0
Comment
Question by:dpoper1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 60 total points
ID: 24319079

Nice homework assignment, what have you got?
 
Here are some hints:
 
man du
man ls
man awk
${file_name#*.}
 
0
 

Author Comment

by:dpoper1
ID: 24322480
ok first of all I would appreciate if you could tell me a way to use the find command to find the file type, this would really help.

Regards,

michael
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 135 total points
ID: 24323696
Hi,
what do you mean with "file type"? An extension like ".txt"?
If so, there are some things to consider -

- does every file have such an extension? If not, filter out those which don't!
- are there directories in the path containing a dot (.)? If yes, filter them out!
Use 'find' to find only files.  Use 'xargs' with 'basename' to get rid of displayed  directories (containing a dot or not). Use 'awk' to process only files with an extension (a dot in their name) and to print those extensions. Use 'sort' and 'uniq -c' to count.
Tell me how far you got with the above, and I'll help you further.
wmp
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Assisted Solution

by:ai_ja_nai
ai_ja_nai earned 60 total points
ID: 24325248
and use the command

file

with the target file as parameter to find out the file mime type. It works better than searching for extensions, which in Linux I can assure you that it makes absolutely no sense
0
 
LVL 14

Accepted Solution

by:
Monis Monther earned 120 total points
ID: 24330931
Try the following although it can be better done , bu this is just something really quick, you can craft it to give you a nice report and do more checks, just make use of the ideas here and make sure it runs correctly as I have not tried it.


#!/bin/bash

#This loop will go through all your dirs under /mnt
for x in $(ls /mnt)
do
#First we get total number of files for each dir
echo "Total number of files in directory $x is $(ls -t1 /mnt/$x | wc -l) files"

#Second thing is to get the size
echo "Directory $x is consuming $(du -sh /mnt/$x) of my disk space"

#this line will list all file types you have in your dir where x refers to the dir name and store them in a file #called types

ls -t1 /mnt/$x | cut -d . -f 2 | sort -u > types

for i in $(cat types)
do
echo "Number of files of type $i is $(ls -t1 /mnt/$x |grep $i |wc -l)"
done

#Dont forget to close the first loop
done
exit 0
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 135 total points
ID: 24331061
OK,

just for fun - this is a one-liner to produce a ranked list of your file extensions (only files with extensions taken into account) -

find /mnt -type f | xargs -n1 basename | awk -F"." '/\./ {print $NF}' | sort | uniq -c | sort -rn

To get a top list, e.g. top 20 -  add "| head -20" at the end.

(don't wonder at the 'basename", it's to process a "find . " command correctly)

Good luck,

wmp

0
 

Author Comment

by:dpoper1
ID: 24345483
My first step to this would be to list all of the files that will have the extensions found and then to find todays date.

I would appreciate your help.



#!/bin/bash
 
find /mnt -type f | xargs -n1 basename | awk -F"." '/\./ {print $NF}' | sort | uniq -c | sort -rn|cut -c 9-40>ext
 
while true
do
  cat ext| while read file
  do
    find / -name *.$file > ext1
  done
done

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question