LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-16-2016, 07:24 AM   #1
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Rep: Reputation: Disabled
How to find the file with the less rows in directory


Hello!

I'm new to this forum and also new on Linux.

I'm trying to learn some scripting in Bash. Like subject says I'm trying to write script which will check directory and than return name of the file with the less rows back to me.
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").

I guess I could solve this with two for loops, but I don't even know how to start.

Thanks for your help!
 
Old 10-16-2016, 07:39 AM   #2
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 962
Blog Entries: 3

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Welcome. There are a lot of ways to get to your goal. What have you tried so far? Have you looked at "wc", "sort", or "find" yet?

Code:
man wc
man sort
man find
man man
You can send the output of one program into the next as input using a pipe. You can chain as many together in a row as you need.
 
Old 10-16-2016, 07:48 AM   #3
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Hi, thanks for response

Well I'm realy fresh into Bash and I was reading some books about Bash (HOW-TO), so I know those basic commands like, cd, mkdir, cat, echo, touch, ls, wc -l, etc, but nothing else like more complicated.
I guess for experts like you this seems to be and easy task which can be solved in many different ways (from easy to complicated) but for the beggining i'm looking for easiest possible solution which will be easy to understand.

I started with something like this
Code:
for i in `ls -l *.txt`
do
for j in `echo $i | grep .txt`
but as far as I know this will only grab *txt files. What if there are other text files like .odt ?
 
Old 10-16-2016, 07:55 AM   #4
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 962
Blog Entries: 3

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Quote:
Originally Posted by NewGuy95 View Post
but as far as I know this will only grab *txt files. 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.

If you want to read the number of lines in a plain text file instead, the utility "wc" will do that. It's good that you're looking at this like programming, because that gives you a just-in-time approach and you learn to look around for programs.

Winding back a little to what you have with the for loop, I'd recommend skimming through these two:

http://mywiki.wooledge.org/BashPitfalls

http://mywiki.wooledge.org/BashGuide

In the first link, see the first item.
 
Old 10-16-2016, 02:11 PM   #5
MadeInGermany
Member
 
Registered: Dec 2011
Posts: 193

Rep: Reputation: 73
The main gain is to read
Code:
man bash
Here is some shell code
Code:
min=""
fname=""
# loop over all items in the current directory
for f in *
do
  # sort out non-files
  [ -f "$f" ] || continue
  # count the lines
  cnt=`wc -l < "$f"`
  # if min is yet empty or count is less
  if [ -z "$min" ] || [ $cnt -lt $min ]
  then
    # store the count and the filename
    min=$cnt
    fname=$f
  fi
done
echo "$fname has $min rows"
Note that || is a logical or,
and [ ] is a test function.
All explained in the bash man page.

Last edited by MadeInGermany; 10-16-2016 at 02:14 PM.
 
2 members found this post helpful.
Old 10-17-2016, 06:17 AM   #6
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
The main gain is to read
Note that || is a logical or,
and [ ] is a test function.
All explained in the bash man page.
Thanks for your answer!

I still don't know how to pass argument to script? I know how to run script with
Code:
bash scriptname.sh
but i don't know where to put argument (name of directory which I want to check).

Thanks for your help!
 
Old 10-17-2016, 06:23 AM   #7
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 962
Blog Entries: 3

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
You can make your script executable by setting the executable bit:

Code:
chmod +x ./myscript
Then you can be in the same directory and run it like this:

Code:
./myscript
See the manual page for "chmod" for all the options.

Many distros are set up to recognize ~/bin/ So if you make that directory and put your script there, it will be available next time you log in and you can then run it just by typing its name:

Code:
myscript
About passing parameters to your script, here is a short summary: http://mywiki.wooledge.org/BashGuide..._and_Variables
 
Old 10-17-2016, 06:30 AM   #8
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
I marked it as executable under Properties but nothing happens when I click on it.
I already moved to Desktop via
Code:
cd Desktop/
where is that script.

But now in terminal I was trying to start the script via
Code:
bash scriptname.sh
.
I don't know where to put directory name? Inside script under
Code:
 fname = ""
or here inside Terminal?

Thanks
 
Old 10-17-2016, 06:53 AM   #9
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 962
Blog Entries: 3

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Quote:
Originally Posted by NewGuy95 View Post
I don't know where to put directory name? Inside script under
Code:
 fname = ""
or here inside Terminal?
Which distro are you using? The graphical file managers behave a little differently in different distros, especially regarding shell scripts. Are you going to be running the script by clicking on an icon and wanting it to go to the same directory each time or a different one each time?

The line in the script several posts above:

Code:
for f in *
has a comment above it. The command to switch working directory is "cd" followed by the directory name. So if you are using the same directory each time, just write it in with "cd" above the comment.
 
1 members found this post helpful.
Old 10-17-2016, 07:14 AM   #10
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
I'm using Xubuntu 16.04 and working in Sublime Text 3.

Well it's not important if I check the same directory or each time other for me at the moment.

Now I try this
Code:
for f in $1
$1 is representing that directory, so now I start the script as
Code:
 bash myscript.sh /home/user/Desktop/TestFolder/
but I get error at row 3 and 4: min and fname, command not found (Idk how to translate it in english)
 
Old 10-17-2016, 07:17 AM   #11
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 962
Blog Entries: 3

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
That's quite close. Remember you want f to cycle through the files in the directory passed as $1 not the directory itself. So you could try:

Code:
for f in $1/*
It will get confused though if $1 is empty so maybe a check to make sure it is non-empty would be good. (Creeping featurism!)
 
Old 10-17-2016, 07:25 AM   #12
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Well if I do that, than i get like 5 new errors, about cnt (cnt ='wc.... ) also command not found
 
Old 10-17-2016, 07:31 AM   #13
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 962
Blog Entries: 3

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
What does your whole script look like at the moment?
 
Old 10-17-2016, 07:36 AM   #14
NewGuy95
LQ Newbie
 
Registered: Oct 2016
Posts: 14

Original Poster
Rep: Reputation: Disabled
Its actualy the same as the one MadeInGermany wrote except that one line where I changed * with $1.

Also I found some typo in my code which is now executable, but as a result I don't get anything only:
Code:
   has    rows.
 
Old 10-17-2016, 07:37 AM   #15
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 962
Blog Entries: 3

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
?

Code:
cat myscript.sh
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Creating script which find the file with the less rows NewGuy95 Linux - Newbie 1 10-17-2016 02:25 PM
[SOLVED] Find a File delete out different file in same directory HardenedCriminal Linux - Security 9 06-04-2015 08:28 PM
find the largest and smallest file in a directory and sub directory schandran Programming 5 05-06-2015 10:53 AM
how to find match between two rows of column in same file using PERL genetist Linux - Newbie 3 07-13-2013 12:17 PM
a2ps: cannot find file `c0419bt_.afm': No such file or directory rg3 Slackware 5 08-02-2009 04:11 PM


All times are GMT -5. The time now is 12:48 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration