Linux basics β
Commands in linux are all case sensitive. All folders and files are file in linux.
All about file system β
In linux world, all files and folders are files.
Root structure β
The root folder represents as /
which is the top-level directory in linux.
Start a new bash session, use ls
to check the structure of root.
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
folder | shortcut for |
---|---|
bin | binary |
boot | booting |
dev | device |
etc | editable text configuration |
home | home folder for each user |
lib | libraries |
mnt | mounted storages |
root | home folder for root user only |
... | ... |
- If working as root user
cd ~
will direct to/root/
instead of/home/
Absolute and Relative path β
Absolute path starts with /
which stands for root. Relative path starts with empty character or ./
, the dot stands for current folder.
Working with folders β
checking content under a folder β
We use ls
to represent the members in current path. By default, its output aligned horizontally, use ls -1
make it vertically aligned.
bin
boot
dev
etc
home
lib
lib32
lib64
libx32
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Additionally, use ls -l
to see more details of those members.
total 60
lrwxrwxrwx 1 root root 7 Oct 4 02:08 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Apr 18 2022 boot
drwxr-xr-x 5 root root 360 Nov 4 12:18 dev
drwxr-xr-x 1 root root 4096 Nov 4 12:23 etc
drwxr-xr-x 2 root root 4096 Apr 18 2022 home
lrwxrwxrwx 1 root root 7 Oct 4 02:08 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Oct 4 02:08 lib32 -> usr/lib32
lrwxrwxrwx 1 root root 9 Oct 4 02:08 lib64 -> usr/lib64
lrwxrwxrwx 1 root root 10 Oct 4 02:08 libx32 -> usr/libx32
drwxr-xr-x 2 root root 4096 Oct 4 02:08 media
drwxr-xr-x 2 root root 4096 Oct 4 02:08 mnt
drwxr-xr-x 2 root root 4096 Oct 4 02:08 opt
dr-xr-xr-x 266 root root 0 Nov 4 12:18 proc
drwx------ 1 root root 4096 Nov 4 13:53 root
drwxr-xr-x 5 root root 4096 Oct 4 02:12 run
lrwxrwxrwx 1 root root 8 Oct 4 02:08 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Oct 4 02:08 srv
dr-xr-xr-x 11 root root 0 Nov 4 12:18 sys
drwxrwxrwt 1 root root 4096 Nov 4 12:23 tmp
drwxr-xr-x 1 root root 4096 Oct 4 02:08 usr
drwxr-xr-x 1 root root 4096 Oct 4 02:12 var
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Manipulating files and folders β
command | usage |
---|---|
mkdir <path> | create folder |
mv <source_path> <dist_path> | move or rename |
touch <...file_path> | create file |
rm <file_path> | remove file |
rm -r <folder_path> | recursively remove folder |
... | ... |
Write and Read β
Read from files β
command | usage |
---|---|
cat <file_path> | read all text |
more <file_path> | read text with scrolling down |
less <file_path> | read text with scrolling up/down |
head -n <line_number> <file_path> | read first n lines |
tail -n <line_number> <file_path> | recursively remove folder |
... | ... |
Write to files β
Redirect output from command to a file, we use >
and >>
. >
overwrites the file, >>
appends text to the file. Any text output can be redirected to a file. Additionally, use nano or other packages to edit files.
cat file1.txt > file2.txt
ls -1 / > root_structure.txt
echo hello >> file3.txt
2
3
Matching text β
Use grep
(global regular expression print) to text content.
grep f* <...file_path> # starts with f
grep -i f* <...file_paths> # starts with f/F (case insensitive)
grep hello <...file_paths> # match the text/s
grep hello -r <path> # recursively match
2
3
4
Matching folders and files β
command | usage |
---|---|
ls -a <path> | list all members including hidden |
find <path> | recursively list all sub folders |
find <path> -type d -name "<pattern>" | recursively find list all folders matching to pattern |
find <path> -type f -iname "<pattern>" | recursively find list all files matching to pattern insensitive |
head -n <line_number> <file_path> | read first n lines |
tail -n <line_number> <file_path> | recursively remove folder |
... | ... |
Command Chaining β
Command sequence β
Use ;
to separate multiple command to execute one by one.
echo start; mkdir test; cd test; echo finished
Command And β
Only when previous command are successfully executed, can next command execute? Use &&
!
echo start && mkdir test && cd test && echo finished
Command Or β
If previous command was executed, next commands won't be executed! Use ||
!
mkdir test || echo "test does not exist"
Pipe operator β
Pipe args into a command using |
!
ls / | head -n 5
Environment Variables β
command | usage |
---|---|
printenv | print all environment variables |
printenv <variable_name> | print specific variable |
echo $<variable_name> | print specific variable |
export <variable_name>=<value> | add temporary environment variable to current session |
... | ... |
What is .bashrc β
The .bashrc
file in Linux is similar to the $PROFILE
file in PowerShell on Windows. Both serve as configuration files that allow users to customize their command-line environment and define various settings and preferences for their shell sessions. Just like the .bashrc
file in Linux, the $PROFILE
file in PowerShell is executed every time a new PowerShell session is initiated.
To reload the .bashrc
file in the current session, use source ~/.bashrc
Managing Processes β
Checking running process β
Check all process using ps
. Ignore the ps process because ps does include itself when retrieving result.
PID TTY TIME CMD
1 pts/0 00:00:00 bash
285 pts/0 00:00:00 ps
2
3
Asynchronous command β
The &
operator allows the command to run asynchronously, enabling you to continue using the terminal for other tasks. Take a sleep behind the scene!
root@34aee1a42ba0:/# sleep 100 &
[1] 286
root@34aee1a42ba0:/# ps
PID TTY TIME CMD
1 pts/0 00:00:00 bash
286 pts/0 00:00:00 sleep
287 pts/0 00:00:00 ps
2
3
4
5
6
7
Managing Users β
Add a user β
Use adduser
to check to options available.
-m
for example:
useradd -m byhpbsh
Who am I β
If you don't know which user you currently logged in, get username using command whoami
.
/app # whoami
root
/app #
2
3
Check all user info β
Check passwords in /etc/
cat /etc/passwd
We can see the new user, the x
means the password is stored somewhere else. The first 1000
is user id, the latter is group id.
...
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
byhpbsh:x:1000:1000::/home/byhpbsh:/bin/sh
2
3
4
5
6
7
8
9
Specify bash as shell for new user β
usermod -s bash byhpbsh
Recheck the /etc/passwd
Before:
byhpbsh:x:1000:1000::/home/byhpbsh:/bin/sh
After:
byhpbsh:x:1000:1000::/home/byhpbsh:bash
2
3
4
Starts a new terminal session using docker β
- Check running container using
docker ps
. - Copy the container id
- Run
docker exec -it -u <username> <container_id> bash
Now we can see the user marked with$
which indicates a regular user.
byhpbsh@34aee1a42ba0:/$
Managing Groups β
What is Group? β
All users in the same group have the same permissions.
Add new group β
groupadd <group_name>
- Let's add a dev group.
groupadd developers
- Then check group info in
/etc/group
root@34aee1a42ba0:/# cat /etc/group | grep developers
developers:x:1001:byhpbsh
2
Add user to group β
This command add a supplementary group for user byhpbsh
. A user can have multiple groups, and each user has a primary group named as the username.
usermod -G developers byhpbsh
Check groups of user β
groups <username>
root@34aee1a42ba0:/# groups byhpbsh
byhpbsh : byhpbsh developers
2
File Permissions β
Permission explained β
Let's create a new file sayHello.sh
in home folder that prints hello when executed.
root@34aee1a42ba0:/home# echo echo hello > sayHello.sh
To check the permissions of files , use ls -l
root@34aee1a42ba0:/home# ls -l
total 8
drwxr-x--- 2 byhpbsh byhpbsh 4096 Nov 5 07:50 byhpbsh
-rw-r--r-- 1 root root 11 Nov 5 08:36 sayHello.sh
2
3
4
File permissions are represented as 10 characters:
- if starts with
d
, indicating the file is a directory. - if starts with
-
, indicating the file is a file. - the rest 9 characters representing 3 permissions for 3 type of users
- first three for the user who owns the file
- second for the group owns the file
- last three for every user
r
for readablew
for writablex
for executable-
for none
Change permission β
Currently sayHello.sh
is not executable for any user.
-rw-r--r-- 1 root root 11 Nov 5 08:36 sayHello.sh
To make it executable for user owns the file, run chmod u+x <file_path>
, u
stands for user, +
stands for add correspond to -
that revoke the permission. For other users, run chmod o+x <file_path>
, for simplicity, chmod
supports syntax like chmod o+x+w-r <...files>
to alter permissions in one go.
root@34aee1a42ba0:/home# chmod u+x sayHello.sh
root@34aee1a42ba0:/home# ls -l
total 8
drwxr-x--- 2 byhpbsh byhpbsh 4096 Nov 5 07:50 byhpbsh
-rwxr--r-- 1 root root 11 Nov 5 08:36 sayHello.sh
root@34aee1a42ba0:/home# ./sayHello.sh
hello
2
3
4
5
6
7