LinuxQuestions.org
Review your favorite Linux distribution.
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, 02:06 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,289

Rep: Reputation: 50
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, 02:34 AM   #2
ponce
Senior Member
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 2,504

Rep: Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912
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 02:41 AM.
 
1 members found this post helpful.
Old 06-01-2013, 02:53 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,289

Original Poster
Rep: Reputation: 50
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, 02:58 AM   #4
ponce
Senior Member
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 2,504

Rep: Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912
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 03:00 AM.
 
Old 06-01-2013, 03:20 AM   #5
ponce
Senior Member
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 2,504

Rep: Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912
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 05:52 AM. Reason: more fixes when spurious stuff remains
 
Old 06-01-2013, 03:38 AM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,289

Original Poster
Rep: Reputation: 50
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 03:40 AM.
 
Old 06-01-2013, 04:05 AM   #7
ponce
Senior Member
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 2,504

Rep: Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912
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 05:45 AM.
 
Old 06-01-2013, 04:09 AM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,289

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

Original Poster
Rep: Reputation: 50
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, 04:15 AM   #10
astrogeek
Senior Member
 
Registered: Oct 2008
Distribution: Slackware [64]X{.0|.1|.2|-current} ::X>=12<=14, FreeBSD_10{.0|.1}
Posts: 2,186

Rep: Reputation: 862Reputation: 862Reputation: 862Reputation: 862Reputation: 862Reputation: 862Reputation: 862
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 04:16 AM.
 
Old 06-01-2013, 04:18 AM   #11
ponce
Senior Member
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 2,504

Rep: Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912
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 04:20 AM.
 
Old 06-01-2013, 04:22 AM   #12
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,289

Original Poster
Rep: Reputation: 50
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 04:28 AM.
 
Old 06-01-2013, 04:25 AM   #13
ponce
Senior Member
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 2,504

Rep: Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912
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 04: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 09:47 PM
List directory into one line ufmale Linux - Newbie 4 07-27-2007 08:17 PM
Set number of colors to be displayed General Ubuntu 1 06-10-2006 07:35 PM
CD-Rom ,filenames are not displayed correctly Darklegion Linux - General 4 08-28-2004 03:46 AM
KBear - Mandrake 10.0 - Filenames not displayed dafatdude Linux - Software 4 07-25-2004 11:42 AM


All times are GMT -5. The time now is 01:17 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration