Quick tour of basic commands before next step. Most commands are used frequently.
- Print a current working directory.
$ pwd- Change directory
$ cd dest
$ cd ~
$ cd ..- List files
$ ls
$ ls -al
$ ls path- Copy
$ cp source dest- Move file or change name
$ mv source dest
$ mv old new- Remove file or directory
$ rm path- Make directory
$ mkdir newdirectory
$ mkdir -p direcory_path_recursively- Create a empty file or update a acces date
$ touch- Get file information
$ file path- Change permission(change mode)
$ chmod 755 file
$ chmod +x file
$ chmod u+x file- Process List
$ ps- Send a signal to a process
$ kill -9 PIDSignal number with a name is in 'Signal numbering for standard signals' of here
- Print system information
$ uname -a- Print disk usage
$ df -h- Print memory usage
$ free -h- Print open files
$ lsof- Print network information
$ netstat -anpIf netstat is not found, install the package, 'net-tools'
$ sudo apt install net-tools- Print resource usage1
$ top- 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- Sort lines
$ printf "b\nd\nc\na\n" | sort
a
b
c
d- Print unique line, remove duplication
$ printf "a\na\na\nb\n" | uniq
a
b- Advanced text processing
| stdin | stdout | stderr |
|---|---|---|
| 0 | 1 | 2 |
| standard input | standard output | standard error |
| keyboard input | terminal screen | terminal screen |
- 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- Input Redirection
# Right side source is used as an input for a left side command
$ wc -l < hello-world.txt
1$ ls /none 2> error.log # An error message is redirected to error.log.
$ cat error.log
ls: cannot access '/none': No such file or directoryIf pipeline, '|', is used, the output of command1, left side, is taken as the input of command2, right side.
$ command1 | command2
$ ls /home | grep `whoami`$ 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.
We have been heard 'Shell' a lot in the IT field, specifically linux or unix system. However, what is the Shell exactly meaning about?
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
<- <- <- <-
To begin with what is your current shell.
$ echo $SHELL
/bin/zsh # default shell on MacThere 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.
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 is from connection between an user and a system or systems.
- Session is stateful.
- 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.
$ echo $0
-bash # start with '-'$ bash # start a new shell session
$ echo $0
bashActually, '/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, ~~.
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,
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?
$ 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 ... -bashUse 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 kshFootnotes
-
You can use 'htop' also for more graphical printing. 'sudo apt install htop' ↩