IOriens / ioriens.github.io

https://Junjie.xyz
12 stars 2 forks source link

Linux Operations #13

Open IOriens opened 6 years ago

IOriens commented 6 years ago

Network

Find port in use

Show all port in use

netstat -an | grep LISTEN

Find a specific port

# use netstat
netstat -anp tcp | grep 3000

# For OSX "El Capitan" or if your netstat doesn't support -p, use lsof
lsof -i tcp:3000

DNS

Get ip address of one domain

dig mina.wiki

Flush Dns Cache

Flushing your DNS cache in Mac OS X and Linux

sudo killall -HUP mDNSResponder;sudo killall mDNSResponderHelper;sudo dscacheutil -flushcache

Process

kill process

kill -15 <PID>
pkill <name>

Recommend trying kill -15 before resorting to -9 for safety:

kill all vscode instances

kill -9 $(pgrep Electron)

kill port

kill -9 $(lsof -t -i:3000 -sTCP:LISTEN)

File System

mkdir

-p : make parents as needed

ln => make link

By default, ln makes hard links.

A hard link to a file is indistinguish-able from the original directory entry; any changes to a file are effec-tively independent of the name used to reference the file.

To make symbolic link, use ln -s

A symbolic link contains the name of the file to which it is linked. Symbolic links may span file systems and may refer to directories.

ls

image

output: https://unix.stackexchange.com/questions/103114/what-do-the-fields-in-ls-al-output-mean/103117 https://en.wikipedia.org/wiki/Unix_file_types

chomod

image

image

diff

file

diff file-1 file-2

folder

diff -rp hybrid/h5/src/service weweb/src/service > diff.diff

Copy to or paste from clipboard

# copy to
cat ~/Desktop/ded.html | pbcopy
pbcopy < ~/.ssh/id_rsa.pub

# paste from
pbpaste > ~/Documents/ded.html

How can I copy the output of a command directly into my clipboard?

Ref

Understanding Exit Codes and how to use them in bash scripts

Others

Bad interpreter

Error:

/bin/bash^M: bad interpreter: No such file or directory

Solution: In vim you could also use :set ff=unix and then save the file, or :set ff=dos to get DOS formatting again.

Sub Bash

Sub bashes's changed directory manipulation won't affect parent process.

beautify all the code in current dir

find . -type f -name '*.js' -not -path "./node_modules/*" -not -path "./modified_modules/*" -exec js-beautify -r -s 2 -p -f '{}' \;

run process as background and never die

nohup ./cow &

How to run process as background and never die?

Remove all node_module folders recursively

npm cache clean --force
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +

Remove all node_module folders recursively

IOriens commented 6 years ago

argc && argv

means what

argc == argument count argv == argument vector

Example

source

#include <stdio.h>

int main (int argc, char *argv[])
{

  return 0;
}

executing

gcc -o myprog myprog.c

result

argc
  4 
argv[0]
  gcc 
argv[1]
  -o 
argv[2]
  myprog 
argv[3]
  myprog.c

ref

argc and argv

IOriens commented 6 years ago

Exit Codes

On POSIX systems the standard convention is for the program to pass 0 for successful executions and 1 or higher for failed executions.

#!/bin/bash

touch /root/test 2> /dev/null

if [ $? -eq 0 ]
then
  echo "Successfully created file"
  exit 0
else
  echo "Could not create file" >&2
  exit 1
fi
IOriens commented 6 years ago

Hard & Symbolic Link

Here’s stuff one can do with hard links that would break a soft link:

Stuff that soft links do that hard links can’t:

image

ref

Symbolic links and hard links: creating, updating, deleting and all that

IOriens commented 6 years ago

Stream

Piping and Redirection!

> Save output to a file. >> Append output to a file. < Read input from a file. 2> Redirect error messages. | Send the output from one program as input to another program.

redirect error

The > operator redirects the output usually to a file but it can be to a device. You can also use>>to append.

If you don't specify a number then the standard output stream is assumed but you can also redirect errors

> file redirects stdout to file 1> file redirects stdout to file 2> file redirects stderr to file

&> file redirects stdout and stderr to file

/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.

IOriens commented 6 years ago

VIM

Shortcuts

image

Moving cursor like vim in the CLI

Just use vi-mode

IOriens commented 4 years ago

Systemctl

假设我们想要让一个名为 app 的应用开机启动

首先在 /etc/systemd/system/ 目录下创建一个 app.service 文件,下面以 frp 的启动配置为例:

[Unit]
Description=frps daemon
After=syslog.target  network.target
Wants=network.target

[Service]
Type=simple
ExecStart=/home/ioriens/bin/frp/frps -c /home/ioriens/bin/frp/frps.ini
Restart= always
RestartSec=1min
ExecStop=/usr/bin/killall frps

[Install]
WantedBy=multi-user.target

开机启动

systemctl enable app

取消开机启动

systemctl disable app

查看运行状态

systemctl status app