bash tips

- no title specified
Little-known bash commands for the SysAdmin Toolbox
 This is a user-information lookup program, which display information on system users.  If it's executed without an argument it will display a list of currently-logged in users:
Login     Name       Tty      Idle  Login Time   Office     Office Phone   Host
user1     user1     tty1     1:40  Jan  7 15:57                           (:0)
user1     user1     pts/0          Jan  7 15:58                           (
Given a username as an argument it will display extended information on that user:
# finger user1
Login: user1                         Name: user1
Directory: /home/user1               Shell: /bin/bash
On since Thu Jan  7 15:57 (CST) on tty1 from :0
    1 hour 40 minutes idle
On since Thu Jan  7 15:58 (CST) on pts/0 from
   4 seconds idle
No mail.
No Plan.
 Similarly, use the chfn command to change the information that the finger command displays.
       chfn [-f full-name] [-o office] [-p office-phone] [-h home-phone] [-u] [-v] [username]
       chfn  is  used to change your finger information.  This information is stored in the /etc/passwd file, and is
       displayed by the finger program.  The Linux finger command will display four pieces of information  that  can
       be changed by chfn: your real name, your work room and phone, and your home phone.
       Any  of  the  four pieces of information can be specified on the command line.  If no information is given on
       the command line, chfn enters interactive mode.
       In interactive mode, chfn will prompt for each field.  At a prompt, you can enter  the  new  information,  or
       just press return to leave the field unchanged.  Enter the keyword "none" to make the field blank.
       chfn supports non-local entries (kerberos, LDAP, etc.) if linked with libuser, otherwise use ypchfn, lchfn or
       any other implementation for non-local entries.
 chsh is a quick tool that allows the user or, more precisely, an administrator to change the default login shell for a user. For example,
# chsh -s /usr/sbin/noshell
If no shell is specified on the commandline, chsh prompts for one.
               The chage command changes the number of days between password changes and the date of the last password change. This information is used by the system to determine when a user must change his/her password.
Usage: chage [options] LOGIN
  -d, --lastday LAST_DAY        set date of last password change to LAST_DAY
  -E, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -h, --help                    display this help message and exit
  -I, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -l, --list                    show account aging information
  -m, --mindays MIN_DAYS        set minimum number of days before password
                                change to MIN_DAYS
  -M, --maxdays MAX_DAYS        set maximim number of days before password
                                change to MAX_DAYS
  -R, --root CHROOT_DIR         directory to chroot into
  -W, --warndays WARN_DAYS      set expiration warning days to WARN_DAYS
        The bash shell is configured, by default, to keep a history list of the user's commands.  This is usually located in the ~/.history file or ~/.bash_history .
From the commandline we can traverse the history list and search for specific commands with the Ctrl-R shortcut key (assuming you kept the default emacs key-bindings).  To continue searching backwards to the next item found matching your search criteria, simply repeat the shortcut key combination (Ctrl-R).  I admit I always forget this key combination as I am a Vim user; emacs' own search shortcut is, if I recall correctly, Ctrl-S but this will “freeze” the screen in a terminal until the user hits Ctrl-Q (XOFF).  A nifty option to add to your environment, if you don't want to keep finding duplicate items in your history, is to add the following to your ~/.bashrc:
        I'm sure you already know how to use the “bang” (!) in front of a command that you already executed some time before to repeat it:
# !cat
 But what if you just wanted to see what it was without executing it; just append :p without any spaces to the command:
# !cat:p
 To repeat the previous command, do:
# !$
 To replace a specific item in the previous command (e.g. car) with a different one (e.g. cat) and execute it again with that new item, do:
# car /some/file/too/long/to/repeat/again.txt
# ^car^cat
There are more options, tricks, use-cases that can be found in the file:
1.1.1 Event Designators
An event designator is a reference to a command line entry in the
history list.  Unless the reference is absolute, events are relative to
the current position in the history list.  
     Start a history substitution, except when followed by a space, tab,
     the end of the line, or `='.
     Refer to command line N.
     Refer to the command N lines back.
     Refer to the previous command.  This is a synonym for `!-1'.
     Refer to the most recent command preceding the current position in
     the history list starting with STRING.
     Refer to the most recent command preceding the current position in
     the history list containing STRING.  The trailing `?' may be
     omitted if the STRING is followed immediately by a newline.
     Quick Substitution.  Repeat the last command, replacing STRING1
     with STRING2.  Equivalent to `!!:s/STRING1/STRING2/'.
     The entire command line typed so far.
Some more terminal shortcut tips:
Ctrl-/                 Undo
Ctrl-g Ctrl-/         Redo
Ctrl-xx         Toggle the cursor between current position and the beginning
Ctrl-w  Cut/yank the word before the cursor
Alt-d                Delete the word after the cursor
Alt-t                 Swap the current word with the previous word
Ctrl-t                 Swap the previous 2 characters
Alt-u                 Capitalize (UPPERCASE) every character of word in front of cursor
Alt-l                 Make every character of word in front of cursor lowercase
Alt-c                 Captialize the character under the cursor
Alt-.                 Insert the last word from the previous command
To change the keybindings to/from Vi/Emacs:
set -o vi
set -o emacs
Finally, if you wanted to have bash print out what it does in the background, verbose (debug) do:
set -x
        Shows the mounted devices in a tree-like display.
# findmnt
TARGET                                SOURCE      FSTYPE      OPTIONS
/                                     /dev/mapper/fedora-root
│                                                 ext4        rw,relatime,seclabel
├─/sys                                sysfs       sysfs       rw,nosuid,nodev,noexec
│ ├─/sys/kernel/security              securityfs  securityfs  rw,nosuid,nodev,noexec
│ ├─/sys/fs/cgroup                    tmpfs       tmpfs       ro,nosuid,nodev,noexec
│ │ ├─/sys/fs/cgroup/devices          cgroup      cgroup      rw,nosuid,nodev,noexec
│ │ ├─/sys/fs/cgroup/memory           cgroup      cgroup      rw,nosuid,nodev,noexec
│ │ ├─/sys/fs/cgroup/freezer          cgroup      cgroup      rw,nosuid,nodev,noexec
│ │ ├─/sys/fs/cgroup/blkio            cgroup      cgroup      rw,nosuid,nodev,noexec
│ │ ├─/sys/fs/cgroup/cpu,cpuacct      cgroup      cgroup      rw,nosuid,nodev,noexec
│ │ ├─/sys/fs/cgroup/perf_event       cgroup      cgroup      rw,nosuid,nodev,noexec
│ │ ├─/sys/fs/cgroup/hugetlb          cgroup      cgroup      rw,nosuid,nodev,noexec
│ │ ├─/sys/fs/cgroup/cpuset           cgroup      cgroup      rw,nosuid,nodev,noexec
│ │ └─/sys/fs/cgroup/net_cls,net_prio cgroup      cgroup      rw,nosuid,nodev,noexec
│ ├─/sys/fs/pstore                    pstore      pstore      rw,nosuid,nodev,noexec
│ ├─/sys/fs/selinux                   selinuxfs   selinuxfs   rw,relatime
│ ├─/sys/kernel/debug                 debugfs     debugfs     rw,relatime,seclabel
│ ├─/sys/kernel/config                configfs    configfs    rw,relatime
│ └─/sys/fs/fuse/connections          fusectl     fusectl     rw,relatime
├─/proc                               proc        proc        rw,nosuid
│ │ └─/proc/sys/fs/binfmt_misc        binfmt_misc binfmt_misc rw,relatime
│ └─/proc/fs/nfsd                     nfsd        nfsd        rw,relatime
├─/dev                                devtmpfs    devtmpfs    rw,nosuid,seclabel
│ ├─/dev/shm                          tmpfs       tmpfs       rw,nosuid,nodev,seclabel
│ ├─/dev/hugepages                    hugetlbfs   hugetlbfs   rw,relatime,seclabel
│ └─/dev/mqueue                       mqueue      mqueue      rw,relatime,seclabel
├─/tmp                                tmpfs       tmpfs       rw,seclabel


The following is a native bash script to output the IP only of an interface.

NOTE: Make sure to change the interface name to correspond with yours.

ipaddr=$(/sbin/ifconfig eth0) ; ipaddr=${ipaddr/*inet /} ipaddr=${ipaddr/ */}; echo $ipaddr

Popular posts from this blog

Centos 7 pulseaudio

RHEL 7 and CentOS 7 syslog Rate Limit

Password Policy in RHEL 7