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.
Ubuntu 16.04.5 LTS
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
** $ @ ** 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'
https://tutorialmore.com/questions-603168.htm
Recommended Posts