bash - Display Only Files And Dotfiles In The Default "ls" Command Format?
问题描述
EDIT 2: For those with the same question, please be sure to read the comments on the accepted answer, as they go more in depth about how to properly use the command. I will post the final script once it is done with my own explanation of what is going on.
I'm trying to write a simple bash function that clears the terminal screen, then performs multiple ls
commands to show the different content types of the current directory in different colors.
I've managed to write something in Python which kind of does the same thing, but it has some big drawbacks (specifically, the behavior for special characters in filenames is "iffy" on Cygwin, and it's a pain to make it fit properly on the screen). I want it to look something like this:
*With non-dotfiles in green (haven't include them in the screenshot for privacy reasons).
I've managed to list both hidden directories and visible directories, but the files are giving me trouble. Every method of using ls
to get all files I've tried uses the -l
argument, or something like find
or grep
, all of which output the files in a single column (not what I want).
Is it possible to use the ls
command (or something else) to output only the files or dotfiles of a directory while keeping ls
's default output format?
EDIT 1: Here's what the script currently looks like (not much yet, but some people want to see it)
function test() {
clear;
GOLD=229;
RED=203;
BLUE=39;
WHITE=15;
GREEN=121;
# Colored legend.
tput sgr0;
tput setaf bold
# echo's "-n" suppresses the new line at the end.
tput setaf $GOLD;
echo -n "Working Directory ";
tput setaf $RED;
echo -n "Hidden Directories ";
tput setaf $BLUE;
echo -n "Visible Directories ";
tput setaf $WHITE;
echo -n "Hidden Files ";
tput setaf $GREEN;
echo "Visible Files";
pwd;
ls -d .*/; # List hidden directories.
ls -d */; # List visible directories.
# List dotfiles.
# List files.
}
解决方案
仅列出当前目录中的点文件
find . -maxdepth 1 -type f -name ".*" | xargs ls --color=tty
仅列出其他文件
find . -maxdepth 1 -type f -not -name ".*" | xargs ls --color=tty
推荐阅读
- node.js - 如何设置 mongoDB 的新 CSFLE 功能,使用 nodejs 显式加密隐式解密?
- flutter - 错误:提供者消费者 - 飞镖(return_of_invalid_type)
- sql - 有效地从一个 ID 与另一张表匹配的表中删除
- scala - (Scala,Akka)在 Akka Flow 缓冲区上设置调度程序吞吐量 = 1 的效果
- flutter - Flutter - 任务':app:checkDebugAarMetadata'的执行失败
- relational-database - 这个规范化的表有什么问题?
- go - 如何使用 Gin 从动态子目录中提供文件?
- mysql - 带有打字稿的续集不能使用带有类型属性的 .create
- javascript - Service Worker onClick 事件 - 打开并将一些数据传递给 PWA
- python - 使用与列名值匹配的键创建新列