[LINUX] Write an alias-like function in .bashrc that becomes ls -lh when you do ls -l

Introduction

Since the size of the file displayed by ** ls -l ** is in bytes, it is very difficult to understand at a glance. I want to set an alias so that it will be automatically converted to KB, MB, GB display, but since it is not possible to set an alias with options, create a function in **. Bashrc ** to handle it. I made it.

environment

Ubuntu 16.04.5 LTS

procedure

Go home.

cd ~/

Open .bashrc.

vi .bashrc

Use ** Shift + g ** to move to the bottom and write the following ls function.

function ls () {
  if [[ "$@" =~ ^--color=auto.*l.*$ ]]; then
    command ls "$@" -h
  else
    command ls "$@"
  fi
}

Run .bashrc for your changes to take effect.

source .bashrc

Confirm that the unit display is OK no matter which of the following commands is executed. You can use the ** ls ** command as is.

ls -l
ls -la
ls -al

Commentary

** $ @ ** contains the ** argument ** of the ls command. If you search the top of the .bashrc file, you will find ** alias ls ='ls --color = auto'**, so if you do ** ls -l **, the contents of $ @ will be ** It will be picked up by --color = auto -l **. Match with a regular expression so that the option corresponds to -l or -la or -al. If it matches, the option **-h ** is added at the end.

Search for ** alias ls ** in .bashrc.

$grep "alias ls" > .bashrc
alias ls='ls --color=auto'

reference

https://tutorialmore.com/questions-603168.htm

Recommended Posts

Write an alias-like function in .bashrc that becomes ls -lh when you do ls -l
An app that you must put in Linux
What to do if you get an error when importing matplotlib in Python (Mac)
When you get an error in python scraping (requests)
What to do if you get an error when running "certbot renew" in CakePHP environment