Linux Commands
explainshell.com - match command-line arguments to their help text

Grep

1
<Command Options>
2
-o, --only-matching: Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
3
-P, --perl-regexp: Interpret the pattern as a Perl-compatible regular expression (PCRE).
4
-v, --invert-match: Invert the sense of matching, to select non-matching lines.
5
-r, --recursive: Read all files under each directory, recursively, following symbolic links only if they are on the command line.
6
-l, --files-with-matches: Suppress normal output; instead print the name of each input file from which output would normally have been printed.
7
8
<Examples of Usage>
9
#Selected lines are those not matching any of the specified patterns.
10
grep -v ‘keyword’ test.txt
11
12
#Print only the mached parts with regular expression
13
grep -oP '\d{1,5}/open' allports.gnmap | sort -u
14
15
# 特定のキーワードを含まない行を表示
16
grep -v ‘keyword’ test.txt
17
18
# 検索したい文字列を含むファイルを表示
19
grep <keyword> -rl <path>
Copied!

Sort

1
<Command Options>
2
-u, --unique: output only the first of an equal run
3
4
<Examples of Usage>
5
#Print only the mached parts with regular expression and make each line unique
6
grep -oP '\d{1,5}/open' allports.gnmap | sort -u
Copied!

Wget

1
# 指定したURLからのファイルダウンロード
2
wget -O <url>
3
4
# 指定したURLからの再起的のファイルダウンロード
5
wget -r <url>
6
7
# 証明書の確認せずにコンテンツを取得
8
wget <url> --no-check-certificate
Copied!

Sed

1
<Command Options>
2
3
<Examples of Usage>
4
#Selected lines are those not matching any of the specified patterns.
5
6
cat srcip_area.csv | sed 's/"//g' |sed 's/,/ /g' | awk '{print $3, $4$5}' | awk '{arr[$1]+=$2} END {for (i in arr) {print i,arr[i]}}' | sort -nrk 2
7
8
cat file.txt | sed -e "s/<instance to find>/<instance to replace it with>/g" > output.txt
9
Copied!

Awk

1
* 「cat access_log | grep bullet.gif | awk -F'"' '{print $1 $4}’」
2
* -F’”'よりデリミターを指定して、文字を取得
3
* 「ls -lt | awk '$5>1000 {print $0}'」
4
* ファイルの容量が1000bytes以上の行のみを表示
5
* 「ls /Applications | awk 'length($0)>13{print}'」
6
* 長さが13を超える行数のみを表示
7
* 「cat access_log | awk '$9 == "404"{print $1}' | sort | uniq | wc -l」
8
* Awk のマッチング
9
* 「lsof -nPi | awk '/LISTEN/‘」
10
* 「ifconfig en1 | awk '/inet/{print substr($2,5,3)}'」
11
* print substr($2, 開始文字位置, 切り取り文字数)
12
* 「# ps -ef | awk '/sshd/ && !($3 == 1 || /sshd: hal[@ ]/) {print $2}'」
13
* &&条件
Copied!

Tr

1
* 「cat /etc/shells/ | tr a-z A-Z」
2
* 小文字を大文字に変換
3
* 「cat /etc/shells/ | tr -d ‘d’」
4
* dを削除
5
* 「ls -l | tr -s ' ' ‘,'」
6
* 連続したスペースを,に変換
7
* 「ls -l | tr -s ' ' '\t' | cut -f 1,5,9-」
Copied!

Tar

1
圧縮:tar cvzf tgzファイル名 圧縮対象ディレクトリ
2
解凍:ar xvzf tgzファイル名
Copied!

Ps

1
[email protected]:~ % ps -auwwx
2
3
-a: list the processes of all users on the system
4
-u: provide detailed information about each process
5
-x: list processes that have no controlling terminal, such as daemons
6
-ww: Wide output with unlimited width
Copied!

Find

1
<Command Options>
2
-name pattern: True if the last component of the pathname being examined matches pattern.
3
-type t: True if the file is of the specified type.
4
b block special
5
c character special
6
d directory
7
f regular file
8
l symbolic link
9
p FIFO
10
s socket
11
-iname pattern: Like -name, but the match is case insensitive.
12
-exec command: Execute command
13
14
15
<Examples of Usage>
16
find / -name .DS_Store > filelist.txt 2> errlist.txt
17
# 標準出力と標準エラー出力をそれぞれリダイレクトするfind [検索対象フォルダのパス]
18
19
-type f -name "[検索したい文字列]"
20
# 指定フォルダを配下を再帰的検索
21
22
find / -name example.txt 2>/dev/null
23
# 標準エラー出力を捨てる
24
25
find . -name "*.php" -type f -exec chmod 755 {} \;
26
27
find / -perm -o+w -type d
28
# Show all world readable directories
29
30
find / -perm -o+e -type d
31
# Show all world executable directories
32
33
find / -perm -1000 -type d 2>/dev/null
34
# Sticky bit - Only the owner of the directory or the owner of a file can delete or rename here.
35
36
find / -perm -g=s -type f 2>/dev/null
37
# SGID (chmod 2000) - run as the group, not the user who started it.
38
39
find / -perm -u=s -type f 2>/dev/null
40
# SUID (chmod 4000) - run as the owner, not the user who started it.
Copied!

Cut

1
Copied!

Kill

1
kill -9 $PID
Copied!

Sudo

1
<Command Options>
2
-l, --list: list user's privileges or check a specific command; use twice for longer format
3
-u, --user=user: run command (or edit file) as specified user name or ID
4
5
<Examples of Usage>
6
sudo -l
7
8
sudo -u scriptmanager bash
9
# execute command as specified user
Copied!

Extracting archives

1
tar xvfj test.tar.bz2
2
tar zxvf test.tar.gz
3
tar zxvf test.tar
4
gzip -d test.gz
5
unzip test.zip
6
zcat rockyou.txt.gz > rockyou.txt
Copied!

Compressing archives

1
tar -zcvf test.tar test
2
gzip test
3
zip -9 test.zip test
4
zip -r test.zip test/
Copied!

Copy files remotely

1
scp /path/to/local/file.txt [email protected]:/path/to/share # local to remote
2
scp -r [email protected]:/path/to/share /local/share # remote to local
3
cat ~/.ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'
Copied!

Bash

for

1
for i in $(seq 0 12); do echo -n '|base64 -d';done
2
|base64 -d|base64 -d|base64 -d|base64 -d|base64 -d|base64 -d|base64 -d|base64 -d|base64 -d|base64 -d|base64 -d|base64 -d|base64 -d
Copied!
Last modified 1yr ago