My system:
- OS: MacOS / Mac OS X (Mojave 10.14.5)
- OS core: Darwin (18.6.0)
- Kernel: Darwin Kernel / XNU (18.6.0 / xnu-4903.261.4~2/RELEASE_X86_64)
- ls: version unknown, but
man ls
gives a page from the BSD General Commands Manual - Shells:
- Bash: GNU bash, version 5.0.7(1)-release (x86_64-apple-darwin18.5.0)
- Zsh: zsh 5.7.1 (x86_64-apple-darwin18.2.0)
In MacOS, in a terminal CLI using a shell such as bash or zsh, I'd like to use the (BSD) command ls
(or perhaps a similarly common and useful tool) to list the contents of a directory other than the current working directory, where all files except those ending with a tilde (~
) are shown.
Excluding the last stipulation, ls
naturally accomplishes this task when the non-current directory is used as an argument to ls
: ls arg
where arg
is an absolute or relative path to the non-current directory (such as /absolute/path/to/directory
, ~/path/from/home/to/directory
, or path/from/current/dir/to/directory
).
I know how to list non-backup contents in the current directory, using filename expansion (aka "globbing") and the -d
option (to list directories and not their contents), like so: ls -d *[^~]
(or ls -d *[!~]
). I want the same sort of results, but for a non-current directory.
I can almost achieve what I want by using ls -d arg/*[^~]
, where arg
is the same as described above, but the results show the path to each content element (ie, each file and directory in the directory of interest). I want ls
to display each element without the path to it, like is done with ls arg
.
In Linux, using the GNU command ls
, I can achieve exactly what I want using the -B
option to not list backup files: ls -B arg
. Although this is what I want, I'd like to achieve this using tools native to MacOS, preferably the BSD ls
.
Note: I do not want to use grep
(eg, ls arg | grep '.*[^~]$'
), because grep
changes the formatting and coloring of the output.
Question recap: On a Mac, how can I list the contents of a non-current directory but not the backup files, preferably using ls
?
ls arg | grep --color=always '.*[^~]$'
good enough?grep
with--color-always
is not appropriate for my purposes because it changes the formatting and coloring of the output. I want it to behave just likels
.