# Commonly used Linux commands
## Navigation
`which ??` where ?? has been installed; show the full path of shell commands
`pwd` where am I
`ls -a or -l` -a all, including .file ; -l more details
`ls *.txt` list all(*) .txt file
`cd -` Back to the previous folder
`cd ..` Go up one upper directory layer
`!cd` the last command with cd; ! can be used with the other commands
`cd ~` Go to $home director
`history` check commands that I have used
## Create, copy, move, rename folders
`mkdir` create a directory
`cp * .` copy somthing to . (one dot means here-this folder; 2 dots mean the upper layer)
`rm sth` remove something
`mv ../a ./b` rename or move ../a to ./b (file/folder, including path)
`mv a b` rename a to be b
## Preview, awk, wraggle file contents
`cat myfile.txt` show the file content
`cat myfile.txt | less` print less or `more` print more
check the last/first few lines
`tail myfile.txt`
`head myfile.txt`
`tail -n 4 myfile.txt` show the last four lines
`wc -l myfile.txt` check how many lines are there
`cat myfile.txt| cut -c 2-9 ` see the specific digits
`cat myfile.txt| awk '{if ($1>10), print $line}'` print $line if satisfy the condition
`cat myfile.txt| awk '{split($1,a,"/") split ($2,b,"/"); printf "%f %f \n%f %f", a[1], a[2], b[1], b[2]}'` split by given symbols and print
`cat myfile.txt| sort -nk 5 -r` sort by the 5th column (-r: reverse); -n (list according to numerical order); -k field (which column)
## Print, printf
`print $1" "$2`
`printf '%s %d %.2f', string_test,$2,$3` print with formats
`echo "my_notes" > note.txt` write "my_notes" in a new file note.txt
`echo "do not cover the existed content in olde file" >> note.txt` by two ">>"
## editing files: vi, vim
`vi myfile.txt` vi or vim to enter the vi mode
Use the following commands to play with the file
### edit, save, and leave
`i` start to edit, insert words, editing mode
`:wq!` save and leave =`:x`
`:q!` leave without save
`Esc` leave editing mode
`dd` delet the whole line
`yy` copy the whole line
`p` paste
`u` recover the previous step
>http://teacher.gtes.tp.edu.tw/~t0093/vi.htm
### find the keywords
`/keword`
`n` show next keyword
### go to specific line
The last line `G`
The #n line `1gg` `157gg`
The #n column in given line `n|`
### replace the given words by
`:%s/pattern/replacement` Replace the first occurrence of "pattern" with "replacement" on all lines
`:[range]s/{pattern}/{string}/[flags] [count]` The command looks for a "{pattern}" on each line in [range] and replaces it with a {string}. The instruction is multiplied by a positive integer called [count]
### print time
`date` system time variable
```
now=$(date +"%T")
echo "Current time : $now"
```
## Search for specific string in files or files in directory
`grep keywords *` find keywords in all files in this folder
`grep -c "linux" long.txt` count (-c) the number of times the pattern repeats
`sed SCRIPT INPUTFILE...`
`sed -i 's/to_be_replaced/replace/' /path/file.txt` replace string by new string in file.txt
`find [flags] [path] -name [expression]` searches for files in a directory hierarchy based on a regex expression
---
## Commands without pop up message or being interrupted
`nohup commands &` running at the background and would not be interrupted
## Monitoring activities
`top` check all operating procedures
`htop` check machine’s resources
`ps` check the processes your current shell session is running
`kill PID` kill project ID
`killall -u myname` kill all procedures started by -u username
## change authority for files or folders
`chmod +x file.txt` make the file exacutable
`chmod 700 myfolder` only myslef can rwx, other has no authority for all
r (read=4) w (write=2) x (execute=1) -(nono=0)
owner, ther group member, the others
> https://ftp.kh.edu.tw/Linux/Redhat/en_6.2/doc/gsg/s1-navigating-chmodnum.htm
> https://www.cs.nccu.edu.tw/~lien/UNIX/Command/chmod.htm
## ssh
`ssh -X, -Y`
`sshpass -p <my_password> ssh <username>@<hostname>`
`sshpass -p <my_password> scp <username>@<hostname>:folder/file .`
`ssh -t XXX001@140.112.XX.XX ssh -t yyyy@140.112.y.yy` go to y station through x station
## Download files
### from remote station
up/download single file
```
scp ./abc.dat wanlin001@xxx.xxx.xxx:/home/wanlin001/working_folder
scp wanlin001@xxx.xxx.xxx:/home/wanlin001/working_folder ./abc.dat
```
then, enter password
up/download folder
add -r ```scp -r ```
`rsync` synchronize data on disk with memory
`rsync -P -r`
`rsync -n source where_to_store` -n skip repeated files
-P, --progress,show progress
-a, --archive
-v, --verbose
-z, --compres
-u, --update
### from a link
`wget` url
`git clone` from github
### uncompress files
`tar`
Unzip various file formats
```
tar xvf FileName.tar
gzip -d FileName.gz
gunzip FileName.gz
tar zxvf FileName.tar.gz
bunzip2 FileName.bz
tar jxvf FileName.tar.bz
bzip2 -d FileName.bz2
bunzip2 FileName.bz2
tar jxvf FileName.tar.bz2
tar Jxvf FileName.tar.xz
tar Zxvf FileName.tar.Z
tar zxvf FileName.tgz
tar zxvf FileName.tar.tgz
unzip FileName.zip
rar e FileName.rar
tar -I zstd -xvf FileName.tar.zst
```
x-> uncompress
c-> compress
>https://note.drx.tw/2008/04/command.html
## Check hardware
`lsblk` list unpartitioned and unmounted disks
`free -m` RAM size
`lscpu` check cpu
`df` check hard disk storage
`df -h` show for human
`df -m` show by MB
`du /folder/` check given folder
`du -s -h /path_to/folder/` -s summary of this folder
`du -h ./folder/ | sort -rn` -rn (reverse), rank according to the size of files in this folder
`du -h folder -d 1` show how many layers of directories: `-d` or `--max-depth=`
`grep 'core id' /proc/cpuinfo |wc -l` check number of cpu
`tree` show the directory tree structure, sudo apt install tree
`tree /path/to/directory`
---
# Commands used by superusers
## Users
add users
`sudo useradd`
`sudo paaword`
`sudo useradd -f 45 newusr` only 45 days for this newusr
`sudo userdel --remove newusr` delet the user and its home directory
`sudo usermod -aG sudo newuser` assign newuser as a superuser
`su - newuser` switch user
`passwd` change password
`last username` login history
## Disk
> https://judysocute.com/blog/linux-%E7%A1%AC%E7%A2%9F%E6%8E%9B%E8%BC%89/
`sudo umount -f` -f force
`mount` mount
reformat
RAID `mdadm`
`ln -s ../foler/store ./link` create a soft link: the "link" will point to "store"
## Netfilter
IPtable
## git
```
git status
git add .
git commit -m "my comments"
git log
git show XDNFLOOGEGML(commit series)
git reset --hard XXXXX
```