Search for file and size subdirectory as well


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for file and size subdirectory as well
# 1  
Old 02-03-2011
Search for file and size subdirectory as well

Hi I made this code to search in directory for file and size
How can I remodel it to seach in the sub direcotry as well
Thanks
Code:
#!/bin/bash
echo -n "Enter: "
 read var
if [ -d "${var}" ]
then
     echo "Directory exists: ${var}"
    size=`du -hs "${var}"`
    echo The size of the current folder is $size
else
     echo $var directory not found
fi

# 2  
Old 02-03-2011
Quote:
Originally Posted by lio123
Hi I made this code to search in directory for file and size
How can I remodel it to seach in the sub directory as well
Thanks
Try This:
Code:
sunt2000/user777$ cat /tmp/check.sh
#!/bin/ksh
echo "Directory to Check Sizes: \c" ; read var
if [ -d "${var}" ] ; then
        echo "Directory exists: ${var}"
        echo "The size of the folders are:"
        du -hs ${var} ${var}/*
else
        echo "Directory not found: ${var}"
fi

The output looks like this:
Code:
sunt2000/user777$ /tmp/check.sh   
Directory to Check Sizes: /expoprt/home
Directory not found: /expoprt/home

sunt2000/user777$ /tmp/check.sh   
Directory to Check Sizes: /export/home
Directory exists: /export/home
The size of the folders are:
  43G   /export/home
   2K   /export/home/user1
   2K   /export/home/user2
   2K   /export/home/user3
  14K   /export/home/user4
   2K   /export/home/user5
...

# 3  
Old 02-03-2011
Thank you,I want to check for single file and size in sertain subdirectory and directory
# 4  
Old 02-03-2011
Quote:
Originally Posted by lio123
Thank you,I want to check for single file and size in sertain subdirectory and directory
I am sorry, I don't quite understand what you are looking for.

Let's take this directory example where there is a certain file (test.out) in a certain directory (/tmp) and in a certain subdirectory (subdir):
Code:
/tmp
/tmp/check.sh
/tmp/subdir/test.out
/tmp/(various other files)

sunt2000/user777$ cat /tmp/check.sh
   4416049 drwxrwxrwt   5 root     sys         3615 Feb  3 18:52 /tmp
  85437260 -rwxrwxrwx   1 root     root         326 Feb  3 18:44 /tmp/check.sh
 104549276 drwxr-xr-x   2 root     root         182 Feb  3 18:50 /tmp/subdir
  88062983 -rw-r--r--   1 root     root        3522 Feb  3 18:50 /tmp/subdir/test.out

Is this what you mean?
Code:
sunt2000/user777$ cat /tmp/check.sh
#!/bin/ksh
echo "Directory or File to Check Size: \c" ; read var
if [ -d "${var}" ] ; then
        echo "Directory exists: ${var}"
        echo "The size is:"
        du -hs ${var}
elif [  -f "${var}" ] ; then
        echo "File exists: ${var}"
        echo "The size is:"
        du -hs ${var}
else
        echo "Directory or File not found: ${var}"
fi

sunt2000/user777$ /tmp/check.sh   
Directory to Check Sizes: /expoprt/home
Directory of File not found: /expoprt/home

sunt2000/user777$ /tmp/check.sh   
Directory or File to Check Size: /tmp
Directory exists: /tmp
The size is:
 180M   /tmp

sunt2000/user777$ /tmp/check.sh   
 File exists: /tmp/check.sh
The size is:
   8K   /tmp/check.sh

sunt2000/user777$ /tmp/check.sh   
 Directory or File to Check Size: /tmp/subdir
Directory exists: /tmp/subdir
The size is:
  16K   /tmp/subdir

sunt2000/user777$ /tmp/check.sh   
 Directory or File to Check Size: /tmp/subdir/test.out
File exists: /tmp/subdir/test.out
The size is:
   8K   /tmp/subdir/test.out

If this is not what you are looking for, can you provide an example directory structure and identify the file you are looking to get a size of?
# 5  
Old 02-03-2011
Code:
echo -n "Enter: "
read var

find . -type d -name "$var" -exec du -hs {} \;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to sort the files by size and based subdirectory un UNIX?

I have the below input data in a file and need to get the output as mentioned below. Need to sort the data by size(Asc/des)/by subdirectory Below is the input which is there in a file: 120 /root/path2/part-00000-d3700305-428d-4b13-8161-42051f4ac5ed-c000.json 532 ... (3 Replies)
Discussion started by: ajarramuk
3 Replies

2. UNIX for Beginners Questions & Answers

Reducing input file size after pattern search

I have a very large file with millions of entries identified by @M. I am using the following script to "extract" entries based on specific strings/patterns: #!/bin/bash if ] then file=$1 else echo "Input_file passed as an argument $1 is NOT found." exit; fi MID=(NULL "string-1"... (10 Replies)
Discussion started by: Xterra
10 Replies

3. Shell Programming and Scripting

Search for specific file type in subdirectory with multiple folders

I have a directory that is in the below order (the --- is not part of the directory tree, only there to help illustrate: DATE --- main level Folder1 --- level under DATE plugin_out --- level under Folder1 variantCaller_out.40 --- level under plugin_out 001,002,003 --- level under... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Optimised way for search & replace a value on one line in a very huge file (File Size is 24 GB).

Hi Experts, I had to edit (a particular value) in header line of a very huge file so for that i wanted to search & replace a particular value on a file which was of 24 GB in Size. I managed to do it but it took long time to complete. Can anyone please tell me how can we do it in a optimised... (7 Replies)
Discussion started by: manishkomar007
7 Replies

5. Shell Programming and Scripting

Sum of file size in directory / subdirectory

Hi , I am trying to write something to find the size of particular type of files in a directory & it's subdirectory and sum the size .. These types of file are found at directory level or its subdirectories level .. #!/bin/ksh FNAME='.pdf' S_PATH=/abc/def/xyz find $S_PATH -exec ls -lad... (4 Replies)
Discussion started by: Vaddadi
4 Replies

6. Shell Programming and Scripting

Find files of specific size excluding search in a subdirectory

Hi All, I was exploring find command and came across -prune option which would exclude search in a mention subdirectory. My quesry is to search all files more that 100 MB size but exclude search in a subdirectory. I am using below command,but somehow it is not working. Can anybody help me... (6 Replies)
Discussion started by: usha rao
6 Replies

7. Shell Programming and Scripting

search text in files within the subdirectory

Hello, I am trying to write a shell script to search for a pattern in the directory and show only one entry for each field, essentially I am looking to search for a pattern in a file and list that file name. (1 Reply)
Discussion started by: grajp002
1 Replies

8. Solaris

Search for big file size only in root fs

Hi all, I'm working on Solaris and quite often I receive the alert message of file system at 90%. I'd like to find which files caused this happens (at least the biggest files) with the following command: find . -size +10000000c -exec ls -larth {} \; This looks for every file in every fs... (4 Replies)
Discussion started by: Evan
4 Replies

9. Shell Programming and Scripting

Search through subfolders and move them into separate folder on the base of file size

Hello guys I am sure that you will help me on this issue as you did earlier::) Scenario : I have a folder named "XYZ". It consist many sub-folders and subfolder contain severals files. there may be abc.dat in each subfolder. Now i want to seperate subfolders on follwing conditions- if abc.dat... (12 Replies)
Discussion started by: infiant
12 Replies

10. Shell Programming and Scripting

How to calculate file's size in directory and subdirectory

Hi, I have written one script to calculate total space of all file in one directory, ignoring subdirectory, it works fine. Now, I've been trying to calculate all files which includes files in any subdirectories. I use recursive function to do this, but it can work only if there is only one... (4 Replies)
Discussion started by: KLL
4 Replies
Login or Register to Ask a Question