Linux使用指南
1. 文件操作
rm -rf directory 强制删除文件夹和文件
rm -f filename
sudo chown ljh:ljh directory //设置用户ljh拥有文件夹权限
sudo chomd 775 directory //设置当前用户可读可写
pwd 显示目前的目录
mkdir 创建新的目录
rmdir 删除一个空的目录
cp 复制文件或者目录
mv 移动文件或者目录,或者重命名
1.1 scp
// 本地文件 拷贝到远程
scp 1.txt root@47.92.243.226:/root/ljh
// 远程拷贝到本地
scp remote@47.92.243.226:/usr/local/sin.sh /home/administrator
2. 用户和用户组管理
2.1 新增新的用户
useradd 选项 用户名
选项:
-c comment 指定一段注释性描述。
-d 目录 指定用户主目录,如果此目录不存在,则同时使用-m选项,可以创建主目录。
-g 用户组 指定用户所属的用户组。
-G 用户组,用户组 指定用户所属的附加组。
-s Shell文件 指定用户的登录Shell。
-u 用户号 指定用户的用户号,如果同时有-o选项,则可以重复使用其他用户的标识号。
demo: useradd –d /home/sam -m sam
useradd -s /bin/sh -g group –G adm,root gem
2.2 删除用户
userdel 选项 用户名
选项:
-r 同时删除用户的主目录
demo: userdel -r sam
此命令删除用户sam在系统文件中(主要是/etc/passwd, /etc/shadow, /etc/group等)的记录,同时删除用户的主目录。
2.3 修改账号
usermod 选项 用户名
选项跟新增用户选项一致。
demo: usermod -s /bin/ksh -d /home/z –g developer sam
此命令将用户sam的登录Shell修改为ksh,主目录改为/home/z,用户组改为developer。
2.4 用户口令管理
指定和修改用户口令的Shell命令是passwd。超级用户可以为自己和其他用户指定口令,普通用户只能用它修改自己的口令。命令的格式为:
passwd 选项 用户名
选项:
-l 锁定口令,即禁用账号。
-u 口令解锁。
-d 使账号无口令。
-f 强迫用户下次登录时修改口令。
3. linux磁盘管理
Linux磁盘管理常用三个命令为df、du和fdisk。
df:列出文件系统的整体磁盘使用量
du:检查磁盘空间使用量
fdisk:用于磁盘分区
df [-ahikHTm] [目录或文件名]
选项:
-a :列出所有的文件系统,包括系统特有的 /proc 等文件系统;
-k :以 KBytes 的容量显示各文件系统;
-m :以 MBytes 的容量显示各文件系统;
-h :以人们较易阅读的 GBytes, MBytes, KBytes 等格式自行显示;
-H :以 M=1000K 取代 M=1024K 的进位方式;
-T :显示文件系统类型, 连同该 partition 的 filesystem 名称 (例如 ext3) 也列出;
-i :不用硬盘容量,而以 inode 的数量来显示
du [-ahskm] 文件或目录名称
选项:
-a :列出所有的文件与目录容量,因为默认仅统计目录底下的文件量而已。
-h :以人们较易读的容量格式 (G/M) 显示;
-s :列出总量而已,而不列出每个各别的目录占用容量;
-S :不包括子目录下的总计,与 -s 有点差别。
-k :以 KBytes 列出容量显示;
-m :以 MBytes 列出容量显示;
fdisk [-l] 装置名称
-l :输出后面接的装置所有的分区内容。若仅有 fdisk -l 时, 则系统将会把整个系统内能够搜寻到的装置的分区均列出来。
4. shell脚本
shell脚本是在linux的shell中运行的,所以称为shell脚本。事实上,shell脚本就是一些命令的集合。
假如完成某个需求需要一口气输入10条命令,对于简单的命令,我们可以直接在shell窗口中输入,但如果是比较长且复杂的命令,一次一次敲就会显得很麻烦。我们可以把这10条命令都记录到一个文档中,然后去调用文档中的命令,这样就能一步操作完成,以后再遇到这个需求的时候也只需要调用这个文档就行了,这个文档就是shell脚本,只是这个文档有它特殊的格式。
shell脚本能帮我们很方便地管理服务器,我们可以指定一个任务计划,定时去执行某个shell脚本以满足需求。在正式编写shell脚本之前,建议将所有自定义的脚本放到 /usr/local/sbin/ 目录下,一是可以更好地管理文档,二是方便后面的维护。
4.1 shell的创建和执行
- 创建:
#!/bin/bash
## This is my first shell script.
## Writen by ljh 2020-09-15
date
echo "Hello World!"
#!
开头表示使用/bin/bash这个解释器来执行
echo 用于向窗口输出文本。
- 执行:
// 将上面的代码保存为 test.sh,并 cd 到相应目录:
chmod +x ./test.sh #使脚本具有执行权限
./test.sh #执行脚本
一定要写成 ./test.sh,而不是 test.sh,运行其它二进制的程序也一样,直接写 test.sh,linux 系统会去 PATH 里寻找有没有叫 test.sh 的文件。要用 ./test.sh 告诉系统说,就在当前目录找。
/bin/sh test.sh
直接作为解释器参数来执行
5. ps 命令
ps -ef|grep redis # 显示所有进程信息,连同命令行,在过滤为redis
ps aux|grep nginx # 查看nginx路径, 可以使用which nginx
/usr/sbin/nginx -t # 查看nginx 配置文件路径
防火墙
systemctl status firewalld #查看防火墙状态
systemctl stop firewalld
systemctl start firewalld
# 对外开放端口
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd -reload # 重启防火墙
firewall-cmd --query-port=9200/tcp # 查询端口是否开放
# 查看开放的端口号
firewall-cmd --list-all
cat 命令
sage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit
Examples:
cat f - g Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/cat>
wc
wc指令比较实用,可以统计文件中的字节数、字符数、行数、字数等
。
Usage: wc [OPTION]... [FILE]...
or: wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified. A word is a non-zero-length sequence of
characters delimited by white space.
With no FILE, or when FILE is -, read standard input.
The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
-c, --bytes print the byte counts
-m, --chars print the character counts
-l, --lines print the newline counts
--files0-from=F read input from the files specified by
NUL-terminated names in file F;
If F is - then read names from standard input
-L, --max-line-length print the maximum display width
-w, --words print the word counts
--help display this help and exit
--version output version information and exit
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/wc>
or available locally via: info '(coreutils) wc invocation'
awk
$ awk '{print $1,$4}' log.txt # 每行按空格或TAB分割,输出文本中的1、4项
$ awk -F, '{print $1,$2}' log.txt # 使用 "," 分割,并打印的分割后第一个和第二个
$ awk -F '[ ,]' '{print $1,$2,$5}' log.txt # 先使用空格分割,再使用“,” 分割
$ awk '$1>2' log.txt # 过滤第一列大于2的行
$ awk '$1>2 && $2=="Are" {print $1,$2,$3}' log.txt # 过滤第一列大于2,第二列等于are的行
$ awk -v a=1 '{print $1,$1+a}' log.txt # 第一列都加上变量a
$ cat url_count.txt | awk '{print $2}' | awk '{sum+=$1} END {print sum}' #对linux文件某一列计算累和
sort
Usage: sort [OPTION]... [FILE]...
or: sort [OPTION]... --files0-from=F
Write sorted concatenation of all FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
Ordering options:
-b, --ignore-leading-blanks ignore leading blanks
-d, --dictionary-order consider only blanks and alphanumeric characters
-f, --ignore-case fold lower case to upper case characters
-g, --general-numeric-sort compare according to general numerical value
-i, --ignore-nonprinting consider only printable characters
-M, --month-sort compare (unknown) < 'JAN' < ... < 'DEC'
-h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G)
-n, --numeric-sort compare according to string numerical value
-R, --random-sort shuffle, but group identical keys. See shuf(1)
--random-source=FILE get random bytes from FILE
-r, --reverse reverse the result of comparisons
--sort=WORD sort according to WORD:
general-numeric -g, human-numeric -h, month -M,
numeric -n, random -R, version -V
-V, --version-sort natural sort of (version) numbers within text
Other options:
--batch-size=NMERGE merge at most NMERGE inputs at once;
for more use temp files
-c, --check, --check=diagnose-first check for sorted input; do not sort
-C, --check=quiet, --check=silent like -c, but do not report first bad line
--compress-program=PROG compress temporaries with PROG;
decompress them with PROG -d
--debug annotate the part of the line used to sort,
and warn about questionable usage to stderr
--files0-from=F read input from the files specified by
NUL-terminated names in file F;
If F is - then read names from standard input
-k, --key=KEYDEF sort via a key; KEYDEF gives location and type
-m, --merge merge already sorted files; do not sort
-o, --output=FILE write result to FILE instead of standard output
-s, --stable stabilize sort by disabling last-resort comparison
-S, --buffer-size=SIZE use SIZE for main memory buffer
-t, --field-separator=SEP use SEP instead of non-blank to blank transition
-T, --temporary-directory=DIR use DIR for temporaries, not $TMPDIR or /tmp;
multiple options specify multiple directories
--parallel=N change the number of sorts run concurrently to N
-u, --unique with -c, check for strict ordering;
without -c, output only the first of an equal run
-z, --zero-terminated line delimiter is NUL, not newline
--help display this help and exit
--version output version information and exit
uniq
sage: uniq [OPTION]... [INPUT [OUTPUT]]
Filter adjacent matching lines from INPUT (or standard input),
writing to OUTPUT (or standard output).
With no options, matching lines are merged to the first occurrence.
Mandatory arguments to long options are mandatory for short options too.
-c, --count prefix lines by the number of occurrences
-d, --repeated only print duplicate lines, one for each group
-D print all duplicate lines
--all-repeated[=METHOD] like -D, but allow separating groups
with an empty line;
METHOD={none(default),prepend,separate}
-f, --skip-fields=N avoid comparing the first N fields
--group[=METHOD] show all items, separating groups with an empty line;
METHOD={separate(default),prepend,append,both}
-i, --ignore-case ignore differences in case when comparing
-s, --skip-chars=N avoid comparing the first N characters
-u, --unique only print unique lines
-z, --zero-terminated line delimiter is NUL, not newline
-w, --check-chars=N compare no more than N characters in lines
--help display this help and exit
--version output version information and exit
A field is a run of blanks (usually spaces and/or TABs), then non-blank
characters. Fields are skipped before chars.
其他指令
which nginx # 查看nginx的执行路径 , 输出结果为:/usr/sbin/nginx
/usr/sbin/nginx -t 查看nginx的配置文件路径
export PATH=/bin:/usr/bin:$PATH # 重新设置环境变量,当出现command not found时候
tail -f sys-info.log # 实时监控sys-info.log文件
cat /usr/local/cuda/version.txt # 查看CUDA版本
设置环境变量
sudo vim /etc/profile
export PATH="$PATH:/home/ccit19/ljh/julia-1.6.2/bin"
source /etc/profile
Hadoop 操作指令
1.1 文件处理
描述 | 指令 |
---|---|
查看文件 | hdfs dfs -ls /home/ljh |
新建文件夹 | hdfs dfs -mkdir /input |
删除文件 | hdfs dfs –rm /user/t/ok.txt |
删除文件夹 | hdfs dfs –rm -r /user/t/ |
重命名 | hdfs dfs –mv /user/test.txt /user/ok.txt |
本地文件上传到hdfs | hdfs dfs -put 本地地址 hadoop目录 |
本地文件夹上传到hdfs | hdfs dfs -put 本地文件夹 hadoop目录 |
hdfs上文件下载到本地 | hadoop dfs –get hadoop文件地址 本地目录 |
查看文件大小 | hdfs dfs -du -s -h 文件|目录 |