Skip to content

Latest commit

 

History

History
317 lines (258 loc) · 7.06 KB

File metadata and controls

317 lines (258 loc) · 7.06 KB

Linux Shell

Basic Linux Commands

Quick tour of basic commands before next step. Most commands are used frequently.

Nagivation

  1. Print a current working directory.
$ pwd
  1. Change directory
$ cd dest
$ cd ~
$ cd ..
  1. List files
$ ls 
$ ls -al
$ ls path

File Operation

  1. Copy
$ cp source dest
  1. Move file or change name
$ mv source dest
$ mv old new
  1. Remove file or directory
$ rm path
  1. Make directory
$ mkdir newdirectory
$ mkdir -p direcory_path_recursively
  1. Create a empty file or update a acces date
$ touch
  1. Get file information
$ file path
  1. Change permission(change mode)
$ chmod 755 file
$ chmod +x file
$ chmod u+x file

Process Management

  1. Process List
$ ps
  1. Send a signal to a process
$ kill -9 PID

Signal number with a name is in 'Signal numbering for standard signals' of here

System Information

  1. Print system information
$ uname -a
  1. Print disk usage
$ df -h
  1. Print memory usage
$ free -h
  1. Print open files
$ lsof
  1. Print network information
$ netstat -anp

If netstat is not found, install the package, 'net-tools'

$ sudo apt install net-tools
  1. Print resource usage1
$ top

Text Processing

  1. Count the number of words or lines
$ echo "shell programming" | wc -w     # word count
2

$ echo "shell programming" | wc -m     # character count
18                                     # the number of characters + 1(\0)

$ printf "a\nb\nc\nd\n" | wc -l        # line count
4
  1. Sort lines
$ printf "b\nd\nc\na\n" | sort
a
b
c
d
  1. Print unique line, remove duplication
$ printf "a\na\na\nb\n" | uniq
a
b
  1. Advanced text processing

Redirection

stdin stdout stderr
0 1 2
standard input standard output standard error
keyboard input terminal screen terminal screen
  1. Output Redirection
# Put the result of the left side command to a file, right side.
$ echo "hello world" > hello-world.txt
$ cat hello-world.txt
hello world

# Append contents to a file
$ echo "bye world" >> hello-world.txt
$ cat hello-world.txt
hello world
bye world
  1. Input Redirection
# Right side source is used as an input for a left side command
$ wc -l < hello-world.txt
1

Standard Error

$ ls /none 2> error.log  # An error message is redirected to error.log. 
$ cat error.log
ls: cannot access '/none': No such file or directory

Who/How select which stream is used?

Pipeline

If pipeline, '|', is used, the output of command1, left side, is taken as the input of command2, right side.

$ command1 | command2
$ ls /home | grep `whoami`

Others

$ history
$ !!
$ !$

Use a logical operator between commands.

$ ls; echo "done"

$ ls && echo "done"

$ ls || echo "done"

$ lb && echo "done"

$ lb || echo "done"

By now, we have completed to start talking about shell itself. Let's begin with shell in a real linux system.

What is Shell?

We have been heard 'Shell' a lot in the IT field, specifically linux or unix system. However, what is the Shell exactly meaning about?

Interface Hirarchy

There are a lot of interfaces in a computer system. To put the text in here, I use my keyboard. When I push a single keycap again and again, a signal is sent to the computer. Then, the computer prints the text I typed.

Shell is one of the interfaces in computer system.

As you know, there is an operating system in a modern computer system.

User -> Terminal -> Shell -> Operating System -> Hardware
     <-          <-       <-                  <-

Current Shell

To begin with what is your current shell.

$ echo $SHELL
/bin/zsh    # default shell on Mac

Creation of Shells

There are two kinds of shells on Linux, Login Shell and Non-Login Shell. Login shell is created when you logged in LInux, so Login shell should be created always if you are using Linux.

Shell Sessions

There are two kinds of Shell sessions, Login Session and Non-Login Session. What's difference between them? Let's talk about session first. The terminology, session, is commonly appeared in computer programming world. What is session?

Session

  1. Session is from connection between an user and a system or systems.
  2. Session is stateful.
  3. Session has time limit and scope.

The purpose of Shell profile is to load environment variables via running scripts for the current session. Before we step forward, look around the Shell session.

Login Shell Session

$ echo $0
-bash         # start with '-'

Non-Login Shell Session

$ bash      # start a new shell session
$ echo $0
bash

What's in profiles?

Actually, '/etc/profile' is not a configuration file but a shell script when running through a shell interpreter. If you are familiar with a shell script, you could understand what it means easily. The shell profiles contains only a few things to configure the shell session. The important thing that is done by a profile is to prepare a shell environment, for instance, ~~.

The order of loading profiles

Linux system divided by two systems, system-level and user-level. As almost computer system like, Linux also dedicates to improve efficiency and reduce not required overhead. Moreover Linux is an operating system so that security and consistency are also one of the most imporant features. I think, Linux Shell inherits these philosophy. When login shell session is created,

Shell Profiles

When starting a shell session, various configuration files are loaded. That configuration file is called a Shell profile.

For example, if the Shell is Bash,

  • /etc/profile
  • /etc/profile.d/*.sh
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile
  • ~/.bashrc
  • ...

As you can see, a few profiles are in the /etc directory, but the others are in the home direcotory. Can you expect the reason?

Psuedo Terminal Slave devices: PTS

$ who
user   pts/0   2024-11-11 12:00 (192.168.0.4)

Imaagine two users are loged in via ssh. Then, the first user was starting new shell session.

$ ps -aux | grep "pts/" | grep -E -v "pts/[0-9]$" | grep -E -v "color|ps -aux"
user1  ... pts/0 ... -bash
user1  ... pts/0 ... bash
user2  ... pts/1 ... -bash

(Option) Setup various shells on MacOS

Use a package manager, brew, to install various shells on MacOS.

# Install C Shell
$ brew install tcsh
$ man csh

# Install Korn Shell
$ brew install ksh93
$ man ksh

Resources

Footnotes

  1. You can use 'htop' also for more graphical printing. 'sudo apt install htop'