Friday, November 20, 2020

Create permanent BASH aliases in Linux

When creating an alias, for example
alias la="ls -la"
it exists until the terminal session is killed. When starting a new terminal window, the alias does not exist any more. The question is, how to create a "permanent" alias, one that exists in every terminal session?
Such aliases can be stored in the ~/.bash_aliases file.
 
That file is loaded by ~/.bashrc. The following lines need to be uncommented or added to enable the use of the ~/.bash_aliases file:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

 
The aliased command will be available on any new terminal. To have the aliased command on any existing terminal one need to source ~/.bashrc from that terminal:
source ~/.bashrc

A one liner would be:
echo "alias la='ls-la'" >> ~/.bash_aliases && source ~/.bash_aliases
 
Another option would be to add the alias line into ~/.bashrc or into ~/.profile / ~/.bash_profile for remote logins. If the command should be executed for all users, put it into /etc/bash.bashrc.
 
The function below can be added to the .bashrc file.
function permalias () 
{ 
  alias "$*";
  echo alias "$*" >> ~/.bash_aliases
}
Then open a new terminal or run source ~/.bashrc in your current terminal. You can now create permanent aliases by using the permalias command, for example permalias cls=clear.

~/.bashrc is run every time you open a new terminal, whereas ~/.bash_profile is not.

In order to update the file, run . ~/.bashrc or source ~/.bashrc

Function from askubuntu.com:

# -----------------------------------
#  Create a new permanent bash alias
#
#  @param $1 - NAME
#  @param $2 - DEFINITION
# -----------------------------------
new-alias () { 
  if [ -z "$1" ]; then
    echo "alias name:" && read NAME
  else
    NAME=$1
  fi

  if alias $NAME 2 > /dev/null > /dev/null; then
    echo "alias $NAME already exists - continue [y/n]?" && read YN
    case $YN in
      [Yy]* ) echo "okay, let's proceed.";;
      [Nn]* ) return;;
      * ) echo "invalid response." && return;;
    esac
  fi

  if [ -z "$2" ]; then
    echo "alias definition:" && read DEFINITION
  else
    DEFINITION="$2"
  fi

  if [ -f ~/.bash_aliases ]; then
    echo "alias $NAME=\"$DEFINITION\"" >> ~/.bash_aliases
  else
    echo "alias $NAME=\"$DEFINITION\"" >> ~/.bashrc
  fi

  alias $NAME="$DEFINITION"
}

Frequently Used Linux Commands

 

File Viewing

Command Function
cat Display the contents of file
less Page through files
head show the top portion of a file
more display screenfuls of a file
tail display bottom portion of a file
nl count the number of lines in a file
wc count the number of lines, words and characters in a file
od View a binary file
tee display output on stdout and write it to a file simultaneously

File Management

Command Function
ls display file attributes
stat display file attributes
wc count the number of lines, words and characters in a file
file identify file types
touch set the time stamp of a file or directory
chgrp change the group of a file
chmod change the permissions (mode) of a file
chown change the owner of a file
chattr change advanced file attributes
lsattr display advanced file attributes

File Manipulation

Command Function
awk pattern-matching, programming language
csplit split a file
cut display columns of a file
paste append columns in a file
dircmp compare two directories
find find files and directories
perl scripting language
sed Stream Editor
sort sort a file
tr translate chracters in a file
uniq find unique or repeated lines in a file
xargs process multiple arguements


File Editing

Command Function
vi text editor
emacs text editor
sed Stream Editor

Locate Files

Command Function
find find files and directories
which locate commands within your search path
whereis locate standard files

File Compression and Archiving

Command Function
gzip compress a file using GNU Zip
gunzip uncompress a file using GNU Zip
compress compress a file using UNIX compress
uncompress uncompress a file using UNIX compress
bzip2 compress a file using block-sorting file compressor
bunzip2 uncompress a file using block-sorting file compressor
zip compress a file using Windows/DOS zip
unzip uncompress a file using Windows/DOS zip
tar read/write (tape) archives
cpio copy files to and from archives
dump dump a disk to tape
restore restore a dump
mt tape control programm

File Comparison

Command Function
diff find differences in two files
cmp compare two files
comm compare sorted files
md5sum compute the MD5 checksum of a file
sum compute the checksum of a file

Disks and File Systems

Command Function
df display free space
du display disk usage
mount mount a filesystem
fsck check aand repair a filesystem
sync Flush disk caches


Printing

Command Function
lpr print files
lpq view the print queue
lprm Remove print jobs
lpc line printer control program


Process Management

Command Function
ps list processes
w list users’ processes
uptime view the system load, amount of time it has been running, etc.
top monitor processes
free display free memory
kill send signals to processes
killall kill processes by name
nice set a processes nice value
renice set the nice value of a running process.
at run a job at a specific time
crontab schedule repeated jobs
batch run a job as the system load premits
watch run a programm at specific intervals
sleep wiat for a specified interval of time

Host Information

Command Function
uname Print system information
hostname Print the system’s hostname
ifconfig Display or set network interface configuration
host lookup DNS information
nslookup lookup DNS information (deprecated)
whois Lookup domain registrants
ping Test reachability of a host
traceroute Display network path to a host
Character based web-browser

Networking Tools

Command Function
ssh Secure remote access
telnet Log into remote hosts
scp Securely copy files between hosts
ftp Copy files between hosts
wget Recursively download files from a remote host
lynx

File and Directory Basics

Command Function
cd change directory
cp copy files
file determine a file’s contents
ls list files or directories
ln make a link to a file
mkdir make a directory
mv move (rename) a file
rm remove a file
rmdir remove a directory
http://www.linux-tutorial.info