Timeline for 'ls -1' : how to list filenames without extension
Current License: CC BY-SA 3.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 19, 2016 at 20:15 | comment | added | Keith Thompson | This is pretty much what I'd do if, for some reason, I wanted to remove strip the .png suffix from a list of file names. I wouldn't put it into a script; instead I'd just type the command at the shell prompt. Doing so would be a reminder that I'm assuming "sane" file names. There are plenty of things I'll do in a one-off manual command, when I feel free to make assumptions about what's in the current directory, that I probably wouldn't do in a script that might be re-used in some other context. | |
| May 19, 2016 at 13:56 | comment | added | reinierpost | @Hastur: those future problems will arise anyway. Lots of things in the system aren't robust against files with newlines. E.g. try using locate or make on them. | |
| May 19, 2016 at 11:23 | comment | added | Hastur | Moreover IMHO It's easy to start to parse ls, but it is afoot of future problems. Often we make scripts that we will use later, when we already will forget their limit... (it's human, it's usual). I proposed a find example (with -exec and without pipe) even if I deem a better (because pure shell) answer the cuonglm's one, solid and posix compliant. | |
| May 19, 2016 at 11:23 | comment | added | Hastur | I agree, in principle, about the usability, but this variant fails when you have a filename with each newline inside. This can easily occur unnoticed, for example, when you copy and paste a line from a pdf in a GUI, So you only think to be avoided pathological filenames. | |
| May 19, 2016 at 9:40 | comment | added | Norman Gray | @Hastur You're correct, in principle, and the famous page about don't parse ls lists further problems (and solutions) when handing pathological filenames. But the best way of handling that is to avoid having pathological filenames (doh!); and if you can't, or if you must be robust against them, then either use find or – possibly better – use a more powerful language than sh to manage them (the fact that sh can do everything doesn't mean that it's the best choice in each case). The shell is designed for usability first. | |
| May 19, 2016 at 0:59 | comment | added | Hastur | I like how the care you used to write your answer. This solution will present problems with filenames including newlines, it will not print the first part of the name. Even more if it is a nastier one with the key (.png) before the newline char: in that case you will print that part without png, not erasing only the last part. It is often sugpugested to avoid to parse (and pipe) the output of ls because the problems can be hidden just where you are not thinking about... | |
| May 18, 2016 at 16:17 | history | answered | Norman Gray | CC BY-SA 3.0 |