bsdnerds.org

What is Linux shell?

A shell is a program (often written in C), which is a bridge for users to use Linux.

A shell is used by the user to interact with the operating system. There are a number of shells, they are listed below:

Shell programs

Here are a few shell versions, with bash being the default.

  • sh (Bourne Shell): is the original shell used by UNIX, and is available on every UNIX.

The Bourne Shell is quite good at shell programming, but doesn’t do as well as several other shells in terms of handling interactions with users.

Fully compatible with the Bourne Shell and adds many features to the Bourne Shell. Command complement, command editing, and command history can be provided. It also contains many of the benefits found in C Shell and Korn Shell, with a flexible and powerful editing interface, while being user friendly.

bash shell on linux
  • csh (C Shell): is a more suitable variant of Shell than Bourne Shell, which has a syntax similar to the C language.

  • Tcsh: is an extended version of the C Shell provided by Linux.

  • ksh (Korn Shell): combines the benefits of C Shell and Bourne Shell and is fully compatible with Bourne Shell.

  • pdksh: is an extension to ksh provided by Linux systems.

  • zsh: zsh is an extended Bourne shell with many new features including those of Bash, ksh, and tcsh (my personal favorite). It is often used with oh-my-zsh

About Bash

Some features of bash are:

  • History Bash records a history commands: bash can record previous commands in the ~/.bash_history file, only those that have been logged out since the last logout.

  • Tab auto-fill: Use tab to see auto-complete command or directory i.

  • alias command alias: you can set the alias of a command like:

alias ll='ls -al'
  • Job control: You can put certain tasks in the background to run, not many of them are described here

  • Program scripts: you can execute shell script files

  • Wildcards: You can use wildcards* when searching for related files or executing related commands.

  • Built-in command type: You can use the type command to see if a command is built-in to the bash

Bash scriptings

Create a new file (test.sh) using your favourite text editor: emacs, vim, nano etc.
Add this contents:

#!/bin/bash
echo "Hello World !"

#!/bin/bash/ tells the system that the program specified by the subsequent path is the Shell program that interprets this script file.

There are two ways to run a Shell script.

Method 1. As an enforceable procedure

Save the above code as test.sh and CD to the appropriate directory.

chmod +x ./test.sh #Encourage the script to execute
./test.sh #Execute script
bash script Note that it must be written as `./test.sh`, not `test.sh`, and the same goes for any other binary.

The linux system will look for test.sh in PATH, but only /bin, /sbin, /usr/bin, /usr/sbin, etc. are in PATH.

echo $PATH

Your current directory is usually not in PATH, so write test.sh and you will not find the command. /test.sh to tell the system to look for it in the current directory.

Method 2、As an interpreter parameter

The way this works is to run the interpreter directly, with the parameters being the filename of the shell script, such as

/bin/sh test.sh
/bin/php test.php

Bash commands

View file size, memory size, CPU information, hard drive space, etc.

# View current folder size
du -sh *

# Statistics the current folder (directory) size and sort by file size
du -sh * | sort -n

# view specified file size
du -sk filename

CPU information

These commands get information about your processor:

# Number of cpu:
cat /proc/cpuinfo | grep "physical id" | uniq | wc -l

# cpu cores:
cat /proc/cpuinfo | grep "cpu cores" | uniq

# cpu model:
cat /proc/cpuinfo | grep 'model name' |uniq

# cpu information:
cat /proc/cpuinfo

Memory view

Get information about your computers memory

cat /proc/meminfo | grep MemTotal

# View total memory
grep MemTotal /proc/meminfo

# View the amount of free memory 
grep MemFree /proc/meminfo

# View memory usage and swap zone usage 
free -m

Disk View

Navigation the Linux file system

fdisk -l | grep Disk

# list files in directory
ls

# enter directory
cd dir

# move up directory
cd ..

# current directory
pwd

Retrieve information about the Linux file system

# View usage by partition
df -h

# View the size of the specified directory 
du -sh

# View system load disks and partitions 
cat /proc/loadavg

# View partition status of the mount
mount | column -t

# View all partitions 
fdisk -l

# View all swap partitions
swapon -s

# View disk parameters (for IDE devices only) 
hdparm -i /dev/hda

view system information:

# View the linux system commands for CPU related parameters
cat /proc/cpuinfo

# system information command to view linux hard disk and partition information 
cat /proc/partitions

# linux system command to view linux system memory information
cat /proc/meminfo

# view version, similar to uname -r 
cat /proc/version

# view device io port
cat /proc/ioports

# view interrupt 
cat /proc/interrupts

# View information about the PCI device
cat /proc/PCI

# view all swap partitions
cat /proc/swaps

# lists all PCI devices 
lspci -TV

# List all linux system information commands for USB devices
lsusb -tv

# lists loaded kernel modules 
lsmod

More bash commands for your Linux or UNIX system

# linux system information to view kernel/OS/CPU information
uname -a

# View OS version
head -n l /etc/issue

# View linux system information command for computer name
hostname

# View environment variable resources
env

# View system uptime, number of users, load
uptime

# View network of IDE device detection status at boot
dmesg | grep IDE

# View properties of all network interfaces 
ifconfig

# View firewall settings
iptables -L

# View route table 
route -n

# View all listening ports
netstat -lntp

# View all established connections 
netstat -antp

# View network statistics process
netstat -s

# View all processes 
ps -ef

# Real-time display of process status users
top

# View active users 
w

# View specified user information
ID

# View user login log 
last

# View all system users
cut -d: -f1 /etc/passwd

# View all groups on the system 
cut -d: -f1 /etc/group

# View the current user's scheduled task service
crontab -l

# list all system services 
chkconfig -list

# List all started system services
chkconfig -list | grep on

# View all installed packages 
rpm -qa