Results 1 to 7 of 7
Hello everyone, I am creating a bash script for a beginners scripting project. I need to find the sizes of all the directories and then print showing the size and ...
- 10-29-2010 #1Just Joined!
- Join Date
- Oct 2010
- Posts
- 3
Beginner Bash Script, Need Help! Directory sizes
Hello everyone, I am creating a bash script for a beginners scripting project. I need to find the sizes of all the directories and then print showing the size and the directory, but only for one level. Sample output if using the home directory:
345280 ./Desktop
11376 ./Documents
83124 ./Downloads
4 ./Music
8 ./Pictures
4 ./Public
I had a command that helped me do this :
ls -d */ | xargs du -ks
When I created my script i tried to use this, but it told me xargs was not a found command.
So far the script that I have is:
If anyone could help me I would appreciate it greatly. I don't know if you are not allowed to use xargs in a script or if I am not putting it right.Code:#! /bin/bash #Script to calculate the size of the Directories and their subdirectories in a given path #but only display the Directory and its size. # Can take in 1 argument which is a given path. If no argument is given, Path is current directory. case $1 in '') PATH=`pwd`;; *) PATH=$1;; esac echo $PATH if [ ! -d $1 ] then echo $1 Not a directory, Exiting Script exit fi "side not" I tried put the ls command here.
When I tried to use just an ls command it told me that ls was not a found command. I had to use /bin/ls
- 10-30-2010 #2
Hello and Welcome.
This is just a guess, since I suck at programming. Try using the full path to xargs, like you did with ls command.I do not respond to private messages asking for Linux help, Please keep it on the forums only.
All new users please read this.** Forum FAQS. ** Adopt an unanswered post.
- 10-30-2010 #3Just Joined!
- Join Date
- Oct 2010
- Posts
- 3
Thank you for responding. I was trying to do that, but I couldn't find where the path is. I plan to look more into this. Hopefully this is the problem since its the same error that ls was giving me.
- 10-30-2010 #4
Locate the path with the whereis command
Use the man pages alsoCode:whereis xargs
Code:man whereis
Code:man xargs
I do not respond to private messages asking for Linux help, Please keep it on the forums only.
All new users please read this.** Forum FAQS. ** Adopt an unanswered post.
- 10-31-2010 #5
Why don't you just use:
It does exactly what you are trying to do with your script.Code:du --max-depth=1
- 10-31-2010 #6Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
This appears to be homework, so I will give you a hint only. The variable name PATH is special:
Best wishes on your project ... cheers, drlCode:PATH The search path for commands. It is a colon-separated list of directories in which the shell looks for commands (see COMMAND EXECUTION below). A zero-length (null) directory name in the value of PATH indicates the current directory. A null directory name may appear as two adjacent colons, or as an initial or trailing colon. The default path is system-dependent, and is set by the administrator who installs bash. A common value is ``/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin''. -- excerpt from man bashWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 11-01-2010 #7Just Joined!
- Join Date
- Oct 2010
- Posts
- 3
Thanks to all
Hello again, I was able to complete the assignment. I remembered the whereis command a little while after posting. I don't know if I should start a new thread for this or post it here, but I had two questions regarding some of the code.
All of the commands in the Bash script worked fine without their full path being written. ls, wc, du. However at the beginning of my code I have a "case ? in ?" statement. When I add this case statement to my script is when I have to start using /bin/ls, /usr/bin/wc and so on. Can anyone tell me why this is and what is happening. ?
My second question is a line in my script that I use to find if any subdirectories exist if a different directory was sent into the script as a parameter.
I use it in an if statement.
if [ ls -F -1 | grep "/" | wc -l = 0 ]
then
echo No subs
fi
I was wondering if anyone could provide an explanation of what the code is doing? I got it off the net and think I understand it.
It is using ls -F -1 to format the ls output into one column. Then it is using grep to find the ending / on the names of the directories. After that it is using wc -l to check for any newline characters and if that total is zero then there are no subdirectories.
Can anyone tell me if this is right or explain whats happening?
Thanks again in advance.


Reply With Quote