# Most common used command
###### tags: `linux`
## uid gid
雖然我們登入 Linux 主機的時候,輸入的是我們的帳號,但是,其實 Linux 主機並不會直接認識你的『帳號名稱』的,他僅認識 ID 啊~ID 就是一組號碼啦~ 主機對於數字比較有概念的,帳號只是為了讓人們容易記憶而已。 而您的 ID 與帳號的對應就在 /etc/passwd 當中哩。
### /etc/passwd
這個檔案的構造是這樣的:每一行都代表一個帳號, 有幾行就代表有幾個帳號在你的系統中!不過需要特別留意的是, 裡頭很多帳號本來就是系統中必須要的,我們可以簡稱他為系統帳號, 例如 bin, daemon, adm, nobody 等等,這些帳號是系統正常運作時所需要的,請不要隨意的殺掉他呢! 這個檔案的內容有點像這樣
每個程序都需要取得 uid 與 gid 來判斷權限的問題,所以, /etc/passwd 的權限必須要設定成為 -rw-r--r--

* 帳號名稱
* 密碼:後來就將這個欄位的密碼資料給他改放到 /etc/shadow ,x 這表示密碼已經被移動到 shadow 這個加密過後的檔案囉
* UID: root 0, 1-499 preserve for system
* GID
* 使用者資訊說明欄
* 家目錄:root 的家目錄在 /root ,所以當 root 登入之後,就會立刻跑到 /root 裡頭,預設的使用者家目錄在 /home/yourIDname
* Shell:預設SHELL,shell 可以用來替代成讓帳號無法登入的指令!那就是 /sbin/nologin 這個東西!這也可以用來製作純 pop 郵件帳號者的資料呢
---
### /etc/shadow
這個檔案的預設屬性是『-rw-------』或者是『-r--------』,亦即只有 root 才可以讀寫
後來發展出將密碼移動到 /etc/shadow 這個檔案分隔開來的技術, 而且還加入很多的密碼限制參數在 /etc/shadow 裡頭
```
root:$1$i9Ejldjfjio389u9sjl$jljsoi45QE/:12959:0:99999:7:::
bin:*:12959:0:99999:7:::
daemon:*:12959:0:99999:7:::
adm:*:12959:0:99999:7:::
```
* 帳號名稱
* 密碼
* 最近更動密碼的日期:1970 年 1 月 1 日作為 1
* 密碼不可被更動的天數: 0 的話, 表示密碼隨時可以更動的意思
* 密碼需要重新變更的天數: 99999 的話,那就表示密碼不需要重新輸入
* 密碼需要變更期限前的警告期限
* 密碼過期的恕限時間:密碼失效後,你還可以用這個密碼在 n 天內進行登入
* 帳號失效日期:這個帳號在此欄位規定的日期之後,將無法再使用
* 保留未來用
## whoami
print current login user

## man <command>
get the manual
1. press space to read next page.
2. q or z to leave
## clear
clean the terminal
1. clear -x : clean but retain the history
## pwd
print where you are (explicit path)
1. ~ ==/home/{user}

## ls
list the content of target folder
1. -a : all component
2. -l : long list format, contain R/W/E file permission, file owner, file group ,file size(bytes)

3. -lh : long list format with file size with unit

## cd
change directory
1. .. : parent folder
2. different user exist in home/{user}

ref: [The 50 Most Popular Linux & Terminal Commands - Full Course for Beginners](https://youtu.be/ZtqBQ68cfJc?t=3421)
## mkdir
make directory , -v for verbose
1. mkdir a b : create a/, b/
2. mkdir a/b -p : create nest folder a/b/ ,making the folders as needs.

## touch
create empty file, if file exists then updating the timestamp.
need subfoldername
1. touch text.txt: create empty file text.txt
2. touch 1.txt 2.txt : create multi empty file
## rmdir
remove empty directory(folder)
1. rmdir a : remove folder a if a is empty
## rm
remove file or directory
, -v for verbose
, -i for interactive, ask before remove
, -f for force remove without asking
1. rm 1.txt : remove 1.txt
2. rm -r a/b c/d: remove dir recursively

3. rm -r -i a/b : ask before remove each component

## open
open folder or target file
mac: open <target or dir>
linux : xdg-open <target or dir>
or
>sudo apt install xdg-utils
## mv
move or rename file,
1. mv \<file> \<path>: move file to path. ex. ../
2. mv \<file1> \<file2>: rename / overwrite file1 to file2
3. mv \<path\> \<path2>: if path2 exist, then move \<path\> to \<path2>,or rename path to path2
4. mv crust.etcMC* /home/out
5. or if etc is not literal string, but for example any three characters then:
mv crust.???MC* /home/out

6. mv Screen?* screen\ print/
## cp
copy file or directory
, -r recursive for folder content
1. cp \<dir\> \<dir2\> : copy dir to dir2(doesnt contain content)
2. cp \<dir\> \<dir2\> -r : copy dir to dir2

## head
pipe head of file
, -n \<num\> number of line
## tail
pipe tail of file
, -n \<num\> number of line
, -f keep trace the file output
## date
output now date
,-R : rfc5322

,-I\<formate\> : iso8601

## redirecting standard output
output > target : overwrite output to target, if target doesn't exist then create as needs.
output >> target :: concatenate output to the end of target file, if target doesn't exist then create as needs.
## cat
read file content
1.cat file_name :: 顯示filecontext
2.cat file_name | [more|less] :: 顯示多航/單行檔案內容
3.cat -n file_name :: 顯示filecontext 並顯示行數
4.cat -e file_name :: 顯示filecontext 並每行結尾 顯示"$"
5.cat -T file_name :: 顯示filecontext 並每TAB 顯示"^I"
6.cat file_name1 file_name2 :: 顯示多檔filecontext
7.cat file_name1 ; cat file_name2 ::顯示多檔案內容
8.cat >test2 ::create new file naming "test2"
9.cat test>test1 :: overwrite test1 with test content
10.cat test test1 test2 > test3 :: overwirte test3 with test1/test2/test3 content
11.cat test test1 test2 test3 | sort > test4 ::This will create a file test4 and the output of the cat command is piped to sort and the result will be redirected to a newly created file.
12.cat test>> test1 :: concate test1 content with test content
13.cat \<test2 :: use test2 file as input to pour to terminal
## less
read file in interactive mode as manual
, /{searchWord}
1. less \<file\>
## expansions
? sign = single char
* sign = wild card char
{} = var group
ex.


## echo
print the output the args pass to it
1. echo "hi"

2. echo "a=b" >> target.txt

3. echo * : output all current file name

4. echo *.txt : output all .txt file

5. echo *.?? : output all file with subname's lens = 2

6.single quote for string, double quote for analyze

## wc
info of the file,
print new line, words, byte count
, -c : byte count
, -m : char count
, -l : line count
, -w : words count
1. wc 1.txt: get the info of 1.txt

## sort
sort content in demanding order, only change the output
, -n : numerically
, -r : reverse direction
, -u : only get unique value
1. cat sorttest.txt | sort -nru

## uniq
pipe the unique value.(work as adjecent line),
recommend piping with 'sort'.
, -d : only print duplicate
, -u : print non-duplicate ones
, -C : print the count
1. \<output\> | uniq , reduce the adjecent duplicate value

2.

## diff
find difference between 2 file.
, -y : side by side comparing
, -u : unified, like git diff info
1. diff a b : a add diff go to b.

arrow(> or <) point to the change side
2322newchange is in sorttest.txt @ line 2
2322,434 is in sorttest1.txt @ line 2,3
`i am new line` is in sorttest1.txt @ line 19

1. diff a b -y

2. diff a b -u

## find
find specific file or folder recursively
, -name \<name_pattern\> : find file whose name in line with name_pattern
, -iname : case insensitive
, -type \<d,f\> : d for directory, f for file
, -not -path {path} : find exclude path
, -size {+,-} {filesize} : restrict the file size greater{+} or smaller{-} then filesize{100c(bytes) ,1M(megabytes)}
, -mtize {+,-}{num} : find modified time before,after previous num days

, -exec {command} '{}' \\; : {} mean each file , \\; is termination
1. find . -name '*.txt' : find all txt file from current to down.
2. find . -name '7': find file whose name is '7'
3. find . -name '*7*' : find files whose name containg '7'
4. find . -type d : find type is directory
5. find . -name '*a' -or -name '*b' : find path in line with {1} or {2}
6. find . -type f -size +100k : find file size greater than 100 kilobytes

7. find . -name '7' -exec cat {} \\; : find the corresponding file and operate cat to each file
8. find . -name '7' -exec ls -l {} \\; : find the corresponding file and list long format
## grep
grep {reg} {file}
find content in file
, -n :with line num
, -C {num}: with before and after num lines
, -r : recursively, replace file with folder
, -v : select non-matching line

```
grep "https://js.arcgis.com/3.35/dojox/gfx/Mover.js" -inr --include \*.html --include *.aspx ./
```
## du
disk usage
,-h : output with human-readable size
,-m : output with megabyte
,-g : output with gigabyte

## df
get usage information
,-h :output human-readable size

## history
find history used command
!{history num} : use the history num command
ps. no.630 command is 'du .'

1. history | grep 'ps' : grep the content including 'ps'
## ps
get process status
, axww :: get wrapper output , ww for unlimited width

ps axww

## top
top most occupied cpu process

## kill
kill program
, kill -l : view kill operation type
-sigterm, -15 :no.15 send TERM signal to pid, default type, gentle termination,
-SIGKILL, -9 : no.9 , brutal kill, like unplug the application

ex. kill {pid}
## jobs, bg, and fg
,ctrl c : shutdown
,ctrl z : suspend this process and detach
jobs : show the pending, running ,stopped programs

choose run programs ::
fg {num from jobs} : run program foreground
bg {num from jobs} : run program background

1. '&' means this process is running background.
2. top & : run top in background.
if there is only one job, then fg/bg don't need args.
## gzip
1. compress and delete original file
,-d : decompress .gz file
,-c : write output and keep original file, need '>' to point the target
,-k : same like -c, without point new filename
gzip 1.txt 2.txt 3.txt : build each .gz file
,-v : verbose, show the compress ratial

gzip -c dscs.txt > newpressed.gz : compress dscs.txt archive and rename as newpressed.gz, leave original file intact

## gunzip
unzipped .gz file, same as "gzip -d"
## tar
build archive file, no compress
,-c :create mode
,-f : use archive file or device ARCHIVE
,-x : extract mode
,-x -C {PATH} : extract to the path
,-t : list the content of the archive
,-z : zipped the archive or file
tar -cf archive.tar file1 file2 : build archive naming as archive.tar with file1 and file2
tar -xf archive.tar -C ~/desktop : extract achive.tar to ~/desktop
tar -czf archive.tar.gz 1.txt 2.txt : archive and zip the 1.txt and 2.txt to archive.tar.gz
## nano
## alias
1.build non-permanent custom command;
2.locallly build(only exists in this terminal) syntax: alias {custom_name}
3. build permant alias:
for bash(linux) :: home/{user}/.bashrc nano this file add alias syntax
for z-shell(mac):: nano .zshrc add alias syntax
open new terminal reload .bashrc
or run "source .bashrc"

3. 'commamd' vs "command"::
'echo $pwd' => single quote resolve the var when invoked
"echo $pwd" => double quote resolve the variable at the definition time.
## xargs
being an adaptor for pipe (|) input to command.
ex. delete file list in deadplayer.txt

rm cant take the pipe input directly

with xargs transfer input

ex2. find file large than 1M and ls -l these file

ex3. find file name is "pom.xml" contain "image"
find . -type f -name 'pom.xml' | xargs grep "image"
## ln
create link of file
1. hard link: create file point to inode(memory site),change each file will sync to those file, after delete source file, owing to both file point to memory site, the left one will persist and readable.
syntax: ln {source file} {target file}
ex. ln 1.txt 2.txt
2. soft link: create file point to the source file,change each file will sync to those file. after delete source file lead to the target file can't be read.

soft link can be use to attribute application version. ex. pythob command.
<b>the soft link file type is "l"</b>,
compare directory is "d"
,file is "-"

## sponge
read all the input before opening the output file for writing, most used for modulate content and write back to same file.
cf. redirect(>) will open file for writing
<b>grep redirect the same file, empty the file</b>

syntax:
1. sponge {filename} {content}
2. <p>{input} | sponge {file}</p>

## who
## su
## sudo
## passwd
## chown
change file owner /group
範例:將 initial-setup-ks.cfg 的擁有者與群組改回為root:
[root@study ~]# chown root:root initial-setup-ks.cfg

[鳥哥linux](https://linux.vbird.org/linux_basic/centos7/0210filepermission.php)
.bashsrc user as 7, group and other as 5
```
chmod u=rwx,go=rx .bashrc
```
all remove execuate right.
```
chmod a-x .bashrc
```
## chmod
## format
get the json structure, use --format to find the data in nest structure
```
docker inspect zookeeper --format='{{ .NetworkSettings.Networks.bridge.IPAddress }}'
```
###### tags: `linux50`