How to find the file with the less rows in directory
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.
#!/bin/bash
rows=""
name=""
for f in $1
do
[ -f "$f" ] || continue
cnt='wc -l < "$f"'
if [ -z "$rows" ] || [ $cnt -lt $rows ]
then
rows=$cnt
name=$f
fi
done
echo "$file has $rows
Thanks. There are three hard to see typos. The first is that single quotes ( ' ) were used instead of backticks ( ` ) That is one reason why it is a good idea to do command substitution with parenthesis instead. The second is the wrong variable name in the final line and missing a closing double quote.
Code:
#!/bin/bash
rows=""
name=""
for f in $1
do
[ -f "$f" ] || continue
cnt=$(wc -l < "$f")
if [ -z "$rows" ] || [ $cnt -lt $rows ]
then
rows=$cnt
name=$f
fi
done
echo "$name has $rows"
Note that the script will try to look at all files in the given directory, even if they are subdirectories or binaries.
Name of directory will be passed as argument, also when there will be no arguments than script should return error (like echo "You forgot arument").
and
Quote:
I don't know where to put directory name?
For this you want to pass a positional parameter.
If you call your script and supply the directory name e.g. scriptname.sh </your/directory/name>
then I suggest a construction like:
Code:
if [ -d "$1"] ; then
cd "$1"
else
echo "Invalid directory name - exiting. Usage: ${0##*/} <directory>"
exit(1)
fi
The '${0##*/}' is a bash parameter expansion that finesses the $0 positional parameter to leave just the script name.
For this you want to pass a positional parameter.
If you call your script and supply the directory name e.g. scriptname.sh </your/directory/name>
then I suggest a construction like:
Code:
if [ -d "$1"] ; then
cd "$1"
else
echo "Invalid directory name - exiting. Usage: ${0##*/} <directory>"
exit(1)
fi
The '${0##*/}' is a bash parameter expansion that finesses the $0 positional parameter to leave just the script name.
Can you be a little bit more specific where to put that code?
After
The square brace is another way of writing "test", which is built-in with "bash"
It checks the existence of the directory you are going to be looking for. The manual page for "bash" is daunting. Really. But over time you'll learn to navigate it. See if you can hunt for "test" and its option -d in there:
Code:
man bash
It's under "conditional expressions"
So you'll need that test in your script before you try to use the directory.
Try it in a separate script:
Code:
#!/bin/bash
if [ -d "$1"] ; then
cd "$1"
else
echo "Invalid directory name - exiting. Usage: ${0##*/} directory"
exit(1)
fi
exit(0)
Edit: fixed shell
Last edited by Turbocapitalist; 10-17-2016 at 09:20 AM.
But if I try to paste those if statements inside my code it is working with directory, but without directory it displays me error like 20 times in a row.
Code:
#!/bin/bash
rows=""
name=""
for f in $1/*
do
if [ -d "$1" ] ; then
[ -f "$f" ] || continue
cnt=$(wc -l < "$f")
if [ -z "$rows" ] || [ $cnt -lt $rows ]
then
rows=$cnt
name=$f
fi
else
echo "Invalid directory name - exiting. Usage: ${0##*/} <directory>"
fi
done
echo "$name has $rows"
The for and done mark a loop. Stuff inside that will be repeated for each file found. Try moving the test before it, since you will need to test the directory.
PS. I fixed the shell in the script above, since we're sticking with "bash" for this task.
#!/bin/bash
rows=""
name=""
if [ -d "$1" ] ; then
cd "$1"
else
echo "Invalid directory name - exiting. Usage: ${0##*/} <directory>"
fi
for f in $1/*
do
[ -f "$f" ] || continue
cnt=$(wc -l < "$f")
if [ -z "$rows" ] || [ $cnt -lt $rows ]
then
rows=$cnt
name=$f
fi
done
echo "$name has $rows"
Like I told you before I got error if I use exit(1) in row 9 (where exit is) and it wont let me any further.
Exit (1) (usually) indicates unsucessful termination if I got it right
I'm kind of curious at what point the OP is going to do any work? So far he/she has been spoon fed every line of code and as far as I can see done zero to work anything out except ask another question
on how to fix code that is clearly not understood.
Maybe the OP would be better served by searching for the solutions instead of asking for a hand out. I would add that running a script on your system when you have no idea what it is doing is fraught with
danger.
Turbocapitalist has provided the 'wooledge' website which if processed would cover off many of the questions being asked. You have also been told repeatedly that operating on non-text files (see your
latest error messages) will cause issues.
Dear grail, thanks for your response.
As an non-english speaker I find it more simple if someone can explain me my problem in a way I will understand what is going on. As a totaly newbie (like few days) I don't understand all terms in literature that has been given to me.
Thats why I am asking experts like you guys are for some help. I you find it too hard to help, than please avoid this topic
I have no issue with helping and am happy to see you take your first steps in this arena, but currently I have not seen any feedback from you that was not written by someone else and you will
not learn by regurgitating someone else's code / issues.
The comments you have made show that you are not completely understanding the first 'if' clause and why it requires the exit command. You should get to grips with this first and understand
what entries make you hit either part of the 'if' clause and what you need to enter at the command line to progress correctly past this structure.
Once you have understood the above it may then be prudent to walk your self the same way through each part of the code inside the 'for loop' as again, the questions you are coming up against are from a lack of understanding which others will fix but you will become no better at coding in bash without understanding it.
I would reiterate my earlier information too in that you really need to click on every link and work your way through the entire 'wooledge' site as the information is invaluable. I know this will be daunting as the sire is only in English, but the concepts are sound and will help you should you learn what is there
What if there are other text files like .odt ?
...
OpenDocument Format files have text in them but it is buried in XML which is in turn archived in a ZIP archive, so it's not a straight up task to read the number of lines in them.
Once you learn a lot of the terminal commands to manage plain text, a frequent task is figuring out to convert those "complex" files like xml into a more readable plain text.
You will lose a lot / all formatting data but this is typically not needed (finding number of lines, grepping for strings, etc)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.