Display by File Name, File Size, and File Owner using ls
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.