Monday, April 29, 2024
8
rated 0 times [  8] [ 0]  / answers: 1 / hits: 3793  / 2 Years ago, wed, june 29, 2022, 3:41:33

Okay guys, this is an experimental question warning!



I often confuse cat and ls when quickly working on the console. Not because I forget their meanings, no. Just because I type without deeply thinking about the command - it just comes automatically. I am sure everyone knows what I am trying to explain: you don't have to think what to type if your task is to list, say, a text file - or a directory.



Now, why two different commands?
Things would be more fluently if there was a command that lists a directory if the target is a directory and that lists the content of a file if the target is a file.



Does such a command exist?



I know there are editors (e. g. vim) with the ability to list a directory. But this is not what I want, as I want to stay on the console. Second, after opening a file through the vim directory listing there is no way back - at least none that I know.



If there is no out-of-the-box solution I will try to make a small bash script (wrapper) to cat and ls. But this is not preferred, because when working with many different systems it has to be placed on each of them...


More From » command-line

 Answers
0

Simply put this function is your .bashrc:



catls()
{
[[ -f "$1" ]] && cat "$1" || ls "$1";
}


It will cat regular files but call ls for directories


[#25997] Wednesday, June 29, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ncharm

Total Points: 255
Total Questions: 105
Total Answers: 118

Location: Virgin Islands (U.S.)
Member since Sat, May 6, 2023
1 Year ago
ncharm questions
;