LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-12-2008, 11:18 AM   #1
akeenabawa
LQ Newbie
 
Registered: Jul 2008
Distribution: Fedora Core 2, Ubuntu 8
Posts: 13

Rep: Reputation: 0
Question Display by File Name, File Size, and File Owner using ls


I am trying to sort by single line the files by name, size and owner.
So far I have this:
Code:
if [ -d $1 ]; then
echo "Processing `pwd'..."
cd $1
ls -1
cd
else
echo "$1 is not a valid directory"
fi
If I add -s after the -1 it will display the size but in front of the name.

Can someone help me out please?
 
Old 08-12-2008, 07:27 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
What you need is the long list cmd

ls -l which is normally aliased to ll in Linux, then get the fields you want in that order

Code:
ll|awk '{print $8 " " $5 " " $3}'
then you need to read the man page for sort.

Last edited by chrism01; 08-14-2008 at 08:05 PM.
 
Old 08-13-2008, 10:59 AM   #3
akeenabawa
LQ Newbie
 
Registered: Jul 2008
Distribution: Fedora Core 2, Ubuntu 8
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks for the help. That works quite well on it's own from the command line but not when I run the script. I had to modify it to this:
Code:
ls -l --color | awk '{print $9 " " $5 " " $3}'
 
Old 08-13-2008, 07:44 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Could you explain/show code/errs? Why would you need --color??
 
Old 08-14-2008, 07:34 AM   #5
akeenabawa
LQ Newbie
 
Registered: Jul 2008
Distribution: Fedora Core 2, Ubuntu 8
Posts: 13

Original Poster
Rep: Reputation: 0
The error I get with the ll command is:
line 21: ll: command not found

I don't need the color, I just added it so I know what kind of file it is or if it's a directory.
 
Old 08-14-2008, 02:33 PM   #6
akeenabawa
LQ Newbie
 
Registered: Jul 2008
Distribution: Fedora Core 2, Ubuntu 8
Posts: 13

Original Poster
Rep: Reputation: 0
Unhappy

Thank you very much for the help. It works exactly how I want it to. Part of the script is to check the filesize and if it's zero, I want it to do something, otherwise something else.
Code:
directory=$1
FILESIZE=$(ls -l | awk '$5 == 0{print $5}')

clear

if [ $# -ge 2 ]; then
        echo "Too many parameters. Usage: file-info [directory]"
        exit
elif [ $# -eq 0 ];then set `pwd`;
fi

if [ -d $1 ]; then
        cd $1
        echo "Processing `pwd`... "
        ls -l | awk '{print $9 " " $5 " " $3}'
        for files in $directory
        do
                if [ $FILESIZE ]; then
                        echo "Would you like to delete this file? (y/n)"
                        read ANSWER
                        if [ $ANSWER = "y" ]; then
                                echo "file has been deleted."
                                rm `pwd`/$filename
                        elif [ $ANSWER = "n" ]; then
                                echo "file retained"
                        fi
                fi
        done
else
        echo "$1 is not a valid directory..."
fi
The problem is it only dipslays what's in the directory and stops. It doesn't go ahead the do if statement, or even give any kind of error.
Why is it just stopping?
 
Old 08-14-2008, 02:49 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
for files in $directory
I think the problem is in the logic. Suppose you have directory "/path/to/some/dir". The code above loops over only one item, which is the directory name:
Code:
for files in /path/to/some/dir
that is the code inside the loop is executed once and the loop variable "files" will have the value "/path/to/some/dir". I think you want something like
Code:
for files in `ls $directory`
instead. This will loop over the files inside the directory and the loop is iterated as many times as many files you have. Furthermore, since you changed to the directory before the loop, you can do this instead:
Code:
for files in *
the shell will expand the asterisk with all the items found in the current directory. In both cases you may have problem with blank spaces inside file names.

Also notice that you have not used the loop variable in the code, but an empty variable "filename":
Code:
rm `pwd`/$filename
try to do the proper corrections and see if it works.
 
Old 08-15-2008, 09:07 AM   #8
akeenabawa
LQ Newbie
 
Registered: Jul 2008
Distribution: Fedora Core 2, Ubuntu 8
Posts: 13

Original Poster
Rep: Reputation: 0
Unhappy

If I change it to for files in * it still stops after displaying the contents. With for files in `ls $directory`, i get the error ls: mytestdir: No such file or directory. Which makes sense because it's in that directory already because of the 'cd $1' command. I take out the $directory it stops at the same place again. I'm new to this so maybe I'm not understanding what you're trying to tell me.
 
Old 08-15-2008, 12:06 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It stops because the conditional expression is not evaluated as you expected. Actually it does not make sense. If I interpret correctly your script, you want to test if the file has size zero and ask confirmation for deletion, right? In this case you can simply define a function that check the size of the file and returns an exit status of 0 or 1. The if statement can evaluate this function and act according to the exit status (0 = success: the file size is zero; 1 = failure: the file size is non zero).

Actually there is no need to define a function for this, nor to assign the command to a variable, as in your original attempt. Anyway, I have written a working version of your script, trying to preserve your style.

Another clue is the usage of the first positional parameter $1 or the variable "directory": if you decide to assign the value of the positional parameter to a variable, depending on the number of the positional parameters themselves, you have to stick with that variable. No need to use $1 again, later in your script.

Also I used the stat command in place of awk to retrieve file's properties (name, size, username) so that it is indipendent from different formats of the ls -l output and can properly manage files containing blank spaces in their name.

Have a look at it. See the differences between your script and mine and let me know if you experience some problem or if you need some clarification.
Code:
#!/bin/bash
zerosize(){
  if [ $(ls -l "$1" | awk '{print $5}') -eq 0 ]
  then
    return 0
  else
    return 1
  fi
}

clear

if [ $# -ge 2 ]; then
   echo "Too many parameters. Usage: file-info [directory]"
   exit
elif [ $# -eq 1 ]; then
   directory=$1
else
   directory=$PWD
fi

if [ -d $directory ]; then
   cd $directory
   echo "Processing $PWD... "
   for file in *
   do
     stat -c "%n %s %U" "$file"
     if zerosize "$file" ; then
        read -p "Would you like to delete this file? (y/n) " ANSWER
        if [ $ANSWER = "y" ]; then
           echo "file $file has been deleted."
           rm "$file"
        elif [ $ANSWER = "n" ]; then
           echo "file $file retained"
        fi
     fi
   done
else
   echo "$directory is not a valid directory..."
fi
 
Old 08-15-2008, 02:21 PM   #10
akeenabawa
LQ Newbie
 
Registered: Jul 2008
Distribution: Fedora Core 2, Ubuntu 8
Posts: 13

Original Poster
Rep: Reputation: 0
Smile

Thank you very much for the help. I wasn't expecting you to write it for me. It does do the files one-by-one but it gives an error on line 9: [: -eq: unary operator expected for each file. I'm going to keep working this, I think I can get it. Thanks again.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you write a script to display file name, Inode, and size of any file? JaxsunApex Linux - Newbie 3 01-30-2007 08:39 AM
Create a script to display file name, Inode, and size of any file. Has to be a script JaxsunApex Linux - Newbie 7 01-29-2007 08:15 PM
File owner and File group locked - root deiphage Linux - Hardware 5 02-05-2005 01:30 PM
File size display munged in Konqueror pumpkin7593 Linux - Software 0 01-24-2005 08:30 PM
file permissions and file owner won't change Nadim Slackware 5 11-29-2003 06:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration