Command to list contents if directory, or do output contents if file? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionApt-get autocompletionCD into directory from HISTORY in a bash script or alias e.g. $(!!)Command completion does not complete the files in the current directoryBack to back Pipes into a CommandHow does Bash auto completion work?Zip the contents of a directory without the directory itselfAccess a file located with findzsh: separate completion for command names and filenamesHow can I reference the same path from one command to the next?Ignore auto-completion on single same filename
How should I respond to a player wanting to catch a sword between their hands?
How do I keep my slimes from escaping their pens?
What would be Julian Assange's expected punishment, on the current English criminal law?
If A makes B more likely then B makes A more likely"
Why does tar appear to skip file contents when output file is /dev/null?
Can I throw a longsword at someone?
How does modal jazz use chord progressions?
What LEGO pieces have "real-world" functionality?
What is the largest species of polychaete?
Why use gamma over alpha radiation?
Can a zero nonce be safely used with AES-GCM if the key is random and never used again?
Unable to start mainnet node docker container
Stop battery usage [Ubuntu 18]
How to rotate it perfectly?
Can a non-EU citizen traveling with me come with me through the EU passport line?
How can I make names more distinctive without making them longer?
What to do with post with dry rot?
Blender game recording at the wrong time
Limit for e and 1/e
Active filter with series inductor and resistor - do these exist?
What did Darwin mean by 'squib' here?
Why is "Captain Marvel" translated as male in Portugal?
What can I do if my MacBook isn’t charging but already ran out?
Notation for two qubit composite product state
Command to list contents if directory, or do output contents if file?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionApt-get autocompletionCD into directory from HISTORY in a bash script or alias e.g. $(!!)Command completion does not complete the files in the current directoryBack to back Pipes into a CommandHow does Bash auto completion work?Zip the contents of a directory without the directory itselfAccess a file located with findzsh: separate completion for command names and filenamesHow can I reference the same path from one command to the next?Ignore auto-completion on single same filename
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Is there any command to list the contents if passed a directory, but output the contents if it's a file?
For example, often when I am navigating the directory structure I will start typing into the terminal something like
ll /etc/path/ # I see a file in tab-completion that interests me, called myfile.txt
ll /etc/path/myfile.txt # I do the auto-completion to get it into the command line
less /etc/path/myfile.txt # Now I have to go back and change 'll' to 'less'
Alternatively, if I'm looking for a file, I start with
less /etc/path # I see in tab-completion several files that might interest me, so I need to know more
ll /etc/path # See more info about file sizes, etc, so I can choose the file I want. Have to change 'less' to 'll'
less /etc/path/myfile.txt # Now that I know which file I want, I have to go back and change 'll' to 'less'
This is something I end up doing dozens of times a day, and starts to slow me down. Is there a command or script that does the likeliest scenario for you? For example
some-program /etc/path/to/file/or/directory
If the target is a text file or symlink to a text file, 'less /etc/path/to/.....' would be executed. But if it were anything else, e.g. directory, binary file, block device, it would do 'll /etc/path/to/......'
Of course if it does something you don't want, like 'less' on a file symlink but you want to see the symlink itself with 'll', then you just run 'll' manually.
Anything like this exist (I bet it's out there, it seems like such a common task)? Or am I just using Linux command line in an inefficient way and there are better ways to navigate through it...how do you do it? Or I am really the only one going back and forth in the command line all the time switching 'less' to 'll' and back again?
some-program()
if [[ $# > 1 ]]; then
echo "Only one argument supported at this time"
exit 1
fi
if [[ $# == 0 ]]; then
ELEMENT="."
else
ELEMENT="$1"
fi
if [[ -d $ELEMENT ]]; then
ls --color=auto -alF "$ELEMENT"
else
less "$ELEMENT"
fi
bash command-line
migrated from serverfault.com Mar 18 '13 at 16:39
This question came from our site for system and network administrators.
add a comment |
Is there any command to list the contents if passed a directory, but output the contents if it's a file?
For example, often when I am navigating the directory structure I will start typing into the terminal something like
ll /etc/path/ # I see a file in tab-completion that interests me, called myfile.txt
ll /etc/path/myfile.txt # I do the auto-completion to get it into the command line
less /etc/path/myfile.txt # Now I have to go back and change 'll' to 'less'
Alternatively, if I'm looking for a file, I start with
less /etc/path # I see in tab-completion several files that might interest me, so I need to know more
ll /etc/path # See more info about file sizes, etc, so I can choose the file I want. Have to change 'less' to 'll'
less /etc/path/myfile.txt # Now that I know which file I want, I have to go back and change 'll' to 'less'
This is something I end up doing dozens of times a day, and starts to slow me down. Is there a command or script that does the likeliest scenario for you? For example
some-program /etc/path/to/file/or/directory
If the target is a text file or symlink to a text file, 'less /etc/path/to/.....' would be executed. But if it were anything else, e.g. directory, binary file, block device, it would do 'll /etc/path/to/......'
Of course if it does something you don't want, like 'less' on a file symlink but you want to see the symlink itself with 'll', then you just run 'll' manually.
Anything like this exist (I bet it's out there, it seems like such a common task)? Or am I just using Linux command line in an inefficient way and there are better ways to navigate through it...how do you do it? Or I am really the only one going back and forth in the command line all the time switching 'less' to 'll' and back again?
some-program()
if [[ $# > 1 ]]; then
echo "Only one argument supported at this time"
exit 1
fi
if [[ $# == 0 ]]; then
ELEMENT="."
else
ELEMENT="$1"
fi
if [[ -d $ELEMENT ]]; then
ls --color=auto -alF "$ELEMENT"
else
less "$ELEMENT"
fi
bash command-line
migrated from serverfault.com Mar 18 '13 at 16:39
This question came from our site for system and network administrators.
Your script seems OK. It will useless
to show the file content if applied to a file, and it will list the directory if applied to a directory. I have one point only which is you defined a function and you did not invoke it. You just need to remove the first line and last one.
– Khaled
Mar 17 '13 at 12:20
Or put this inside your.profile
or.bashrc
– fboaventura
Mar 17 '13 at 15:41
Have you considered actually using your command history? Try the arrow keys on your keyboard...
– Michael Hampton
Mar 18 '13 at 11:36
add a comment |
Is there any command to list the contents if passed a directory, but output the contents if it's a file?
For example, often when I am navigating the directory structure I will start typing into the terminal something like
ll /etc/path/ # I see a file in tab-completion that interests me, called myfile.txt
ll /etc/path/myfile.txt # I do the auto-completion to get it into the command line
less /etc/path/myfile.txt # Now I have to go back and change 'll' to 'less'
Alternatively, if I'm looking for a file, I start with
less /etc/path # I see in tab-completion several files that might interest me, so I need to know more
ll /etc/path # See more info about file sizes, etc, so I can choose the file I want. Have to change 'less' to 'll'
less /etc/path/myfile.txt # Now that I know which file I want, I have to go back and change 'll' to 'less'
This is something I end up doing dozens of times a day, and starts to slow me down. Is there a command or script that does the likeliest scenario for you? For example
some-program /etc/path/to/file/or/directory
If the target is a text file or symlink to a text file, 'less /etc/path/to/.....' would be executed. But if it were anything else, e.g. directory, binary file, block device, it would do 'll /etc/path/to/......'
Of course if it does something you don't want, like 'less' on a file symlink but you want to see the symlink itself with 'll', then you just run 'll' manually.
Anything like this exist (I bet it's out there, it seems like such a common task)? Or am I just using Linux command line in an inefficient way and there are better ways to navigate through it...how do you do it? Or I am really the only one going back and forth in the command line all the time switching 'less' to 'll' and back again?
some-program()
if [[ $# > 1 ]]; then
echo "Only one argument supported at this time"
exit 1
fi
if [[ $# == 0 ]]; then
ELEMENT="."
else
ELEMENT="$1"
fi
if [[ -d $ELEMENT ]]; then
ls --color=auto -alF "$ELEMENT"
else
less "$ELEMENT"
fi
bash command-line
Is there any command to list the contents if passed a directory, but output the contents if it's a file?
For example, often when I am navigating the directory structure I will start typing into the terminal something like
ll /etc/path/ # I see a file in tab-completion that interests me, called myfile.txt
ll /etc/path/myfile.txt # I do the auto-completion to get it into the command line
less /etc/path/myfile.txt # Now I have to go back and change 'll' to 'less'
Alternatively, if I'm looking for a file, I start with
less /etc/path # I see in tab-completion several files that might interest me, so I need to know more
ll /etc/path # See more info about file sizes, etc, so I can choose the file I want. Have to change 'less' to 'll'
less /etc/path/myfile.txt # Now that I know which file I want, I have to go back and change 'll' to 'less'
This is something I end up doing dozens of times a day, and starts to slow me down. Is there a command or script that does the likeliest scenario for you? For example
some-program /etc/path/to/file/or/directory
If the target is a text file or symlink to a text file, 'less /etc/path/to/.....' would be executed. But if it were anything else, e.g. directory, binary file, block device, it would do 'll /etc/path/to/......'
Of course if it does something you don't want, like 'less' on a file symlink but you want to see the symlink itself with 'll', then you just run 'll' manually.
Anything like this exist (I bet it's out there, it seems like such a common task)? Or am I just using Linux command line in an inefficient way and there are better ways to navigate through it...how do you do it? Or I am really the only one going back and forth in the command line all the time switching 'less' to 'll' and back again?
some-program()
if [[ $# > 1 ]]; then
echo "Only one argument supported at this time"
exit 1
fi
if [[ $# == 0 ]]; then
ELEMENT="."
else
ELEMENT="$1"
fi
if [[ -d $ELEMENT ]]; then
ls --color=auto -alF "$ELEMENT"
else
less "$ELEMENT"
fi
bash command-line
bash command-line
edited 17 hours ago
Rui F Ribeiro
42.1k1483142
42.1k1483142
asked Mar 17 '13 at 11:35
user779159user779159
237210
237210
migrated from serverfault.com Mar 18 '13 at 16:39
This question came from our site for system and network administrators.
migrated from serverfault.com Mar 18 '13 at 16:39
This question came from our site for system and network administrators.
Your script seems OK. It will useless
to show the file content if applied to a file, and it will list the directory if applied to a directory. I have one point only which is you defined a function and you did not invoke it. You just need to remove the first line and last one.
– Khaled
Mar 17 '13 at 12:20
Or put this inside your.profile
or.bashrc
– fboaventura
Mar 17 '13 at 15:41
Have you considered actually using your command history? Try the arrow keys on your keyboard...
– Michael Hampton
Mar 18 '13 at 11:36
add a comment |
Your script seems OK. It will useless
to show the file content if applied to a file, and it will list the directory if applied to a directory. I have one point only which is you defined a function and you did not invoke it. You just need to remove the first line and last one.
– Khaled
Mar 17 '13 at 12:20
Or put this inside your.profile
or.bashrc
– fboaventura
Mar 17 '13 at 15:41
Have you considered actually using your command history? Try the arrow keys on your keyboard...
– Michael Hampton
Mar 18 '13 at 11:36
Your script seems OK. It will use
less
to show the file content if applied to a file, and it will list the directory if applied to a directory. I have one point only which is you defined a function and you did not invoke it. You just need to remove the first line and last one.– Khaled
Mar 17 '13 at 12:20
Your script seems OK. It will use
less
to show the file content if applied to a file, and it will list the directory if applied to a directory. I have one point only which is you defined a function and you did not invoke it. You just need to remove the first line and last one.– Khaled
Mar 17 '13 at 12:20
Or put this inside your
.profile
or .bashrc
– fboaventura
Mar 17 '13 at 15:41
Or put this inside your
.profile
or .bashrc
– fboaventura
Mar 17 '13 at 15:41
Have you considered actually using your command history? Try the arrow keys on your keyboard...
– Michael Hampton
Mar 18 '13 at 11:36
Have you considered actually using your command history? Try the arrow keys on your keyboard...
– Michael Hampton
Mar 18 '13 at 11:36
add a comment |
1 Answer
1
active
oldest
votes
less
has a really neat built-in feature that lets you do things like this. See the lesspipe
man page.
To use that feature to automatically show a directory listing of directories, do the following:
Set up
lesspipe
following the man page:eval "$(lesspipe)"
Create
~/.lessfilter
with the following contents and make it executable:#!/bin/sh
if [ -d "$1" ]; then
ls -alF "$1"
else
# We don't handle this format.
exit 1
fi
exit 0
This sets up a filter script run by lesspipe
to handle file types. It checks to see if what you're trying to view is a directory and, if so, runs ls
instead.
Your original request included color. That's unfortunately a bit harder, since less
doesn't show color by default and the output from the filter script is still piped through less
. You'd need to use -R
as a default option (which is probably not a good idea if you ever view a binary file) to do that. I don't think there's a way to have the lessfilter script tell less
to add a new option.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f68350%2fcommand-to-list-contents-if-directory-or-do-output-contents-if-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
less
has a really neat built-in feature that lets you do things like this. See the lesspipe
man page.
To use that feature to automatically show a directory listing of directories, do the following:
Set up
lesspipe
following the man page:eval "$(lesspipe)"
Create
~/.lessfilter
with the following contents and make it executable:#!/bin/sh
if [ -d "$1" ]; then
ls -alF "$1"
else
# We don't handle this format.
exit 1
fi
exit 0
This sets up a filter script run by lesspipe
to handle file types. It checks to see if what you're trying to view is a directory and, if so, runs ls
instead.
Your original request included color. That's unfortunately a bit harder, since less
doesn't show color by default and the output from the filter script is still piped through less
. You'd need to use -R
as a default option (which is probably not a good idea if you ever view a binary file) to do that. I don't think there's a way to have the lessfilter script tell less
to add a new option.
add a comment |
less
has a really neat built-in feature that lets you do things like this. See the lesspipe
man page.
To use that feature to automatically show a directory listing of directories, do the following:
Set up
lesspipe
following the man page:eval "$(lesspipe)"
Create
~/.lessfilter
with the following contents and make it executable:#!/bin/sh
if [ -d "$1" ]; then
ls -alF "$1"
else
# We don't handle this format.
exit 1
fi
exit 0
This sets up a filter script run by lesspipe
to handle file types. It checks to see if what you're trying to view is a directory and, if so, runs ls
instead.
Your original request included color. That's unfortunately a bit harder, since less
doesn't show color by default and the output from the filter script is still piped through less
. You'd need to use -R
as a default option (which is probably not a good idea if you ever view a binary file) to do that. I don't think there's a way to have the lessfilter script tell less
to add a new option.
add a comment |
less
has a really neat built-in feature that lets you do things like this. See the lesspipe
man page.
To use that feature to automatically show a directory listing of directories, do the following:
Set up
lesspipe
following the man page:eval "$(lesspipe)"
Create
~/.lessfilter
with the following contents and make it executable:#!/bin/sh
if [ -d "$1" ]; then
ls -alF "$1"
else
# We don't handle this format.
exit 1
fi
exit 0
This sets up a filter script run by lesspipe
to handle file types. It checks to see if what you're trying to view is a directory and, if so, runs ls
instead.
Your original request included color. That's unfortunately a bit harder, since less
doesn't show color by default and the output from the filter script is still piped through less
. You'd need to use -R
as a default option (which is probably not a good idea if you ever view a binary file) to do that. I don't think there's a way to have the lessfilter script tell less
to add a new option.
less
has a really neat built-in feature that lets you do things like this. See the lesspipe
man page.
To use that feature to automatically show a directory listing of directories, do the following:
Set up
lesspipe
following the man page:eval "$(lesspipe)"
Create
~/.lessfilter
with the following contents and make it executable:#!/bin/sh
if [ -d "$1" ]; then
ls -alF "$1"
else
# We don't handle this format.
exit 1
fi
exit 0
This sets up a filter script run by lesspipe
to handle file types. It checks to see if what you're trying to view is a directory and, if so, runs ls
instead.
Your original request included color. That's unfortunately a bit harder, since less
doesn't show color by default and the output from the filter script is still piped through less
. You'd need to use -R
as a default option (which is probably not a good idea if you ever view a binary file) to do that. I don't think there's a way to have the lessfilter script tell less
to add a new option.
answered Mar 18 '13 at 5:57
rrarra
30015
30015
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f68350%2fcommand-to-list-contents-if-directory-or-do-output-contents-if-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
-bash, command-line
Your script seems OK. It will use
less
to show the file content if applied to a file, and it will list the directory if applied to a directory. I have one point only which is you defined a function and you did not invoke it. You just need to remove the first line and last one.– Khaled
Mar 17 '13 at 12:20
Or put this inside your
.profile
or.bashrc
– fboaventura
Mar 17 '13 at 15:41
Have you considered actually using your command history? Try the arrow keys on your keyboard...
– Michael Hampton
Mar 18 '13 at 11:36