19

When I enter command

ls

I see a list of files and directories in current directory, the list displayed is quite annoying.

Rather, I like to execute ls command with -1 option like

ls -1

My question is how to change default behavior of ls command such that it will always execute with -1 option. i.e how to display files and directories in one column with ls command.

Thanks.

5 Answers 5

20

One way would be by creating alias in your ~/.bashrc file:

alias l1='ls -1'

then by typing l1, ls -1 will be executed

4
  • How do you find the ~/.bashrc file? I found the "bash" application in Computer\bin, but not a text file with the name you mention.
    – AlainD
    Nov 21, 2015 at 19:40
  • @AlainD the .bashrc file is present in your home directory, not in /bin . The ~ expands to your home directory in bash, so the path would read something like /home/user/.bashrc
    – Jishnu
    Jan 22, 2016 at 4:46
  • I utilize alias lsl="ls -l" as I do not have to move from the home row to enter it.
    – dvnkiss
    Jun 21, 2019 at 1:31
  • On newer distros: ~/.profile
    – MrCalvin
    Dec 24, 2022 at 7:52
7

In your Home directory, open .bashrc file in editor and add alias ls='ls -1'.

First open the terminal ( Press ControlAltT), enter gedit ./.bashrc to open your .bashrc file in the editor.

Find the section that has some aliases for ls. In mine (stock 11.10) it looks like:

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

Add the following line after the ls aliases:

alias ls='ls -1'

Save the file, exit gedit and the terminal and reboot. Now the ls command should execute ls -1 by default.

2
  • 2
    This redefines "ls" to now operate as "ls -1" by default for this user. If you'd rather use a new command, then @zetah answer covers that.
    – James
    Feb 13, 2012 at 4:03
  • In my Home directory on uBuntu 14.04 LTS I only see a bunch of folder, and no .bashrc file.
    – AlainD
    Nov 21, 2015 at 19:45
3

Just to clarify something to @RobDavenport answer. You can't use a function to override a command that has the same name.

e.g. to add a default param to the ls command you can do :

alias ls='ls -1 $@'

This will add a new alias called ls so it will be called instead of the original command. It will add the -1 option and forward every parameter $@ to the original ls command.

You could also do

function ls_column () {
  ls -1 $@
}

It would have the same effect but you must use a different name for your function. Otherwise it will call itself again and again.

2

zetah's answer is the best. To elaborate:

Aliases are best used for short, simple, often used modifications of command default parameters. They are stored in memory (after being read from their source file), for better performance or repetitive use.

Functions are appropriate for more complex activity that are often used, and are also stored in memory.

Scripts are appropriate for the most complex and least often used commands.

See this question and answers on unix stackexchange - explains the difference in best use between aliases, functions, and scripts.

0

Add the following to your .bashrc (in your personal home dir). Some distributions might already have added this to this file (Mint for example)

    # enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls -lah --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

As you can see. you can customize not only ls but also grep etc. This is probably the closest to the question that was asked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .