Linux: Basic Command Introduction II
1) Command Name: cat
Command Derived From: concatenate and display files
Command Path: /bin/cat
Command Auth: all users
Command Function: display the content of the file
Command Syntax: cat [file name]
Example:
cat epl-v10.html -> If the file is too long, then there may be overflow in current display window.
2) Command Name: more
Command Derived From: more
Command Path: /bin/more
Command Auth: all users
Command Function: display all the content of the file by paging
Command Syntax: more [file name]
(Space) or f -> show next page
(Enter) -> show next line
(q) or (Q) -> quit
Example:
more epl-v10.html -> If the file is too long, then there may be paging in current display window.
-> Then we should use (Space or f) and (Enter) and (Q or q) to browse all the content.
3) Command Name: head
Command Derived From: head
Command Path: /bin/head
Command Auth: all users
Command Function: display the specific lines from the head for the content of the file
Command Syntax: head -XX [file name]
-XX means the number of lines. If we didn't specific the count, then it is 10 by defalut.
Example:
head -100 epl-v10.html -> Only display the first 100 lines for the file.
4) Command Name: tail
Command Derived From: tail
Command Path: /bin/tail
Command Auth: all users
Command Function: display the specific lines from the tail for the content of the file
Command Syntax: head -XX [file name]
-f [file name]
-XX means the number of lines. If we didn't specific the count, then it is 10 by defalut.
-f means the dynamic display of file content. Is very useful for inspecting running log
Example:
tail -100 epl-v10.html -> Only display the last 100 lines for the file.
tail -f log.log -> As long as the file is updated, the display.
5) Command Name: ln
Command Derived From: link
Command Path: /bin/ln
Command Auth: all users
Command Function: create link file for a specific file
Command Syntax: ln -s [source file] [dest file]
-s create soft link
Example:
ln -s /etc/issue /etc/issue.soft -> Create soft link for file etc/issue
ln /etc/issue /etc/issue.hard -> Create hard link for file etc/issue
Explanation:
1) Soft link:
administrator@ubuntu:~/Downloads/eclipse/configuration$ ls -l config.ini.soft config.ini
-rw-rw-r-- 1 administrator administrator 862 Jun 14 10:52 config.ini
lrwxrwxrwx 1 administrator administrator 10 Sep 18 08:04 config.ini.soft -> config.ini
We can find out the difference between soft link file and normal file.
1) The auth of soft link file is 'rwx' for all user.
But it doesn't matter as when we click soft link, we are nevagated into the source.
So we don't care the auth of soft link file.
2) The timestamp for soft link file.
It is the time that this soft link file is created and not the time that the source file is created.
3) Soft link is just like 'shortcut' in Windows.
2) Hard link:
administrator@ubuntu:~/Downloads/eclipse/configuration$ ls -l config.ini config.ini.hard
-rw-rw-r-- 2 administrator administrator 862 Jun 14 10:52 config.ini
-rw-rw-r-- 2 administrator administrator 862 Jun 14 10:52 config.ini.hard
We can find out that the two files are exactly the same.
It is just like copy config.ini.
1) But we should pay attention that the two files are synchronous.
That means whatever we did to one file, this modification will reflect on the other file.
2) Also, if we "cp config.ini config.ini.hard" then the time stamp for config.ini.hard would be different
from config.ini.
if we "cp -p config.ini config.ini.hard" then the time stamp for config.ini.har would be the same with
config.ini.
3) Difference beteween soft link & hard link:
1) If we rm sourcefile, we cannot visit soft link file any more. But we can visit hard link file.
2) But why hard link file can be synchronous with source file?
Linux kernel cannot recognize object that is not digit. So any object that would be handled by kernel should
have a digit representation. For file, that digit is inode. For user, that digit is uid. For usergroup, that digit is gid.
For process, that digit is pid. Kernel cannot recoginze/handle object that doesn't have a digit representation.
administrator@ubuntu:~/Test3$ ls -i
531462 hello.hard 531462 hello.ini 531463 hello.soft
We can find out that the inode for hard link file and source file is the same.
This single inode will reflect into two files hello.hard and hello.ini.
When we rm hello.ini or hello.hard, we only cut a branch of this reflection and will not affect the other branch.
3) Hard link file cannot be generated across the file system.
6) Command Name: chmod
Command Derived From: change the permissions mode of a file
Command Path: /bin/chmod
Command Auth: all users
Command Function: change the permissions of file or dir
Command Syntax: chmod [{ugo}{+-=}{rwx}] [file name or folder name]
[mode=421] [file name or folder name]
Example:
chmod u+x hello.ini -> Add x permission to the file owner.
administrator@ubuntu:~/Test3$ ls -l hello.ini
-rwxrw-r-- 1 administrator administrator 27 Sep 18 08:27 hello.ini
chmod g+x hello.ini
administrator@ubuntu:~/Test3$ ls -l hello.ini
-rwxrwxr-- 1 administrator administrator 27 Sep 18 08:27 hello.ini
chmod g-x hello.ini
administrator@ubuntu:~/Test3$ ls -l hello.ini
-rwxrw-r-- 1 administrator administrator 27 Sep 18 08:27 hello.ini
u - means the owner
g - means group that it belongs to
o - means others
Example2:
r = 4 w = 2 x = 1
rwxr-xr-- represents: 754
rw-r-x--x represents: 651
This kind of representation is very important cause it is commonly used in shell script.
administrator@ubuntu:~/Test3$ chmod 766 hello.ini
administrator@ubuntu:~/Test3$ ls -l hello.ini
-rwxrw-rw- 1 administrator administrator 27 Sep 18 08:27 hello.ini
administrator@ubuntu:~/Test3$ mkdir pre
administrator@ubuntu:~/Test3$ ls -ld pre/
drwxrwxr-x 2 administrator administrator 4096 Sep 19 11:03 pre/
administrator@ubuntu:~/Test3$ touch pre/test.java
administrator@ubuntu:~/Test3$ ls -l pre/test.java
-rw-rw-r-- 1 administrator administrator 0 Sep 19 11:05 pre/test.java
administrator@ubuntu:~/Test3$ chmod 777 pre/test.java
administrator@ubuntu:~/Test3$ ls -l pre/test.java
-rwxrwxrwx 1 administrator administrator 0 Sep 19 11:05 pre/test.java
administrator@ubuntu:~/Test3$ chmod 754 pre/
administrator@ubuntu:~/Test3$ ls -ld pre/
drwxr-xr-- 2 administrator administrator 4096 Sep 19 11:05 pre/
Q: Can other user(o) delete this pre/test.java?
To be examined. --> Other user cannot delete pre/test.java!
administrator@ubuntu:~/Test3$ ls -ld pre
drwxr-xr-x 2 administrator administrator 4096 Sep 19 11:05 pre
administrator@ubuntu:~/Test3$ ls -l pre/test.java
-rwxrwxrwx 1 administrator administrator 0 Sep 19 11:05 pre/test.java
administrator@ubuntu:~/Test3$ chmod 777 pre/
administrator@ubuntu:~/Test3$ chmod 644 pre/test.java
administrator@ubuntu:~/Test3$ ls -ld pre
drwxrwxrwx 2 administrator administrator 4096 Sep 19 11:05 pre
administrator@ubuntu:~/Test3$ ls -l pre/test.java
-rw-r--r-- 1 administrator administrator 0 Sep 19 11:05 pre/test.java
Q: Can other user(o) delete this pre/test.java?
To be examined. --> Other user can delete pre/test.java!
WHY?
We need a better understanding of permission in Linux.
We should pay attention that rwx means different for file and folder.
For file:
r----cat, more, head, tail
w---vi, gedit, echo
x---command, script...
For folder:
r----ls
w--touch, mkdir, rm
x---cd
As convention, r and x are tightly bind when it comes to the permission of a folder.
If we can ls this folder, then we should be able to cd into this folder.
If we can cd into this folder, but we cannot ls this folder, that would be jokey.
7) Command Name: chown
Command Derived From: change file ownership
Command Path: /bin/chown
Command Auth: all users
Command Function: change file or dir's owner
Command Syntax: chown [username] [filename]
Example:
chown administrator test.java ->Change the owner of file test.java to administrator
Q: Who can execute chown for a specific file or folder?
A: