361 lines
8.7 KiB
Markdown
361 lines
8.7 KiB
Markdown
# Linux
|
||
|
||
## Absolute Path vs Relative Path in Linux/Unix
|
||
One of this blog follower asked us that whatâs the difference between absolute and relative path?
|
||
|
||
To understand this we have to know what is a path in Linux.
|
||
### What is a path?
|
||
|
||
A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters.
|
||
What is an absolute path?
|
||
|
||
An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.
|
||
### Some examples of absolute path:
|
||
```bash
|
||
/var/ftp/pub
|
||
/etc/samba.smb.conf
|
||
/boot/grub/grub.conf
|
||
```
|
||
If you see all these paths started from / directory which is a root directory for every Linux/Unix machines.
|
||
### What is the relative path?
|
||
|
||
Relative path is defined as path related to the present working directory(pwd). Suppose I am located in /var/log and I want to change directory to /var/log/kernel. I can use relative path concept to change directory to kernel
|
||
|
||
changing directory to /var/log/kernel by using relative path concept.
|
||
|
||
> $ pwd/var/logcd kernel
|
||
|
||
Note: If you observe there is no / before kernel which indicates itâs a relative directory to present working directory.
|
||
|
||
Changing directory to /var/log/kernel using absolute path concept.
|
||
|
||
> $ cd /var/log/kernel
|
||
|
||
Note: We can use an absolute path from any location where as if you want to use relative path we should be present in a directory where we are going to specify relative to that present working directory.
|
||
|
||
Examples of relative path and absolute path for the same operation.
|
||
|
||
# Basic Linux Command
|
||
|
||
## help
|
||
- Man: an interface to the on-line reference manuals
|
||
> $ man ls
|
||
|
||
## Wildcard
|
||
* Show all picture (multiple char)
|
||
> $ ls pic*
|
||
|
||
? show only picture between 50 and 59 (only one char)
|
||
|
||
> $ ls pic5?.jpg
|
||
|
||
[] show only if char in [p-P]
|
||
|
||
> $ ls [pP]ic*
|
||
|
||
## Files and directory
|
||
|
||
- ls: list directory contents
|
||
> $ ls
|
||
|
||
> args = -l -a -r -t -h --help
|
||
|
||
|
||
- Pwd: output the current working directory
|
||
> $ pwd
|
||
|
||
- Cd: Change directory
|
||
> $ cd
|
||
|
||
back to previous folder
|
||
> $ cd -
|
||
|
||
|
||
- Mkdir: Make directory
|
||
> $ mkdir test1 test2 test3
|
||
|
||
> $ mkdir -p lol/lol/lol
|
||
|
||
-p to create parent directory if needed
|
||
|
||
- Rmdir: Remove directory
|
||
|
||
> rmdir filename
|
||
|
||
> rm -rf filename
|
||
|
||
Delete all of the files in the diectory including all subdirectories and tier contents
|
||
> $ rm -r \* .\*
|
||
|
||
Remove all files with the .doc extension recursively in the current working directory.
|
||
> $ rm \*\*/\*.doc
|
||
|
||
|
||
- Mv: Move directory (can be used to rename a file)
|
||
> mv file /opt/movedfile
|
||
|
||
- Cp: Copy file or directory
|
||
> $ cp file /opt/newcopiedfile
|
||
|
||
- Touch: change file timestamps but it can also create files
|
||
> $ touch nomdefichier.md
|
||
|
||
> $ touch pic{00..99}.jpeg # does not work
|
||
|
||
- Which: Searching the PATH for executable files matching the names of the arguments
|
||
> $ which ls
|
||
|
||
- File: file — determine file type
|
||
> $ file myfile
|
||
|
||
## file viewer
|
||
- More: file perusal filter for crt viewing
|
||
|
||
> $ more filename
|
||
|
||
- Less: opposite of more but Less is more ;)
|
||
|
||
> $ less filename
|
||
|
||
- Cat: concatenate files and print on the standard output
|
||
|
||
> $ cat filename
|
||
|
||
## Users
|
||
|
||
adduser, addgroup - add a user or group to the system
|
||
> sudo adduser steve
|
||
|
||
```bash
|
||
r4v3n@d3bi4n:~/Workspace/test$ sudo adduser steve
|
||
[sudo] password for r4v3n:
|
||
Sorry, try again.
|
||
[sudo] password for r4v3n:
|
||
Adding user `steve' ...
|
||
Adding new group `steve' (1002) ...
|
||
Adding new user `steve' (1002) with group `steve' ...
|
||
Creating home directory `/home/steve' ...
|
||
Copying files from `/etc/skel' ...
|
||
New password:
|
||
Retype new password:
|
||
passwd: password updated successfully
|
||
Changing the user information for steve
|
||
Enter the new value, or press ENTER for the default
|
||
Full Name []:
|
||
Room Number []:
|
||
Work Phone []:
|
||
Home Phone []:
|
||
Other []:
|
||
Is the information correct? [Y/n] y
|
||
|
||
```
|
||
|
||
user skeleton: skeleton files used for new user configuration
|
||
> $ ls /etc/skel/
|
||
|
||
```bash
|
||
─r4v3n at d3bi4n in /etc/skel
|
||
╰─» ls -a 0 (0.002s) < 05:21:24
|
||
./ ../ .bash_logout .bashrc .profile
|
||
|
||
```
|
||
|
||
- Change user
|
||
> $ su - marie
|
||
|
||
## Permissions
|
||
|
||
- Chmod: change file mode bits
|
||
|
||
A combination of the letters **ugoa** controls which users' access to the file will be changed:
|
||
- the user who owns it (u),
|
||
- other users in the file's group (g),
|
||
- other users not in the file's group (o),
|
||
- or all users (a).
|
||
|
||
If none of these are given, the effect is as if (a) were given, but bits that are set in the umask are not affected.
|
||
|
||
![file permissions](./img/file_permission.png)
|
||
|
||
```bash
|
||
marie@d3bi4n:~$ ls -l
|
||
total 4
|
||
-rw-r--r-- 1 marie marie 12 Apr 7 05:44 test
|
||
```
|
||
|
||
> $ chmod o-r mysecret
|
||
|
||
> -rw-r--**-**-- 1 marie marie 12 Apr 7 05:44 test
|
||
|
||
> $ chmod o+**rw** myscret
|
||
|
||
> -rw-r--**rw**- 1 marie marie 12 Apr 7 05:44 test
|
||
|
||
|
||
##Using chmod in absolute mode
|
||
|
||
In the absolute mode, permissions are represented in numeric form (octal system to be precise). In this system, each file permission is represented by a number.
|
||
|
||
- r (read) = 4
|
||
- w (write) = 2
|
||
- x (execute) = 1
|
||
- – (no permission) = 0
|
||
|
||
With these numeric values, you can combine them and thus one number can be used to represent the entire permission set.
|
||
|
||
| Number | Permission |
|
||
|--------|------------|
|
||
| 0 | — |
|
||
| 1 | –x |
|
||
| 2 | -w- |
|
||
| 3 (i.e. 2+1) | -wx |
|
||
| 4 | r– |
|
||
| 5 (i.e. 4+1) | r-x |
|
||
| 6 (i.e. 4+2) | rw- |
|
||
| 7 (i.e. 4+2+1) | rwx |
|
||
|
||
most commonly used:
|
||
755 644 600 640
|
||
|
||
Can you guess the file permission in numbers on agatha.txt file in our example so far? That’s right, it’s 764.
|
||
|
||
Now that you know what number represents which permission, let’s see how to change file permission using this knowledge.
|
||
|
||
Suppose you want to change the file permission on agatha.txt so that everyone can read and write but no one can execute it? In that case, you can use the chmod command like this:
|
||
|
||
> $ chmod 666 agatha.txt
|
||
|
||
## Danger : if a folder has not the X (executable) right => you cannot open it.
|
||
|
||
-R for recursive on folder
|
||
|
||
If you list agatha.txt now, you’ll see that the permission has been changed.
|
||
|
||
> -rw-rw-rw- 1 abhishek abhishek 457 Aug 10 11:55 agatha.txt
|
||
|
||
- Chown: change file owner and group
|
||
> $ sudo chown marie:marie agatha.txt
|
||
|
||
> -rw-rw-rw- 1 marie marie 457 Aug 10 11:56 agatha.txt
|
||
|
||
### Groups
|
||
|
||
Groups: print the groups a user is in
|
||
> $ groups
|
||
|
||
Adds user marie into steve group
|
||
> $ adduser marie steve
|
||
|
||
```bash
|
||
r4v3n@d3bi4n:~/Workspace/test$ sudo adduser marie steve
|
||
Adding user `marie' to group `steve' ...
|
||
Adding user marie to group steve
|
||
Done.
|
||
|
||
marie@d3bi4n:~$ groups
|
||
marie steve
|
||
```
|
||
|
||
## Sysadmin tools
|
||
|
||
- How to create a symbolic link in Linux
|
||
To create a symbolic link to target file from link name,
|
||
you can use the ln command with -s option like this:
|
||
|
||
> ln -s target_file link_name
|
||
|
||
The -s option is important here. It determines that the link is soft link. If you don’t use it, it will create a hard link. I’ll explain the difference between soft links and hard links in a different article.
|
||
|
||
- Htop: Interactive processes viewer
|
||
> $ htop
|
||
|
||
- Changer default shell
|
||
> $ vim /etc/passwd
|
||
```bash
|
||
steve:x:1002:1002:,,,:/home/steve:/bin/bash
|
||
steve:x:1002:1002:,,,:/home/steve:/bin/fish
|
||
|
||
```
|
||
|
||
- |: pipe send result of the first command to the second
|
||
> cat /etc/passwd **|** **grep** bash |**cut** -d ":" -f1
|
||
|
||
- Grep: print lines that match patterns
|
||
> $
|
||
|
||
Search for specific text with grep command
|
||
> $ grep -l example document1.txt document2.txt
|
||
|
||
> $ grep -l example \*.txt
|
||
|
||
grep as long as you include the -r (recursive) option in the command.
|
||
|
||
> $ grep -lr example /path/to/directory1/\*.txt /path/to/directory2
|
||
|
||
Or, to search the current directory and all subdirectories, omit the path at the end of the command.
|
||
|
||
> $ grep -lr example
|
||
|
||
|
||
- Cut: remove sections from each line of files
|
||
> $
|
||
|
||
- Wc: print newline, word, and byte counts for each file
|
||
> $ wc -l
|
||
|
||
```bash
|
||
$ realpath example.txt
|
||
/home/username/example.txt
|
||
```
|
||
|
||
## text editor
|
||
- Nano: Nano's ANOther editor, an enhanced free Pico clone
|
||
(simple text editor for noobies)
|
||
|
||
> $ nano
|
||
|
||
> $ nano filename
|
||
|
||
- VIM: vim - Vi IMproved, a programmer's text editor (PGM)
|
||
|
||
> $ vim
|
||
|
||
> $ vim filename
|
||
|
||
# APT
|
||
> $ apt install
|
||
> $ apt remove
|
||
> $ apt autoremove
|
||
> $ apt update
|
||
|
||
|
||
## Display & Destop Manager
|
||
|
||
* Architecture:
|
||
> BIOS -> GRUB -> Display Manager -> Desktop Environement
|
||
|
||
* Install Desktop Environement (GUI)
|
||
|
||
> $ tasksel
|
||
> $ apt install gnome
|
||
> $ apt remove gnome
|
||
|
||
* Reconfigurer le display manager
|
||
> $ sudo dpkg-reconfigure gdm3
|
||
|
||
* Installer le display manager
|
||
|
||
> $ sudo apt install lightdm
|
||
|
||
> $ sudo apt install gdm3
|
||
|
||
* remove Desktop environement
|
||
|
||
> $ sudo apt remove lightdm
|
||
|
||
# Services
|
||
- HTOP
|
||
- dhclient > get ip
|
||
- gnome networkmanager
|
||
- wpa_supplicant > encryption @ wifi
|