LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 06-01-2013, 01:06 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Getting a directory list with a line specifying the number of filenames displayed.


Hi: would it be too complicated to have a command line, or maybe a shell script, to make ls print the number of displayed files? Then I could have an alias to invoke it and use it instead of ls.
 
Old 06-01-2013, 01:34 AM   #2
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,068

Rep: Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145
Code:
ls -1A ; echo "- - -" ; echo $(/usr/bin/ls -1A | wc -l)" files/directories"
it counts also files starting with a dot (the hidden ones): if you don't want that, omit the "A".

Last edited by ponce; 06-01-2013 at 01:41 AM.
 
1 members found this post helpful.
Old 06-01-2013, 01:53 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
It's very interesting, thanks. Would it be possible to modify it to make it print size and date/time besides file names? Perhaps a utility that trims off the other data from the output of 'ls -l' (permissions, number of hard links, owners)?
 
Old 06-01-2013, 01:58 AM   #4
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,068

Rep: Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145
yes, you can do it using ls options and awk (have a look at the various examples around).

btw, the code above cannot be used as an alias: maybe its better to create a small script called, for example, lsn that does it
Code:
#!/bin/sh
ls $@
echo "- - -"
echo $(/usr/bin/ls -1A $@ | wc -l)" files/directories"

Last edited by ponce; 06-01-2013 at 02:00 AM.
 
Old 06-01-2013, 02:20 AM   #5
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,068

Rep: Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145
as using awk you probably will lose formatting, you can also do it with sed (it's not elegant but more or less it works)
Code:
#!/bin/sh
ls -gGA --time-style="+%b %d %Y %H:%M" $@ | sed "s/^.............//" | grep -v ^total.* | sed "s/^[0-9]\ //"
echo "- - -"
echo $(/usr/bin/ls -1A $@ | wc -l)" files/directories"
EDIT: I added, as an example, the customizing of the timestamp output (still using ls options) and cutted out spurious stuff (JIC)

Last edited by ponce; 06-01-2013 at 04:52 AM. Reason: more fixes when spurious stuff remains
 
Old 06-01-2013, 02:38 AM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
The last line I had no problem in understanding it. The line with sed: $@ is argument #1 (#0 is the command name itself). I do not know sed but I'm sure I will be able to understand the rest of the line (the part after '|') if only you were so kind to give some hints. In that way perhaps I would be able to trim the script to my taste in the case of new needs.

Last edited by stf92; 06-01-2013 at 02:40 AM.
 
Old 06-01-2013, 03:05 AM   #7
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,068

Rep: Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145
the first part of the line is just ls and his options ($@ means "all the arguments passed to the script").
the part after the pipe ("|") is a sed that substitutes (sed "s/whatever/whatever_else/options") the first (^ match the begin of the line) 13 chars (. means any character -besides newlines) of the output from the ls command with an empty character.

see "man ls" and "man sed" for more insights.

Last edited by ponce; 06-01-2013 at 04:45 AM.
 
Old 06-01-2013, 03:09 AM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
And why two slashes ('/') on the right? They don't balance!
 
Old 06-01-2013, 03:12 AM   #9
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Sed's seems like an atractive man page to study. It only has 280 lines in slackware 14.0 and possible less in older versions. I'll try to invent occasions where to use it to get familiar with it. Thank you very much for your kind posts.
 
Old 06-01-2013, 03:15 AM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,258
Blog Entries: 24

Rep: Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193
Quote:
Originally Posted by stf92 View Post
And why two slashes ('/') on the right? They don't balance!
With an expression like

Code:
sed 's/first/second/'
it replaces 'first' with 'second'.

so something like
Code:
sed 's/something//'
replaces 'something' with '', nothing at all.

As ponce said, see the sed man page, and look online for some references for regular expressions.

Last edited by astrogeek; 06-01-2013 at 03:16 AM.
 
Old 06-01-2013, 03:18 AM   #11
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,068

Rep: Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145
Quote:
Originally Posted by ponce View Post
sed "s/^.............//"
Quote:
Originally Posted by ponce View Post
(sed "s/whatever/whatever_else/options")
Quote:
Originally Posted by stf92 View Post
And why two slashes ('/') on the right? They don't balance!
I'll try to explain again: the syntax for the substitution in sed is the above (regular expressions let you substitute the delimiter / with the character you prefer, I usually prefer |)
Code:
sed "s|[text to be substituted]|[final text]|[options for the substitution]"
in the code above, [text to be substituted] is specified as "any 13 chars at the begin of the line", [final text] is empty and there are no additional options for the substitution.

the // above are just two delimiters, that in the case above delimit an empty string.

Last edited by ponce; 06-01-2013 at 03:20 AM.
 
Old 06-01-2013, 03:22 AM   #12
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
I now see. 's' is just a search and replace command like in vim. s/first string/second string/ in vim replaces 'first string' with 'second string'. With '^' I am familiar from using regexps with grep. And with '.' of course. This sed seems to be a very nice tool indeed.

Overlapping occurred. Sent when post #11 had not yet arrived at my screen. I will not mark the thread solved in case I have a new question. Again, thanks ponce.

Last edited by stf92; 06-01-2013 at 03:28 AM.
 
Old 06-01-2013, 03:25 AM   #13
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,068

Rep: Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145
Quote:
Originally Posted by stf92 View Post
I now see. 's' is just a search and replace command like in vim. s/first string/second string/ in vim replaces 'first string' with 'second string'. With '^' I am familiar from using regexps with grep. And with '.' of course. This sed seems to be a very nice tool indeed.
the syntax is the same, so if you're familiar with vim's regexps you should have a nice time with sed

learning to use these standard tools, like awk, cut, sort (just to cite a few) will help you a lot.

Last edited by ponce; 06-01-2013 at 03:27 AM.
 
  


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
character conversion issue - wrongly displayed filenames. kevinyeandel Linux - Newbie 1 12-10-2010 08:47 PM
List directory into one line ufmale Linux - Newbie 4 07-27-2007 07:17 PM
Set number of colors to be displayed General Ubuntu 1 06-10-2006 06:35 PM
CD-Rom ,filenames are not displayed correctly Darklegion Linux - General 4 08-28-2004 02:46 AM
KBear - Mandrake 10.0 - Filenames not displayed dafatdude Linux - Software 4 07-25-2004 10:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 12:04 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