Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a bash shell script that will take an input directory and give me the name and size (with units) within. I'm a bit new to shell scripts, so I've been able to build the script to take the argument and assure it is a single directory, but I'm stuck after the ls command. I want to simplify it down to just file name and size.

share|improve this question

closed as unclear what you're asking by muru, Scott, cuonglm, Anthon, dr01 Feb 17 at 8:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
You should probably include your script as it is now – smokes2345 Feb 17 at 2:41
    
you can pipe the output of ls -l to awk {'print $9 , $5'}, $9 is the name field and $5 is the size field. Or instead of using ls try du -sh /tmp/*. It will give you the size and name of all the contents of the directory – Piyush Jain Feb 17 at 3:00

Try below code

echo "\t Enter a directory name :\c"
read dirName
if [ ! -d $dirName ]; then
    echo "Not a valid directory "
else
   du -ksh $dirName/*
fi

if you want to use only 'ls' command output, then try the way suggested by piyush.

i.e. instead of

du -ksh $dirName/* 

use

echo " size |  name"
ls -lh $dirName | awk '{print $5 "|" $9}'

option 'h'(du -ksh, ls -lh) will convert the size to 'human readable' formats(KB,MB,GB).

share|improve this answer
    
Thanks for all the suggestions! I'll try out the "ls-lh" option. That simplifies the conversions a lot. Thanks for being patient with me, I appreciate the help. –  A Pompeii Feb 17 at 5:27
    
if this answer solves your question, then click on 'accept answer'. If you are facing any other issue, just comment it out. – Raju Feb 17 at 5:30
    
I'm trying to use " item=$(ls -l $@)" to store the output as a shell variable, but for some reason when I call the variable, say echo "$item", it simply prints the directory path instead of the output of the ls-l command on that directory. Is this a syntax error? –  A Pompeii Feb 17 at 5:36
    
@A Pompeii, if you are intending to redirect ls command output to a variable, try " ` " i.e. <code> item=ls -l $@ <code> – Raju Feb 17 at 5:42
    
item=`ls -l $@` – Raju Feb 17 at 5:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.