LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 12-03-2017, 06:01 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,647

Rep: Reputation: 51
Using ls to list filenames without extension.


Hi:
Code:
bill@darkstar:/extra/STORE2/Nonsoft/libros/history/ANTIGUEDAD/ROSTOVTZEFF$ ls 
2015.459044.A-History-Of-The-Ancient-World.pdf  Vol_1_2015.529872.history-of.pdf
README                                          Vol_2_2015.529873.history-of.pdf
README~
bill@darkstar:/extra/STORE2/Nonsoft/libros/history/ANTIGUEDAD/ROSTOVTZEFF$
Here I want ls to list filenames without extension: trying with 'ls *.', 'ls *\.' escaping the dot won't do. Using 'find -regex' seems complicated. Either way the question refers to the linux command ls. Can ls do the job, so the output is
Code:
README
README~
?

Last edited by stf92; 12-03-2017 at 06:02 PM.
 
Old 12-04-2017, 12:46 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,688
Blog Entries: 3

Rep: Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168
Which shell are you using for this? If you are using Bash then you can set extended pattern matching to work with plain ls and look for files without dots.

Code:
shopt -s extglob

ls !(*\.*)

shopt -u extglob
What are you trying to do? Just a reminder, you shouldn't try to parse the output of ls, in case that is what this is about.
 
Old 12-04-2017, 04:31 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,879

Rep: Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910
if I understand well OP wanted to find files which have no extensions.
You need to understand it is not the command ls, but the bash which will evaluate "arguments" to ls and will pass a filelist to ls. ls will only execute ls on those files.
I have no idea how can you write a regex or glob for this, but probably this can be used:
Code:
ls -1 | grep -Fv .
(will be hard if you want to go into subdirs)
 
Old 12-04-2017, 06:45 PM   #4
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,647

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by Turbocapitalist View Post
Which shell are you using for this?
It is bash. In fact it would have to print any filename not ending in .??? where ? is any printable char (foo1.foo2 could be a file name and it would not be printed with your command). I just want to list a dir and know at a glance if it has names like README or README.PLEASE, for instance.

By the way, could you tell me what !, the exclamation mark does in your command?

EDIT: I've got it. It matches anything but *\.*. So it could also be !(*\.[0-z][0-z][0-z]$). Unluckily I should work with regexps due to the dollar sign and so, perhaps grep, as pointed to by pan64 would be in order.

Last edited by stf92; 12-04-2017 at 07:26 PM.
 
Old 12-05-2017, 01:59 AM   #5
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,688
Blog Entries: 3

Rep: Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168Reputation: 1168
Are these files from CP/M or a that infamous copy of CP/M that has mutated over the years? Normally it does not matter how many dots are in the name or where they are relative to the beginning or end.

Anyway, think about how the pattern is played out against the file names and try it without the dollar sign.

Code:
shopt -s extglob

ls !(*\.[0-z][0-z][0-z])

shopt -u extglob
Again, you shouldn't try to parse the output of ls. If you want more complex regex then you could use find for that. That gets quite complex because there are many regex styles to choose from.

Code:
find . -maxdepth 1 -type f -regextype posix-extended \
        -not -regex '.*/*\.[0-z][0-z][0-z]$' -exec somescript
So I'd stick with Bash because it can still do a lot. Although the manual page really is overwhelming it is really worth learning to navigate.

Code:
man bash
 
Old 12-05-2017, 03:25 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 6,979
Blog Entries: 4

Rep: Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681
Quote:
Originally Posted by stf92 View Post
I just want to list a dir and know at a glance if it has names like README or README.PLEASE, for instance.
that has a dot in it?

anyhow, so-called file extensions are relatively meaningless in gnu/linux.

is there some sort of xy-problem?
 
1 members found this post helpful.
Old 12-05-2017, 09:08 AM   #7
fatmac
Senior Member
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: AntiX
Posts: 1,978

Rep: Reputation: Disabled
Try
Code:
ls R*
 
Old 12-05-2017, 07:02 PM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,647

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by Turbocapitalist View Post
Are these files from CP/M or a that infamous copy of CP/M that has mutated over the years? Normally it does not matter how many dots are in the name or where they are relative to the beginning or end.
Of course it doesn't. But consider names like xxx.pdf, yyyyyy.epub, z.mp3 or x.tgz going around in the net or in your system. In 8.3 format extensions had meaning because foo.tgz was written on disk as 'foobbbbb.tgz', where b is char 0x20 (in what is called a node in Unix). If I use the name extension it should be understood as the termination .pdf of the filename, for instance. Nothing more.


[/QUOTE]
Code:
bill@darkstar:/extra/STORE2/Nonsoft/li bros/history/ANTIGUEDAD/ROSTOVTZEFF$ ls
2015.459044.A-History-Of-The-Ancient-World.pdf  Vol_1_2015.529872.history-of.pdf
README                                          Vol_2_2015.529873.history-of.pdf
README.PLEASE                                   foo.www.aaaaaa
README~
bill@darkstar:/extra/STORE2/Nonsoft/libros/history/ANTIGUEDAD/ROSTOVTZEFF$ ls !(*\.[0-z][0-z][0-z])
README  README.PLEASE  README~  foo.www.aaaaaa
bill@darkstar:/extra/STORE2/Nonsoft/libros/history/ANTIGUEDAD/ROSTOVTZEFF$
As you can see, omiting the dollar sign won't do. The use of find: I want to know if ls is able to accomplish the task. I think a file directory listing tool is one the fundamental utilities in any OS. Now if you first have to accomplish the feat of fully understanding bash before using that tool, or whatever the shell is, or if you have to rely on an extra utility such as find, I then begin to distrust people praising Linux.

Another example. Go list only files (omit directories). I don't think you'll be able to do it with only ls! There are plenty of elementary cases in directory listings that ls will be able to accomplish only within a pipe, to write which you have to immerse into the complexities of filename expansion in the shell. Or end using find.

Last edited by stf92; 12-05-2017 at 07:26 PM.
 
Old 12-06-2017, 03:05 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,879

Rep: Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910
Quote:
Originally Posted by stf92 View Post
I want to know if ls is able to accomplish the task.
I'm not really sure if you specified that task properly. Probably ls is not the best tool for that.

Quote:
Originally Posted by stf92 View Post
I think a file directory listing tool is one the fundamental utilities in any OS. Now if you first have to accomplish the feat of fully understanding bash before using that tool, or whatever the shell is, or if you have to rely on an extra utility such as find, I then begin to distrust people praising Linux.
Hm. I don't think so. As far as I see you have a special query, which cannot be done if you don't know the tools you use to do that.


Quote:
Originally Posted by stf92 View Post
Another example. Go list only files (omit directories). I don't think you'll be able to do it with only ls! There are plenty of elementary cases in directory listings that ls will be able to accomplish only within a pipe, to write which you have to immerse into the complexities of filename expansion in the shell. Or end using find.
you need to learn what is ls exactly and how to use. If it is not what you need use something else...

Code:
 find <dir> -type f [-regex <pattern>] -ls
probably this is the tool you need
 
1 members found this post helpful.
Old 12-06-2017, 06:45 PM   #10
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,647

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by pan64 View Post
I'm not really sure if you specified that task properly. Probably ls is not the best tool for that.
I'm not sure if you read the thread properly. See post #4 if you please.

Quote:

you need to learn what is ls exactly and how to use. If it is not what you need use something else...

ls was intended to mean LiSt. Of course, list files.

Now I'll give a concrete example as an illustration. This is not about the browser or the download manager. Most of the times when downloading a web page the html file ends up in disk without the terminating string .htm/html. I was looking for a certain html page I had once downloaded and the natural thing would have been to issue 'ls *html' or 'ls *.html'. I did that but, because of the above said, many html downloaded files on the directory/ries where skipped. So the problem is how to list the non .html terminated filenames, which amounts in practice to listing those not ending in any of the most used terminations or just not ending in .xxx or .xxxx How would you solve it? This is the exact contents of post #4 above.

Last edited by stf92; 12-06-2017 at 07:13 PM.
 
Old 12-06-2017, 11:54 PM   #11
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 340

Rep: Reputation: 142Reputation: 142
GNU coreutils ls can hide entries matching a shell pattern.
Code:
ls -I '*.???'         # The pattern needs to be in quotes or it will be expanded before ls starts
From the man page:
Code:
-I, --ignore=PATTERN
       do not list implied entries matching shell PATTERN
 
Old 12-07-2017, 02:30 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,879

Rep: Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910Reputation: 2910
Quote:
Originally Posted by stf92 View Post
I'm not sure if you read the thread properly. See post #4 if you please.
If I understand correctly you want to list files except the one which has a . (dot) in their filenames on the 4th pos on the right side.
But in this sentence list does not mean any linux tool just a regular English verb.
Quote:
Originally Posted by stf92 View Post
ls was intended to mean LiSt. Of course, list files.
And no, this sentence/statement is either incomplete or incorrect.
ls will list files passed to it as arguments, or content of dirs. If you check the man page originally it was intended to list dirs, not files.
furthermore using the syntax: ls <pattern> the <pattern> will be evaluated by the shell (bash), not ls, and the result (=list of files/dirs) will be passed to ls. ls will not pick and choose just list all of them.
In newer implementations of ls there are some interesting additions like -I (was mentioned already), but ls will still work on the arguments passed to it.


Quote:
Originally Posted by stf92 View Post
Now I'll give a concrete example as an illustration. This is not about the browser or the download manager. Most of the times when downloading a web page the html file ends up in disk without the terminating string .htm/html. I was looking for a certain html page I had once downloaded and the natural thing would have been to issue 'ls *html' or 'ls *.html'. I did that but, because of the above said, many html downloaded files on the directory/ries where skipped. So the problem is how to list the non .html terminated filenames, which amounts in practice to listing those not ending in any of the most used terminations or just not ending in .xxx or .xxxx How would you solve it? This is the exact contents of post #4 above.
So this is the goal you want to reach. No, this is not the same as post #4. So probably -I '*.html' -I '*.htm' (or similar) is your friend.
 
Old 12-07-2017, 12:52 PM   #13
urbanwks
Member
 
Registered: Sep 2003
Distribution: Slackware64-Current, FreeBSD 11.0
Posts: 130

Rep: Reputation: 110Reputation: 110
You’re making a lot of assumptions about ls, bash, and Linux generally. Just because you’re frustrated with trying to use a fork to eat soup, that doesn’t make the soup useless, or the fork for that matter.

Can you provide a mock-up of what your expected output might look like, along with an example ‘before’ instance of the listing you’re trying to filter? I am (and it looks like others are) having a hard time figuring out the exact endgame here, and it seems it would be easier to just figure out how to get from A to B.
 
Old 12-07-2017, 07:52 PM   #14
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,647

Original Poster
Rep: Reputation: 51
Alright:
Code:
$ ls -1
rotman_4th_ed.mp3                 
rotman_ex_7_13.pdf.               
theory.of.infinite.groups.htm     
theory_of_infinite_groups.htm     
theory.groups.fin01.burngoog      
theory-of-infinite-groups         
theory\ of\ infinite\ pdf\ groups
Now I want the command (presumibly ls) to output:
Code:
theory.groups.fin01.burngoog      
theory-of-infinite-groups         
theory\ of\ infinite.pdf\ groups
That is, do not print any filename ending in a dot followed by three printable chars. ls -I '*.???' and ls !(*\.[0-z][0-z][0-z]) will unduly omit the last entry. So after all I think find must come to the rescue. For instance, as suggested,
Code:
find . -type f -regex '.*\.[0-z][0-z][0-z]$' -ls
would do the job if only the syntax if modified for showing dir entries not matching the regular expression, a thing I couldn't do yet. Again, this is only for visual inspection. Not for processing by a program.
 
Old 12-07-2017, 09:07 PM   #15
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,479

Rep: Reputation: 620Reputation: 620Reputation: 620Reputation: 620Reputation: 620Reputation: 620
Why not just use:
Code:
find -maxdepth 1 ! -iname "*.???"
-maxdepth sets how many levels of recursion find will use, in this case only the contents of the current folder will be shown, -iname is a case insensitive pattern which matches any number of characters a dot and three characters, the ! then inverts the match.
 
  


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
Why limitation of 6 or 8 character filenames + extension on older operating systems? Xeratul Programming 14 10-07-2015 09:44 PM
How to list duplicate filenames wonfineday Linux - Newbie 7 12-07-2011 01:51 AM
list filenames with spaces in a shell script xp_newbie Programming 6 03-15-2009 08:46 PM
script to list the filenames which are in lower case naveensankineni Programming 2 03-12-2008 08:09 AM
Folder List With Numbers Over 100 In Filenames hazmatt20 Linux - General 14 01-09-2006 03:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

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