Yifei Kong

Nov 01, 2018

Linux 下分区并挂载磁盘

分区

parted -s -a optimal /dev/sda mklabel gpt -- mkpart primary ext4 1 -1s

创建文件系统

mkfs.ext4 /dev/sda1

查看分区结果

parted -l

复制数据

首先挂载到临时分区

mount /dev/sdb1 /mnt

把之前的数据考进去

# rsync -av /home/* /mnt/
OR
# cp -aR /home/* /mnt/

校验数据

diff -r /home /mnt

删除老数据

rm -rf /home/*
umount /mnt

挂载 …

Apr 01, 2018

fd - 更好的 find 命令(fd - A nicer find command)

fd(https://github.com/sharkdp/fd) 是 find 命令的一个更现代的替换。

fd(https://github.com/sharkdp/fd)is a modern and nicer replacement to the traditional find command.

对比一下 Some comparisons

查找名字含有某个字符的文件 Find a filename with certain string

OLD

-> % find . -name "*hello*"
./courses/hello_world.go
./courses/chapter_01/hello_world.go
./courses/chapter_01/hello_world …

Jan 28, 2018

命令行的一些小技巧

最近工作中经常用到的一些组合命令,本来想提交到 commandlinefu.com 上,但是忘记了密码,怎么也登录不上去了,记到这里吧

  • 脚本所在的目录
dirname $0
  • 文件夹下面按照占用空间大小排序
du -sh `ls` | sort -rh
  • 返回上一个目录
cd -
  • 显示所有的日志的最后几行
tail *
  • set
set -x # 显示每个执行的命令
set -e # 当有程序返回非0值时直接退出,相当于抛出异常
  • here doc
cat << EOF > /tmp/yourfilehere
These contents will be written to the file.
        This line is indented.
EOF
  • 删除包含某个关键字的所有行
fd …

Jul 16, 2017

shell 编程教程

变量和值

variables are referenced by $var or ${var}. Global variables are visible to all sub bash sessions, and are often called env variables, local variables are only visible to local session, not subsessions.

Global variables can be viewed as env, and can be created by export.

TEST=testing; export $TEST …

Jun 30, 2017

May 30, 2017

Linux 命令行文件管理

get current file path

1
2
3
4
5
6
#!/bin/bash 
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$0")
# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=$(dirname "$SCRIPT")
echo $SCRIPTPATH

这些程序基本都有一个模式:如果不给定文件作为参数,那么就从stdin读取,从 stdout 输出,非常适合 …

May 30, 2017

命令行压缩图片的工具

加载速度对于网页的体验还是很重要的, 而每个页面比较耗费带宽的资源就是图片了, 所以在页面发布后对图像做适当的压缩是很有必要的.

常用的图片格式基本有三种: jpg, png 和 gif, 分别有不同的压缩工具

JPG 压缩建议使用 jpegoptim, 实测压缩比在40%左右, 最常用的语法是

jpegoptim -p -m<dd> <imagefile>

其中 -p 是保留 mtime 的意思, 也可以用 --perserve, -m 后面跟两个数字表示, 压缩的比例, 一般用90即可, 肉眼无法分辨, 且能够压缩掉40%左右

png 压缩有两个选项, 常规的 optipng 和另一个新一点 pngquant, 测试了一下, 发现 optipng 速度慢而且压缩效率低, pngquant 则表现相当优秀, 但是需要注意 pngquant 是有损压缩.

pngquant …

May 30, 2017

converting mp4 to gif

Converting MP4 to gif

To convert the entire video to GIF, use the following command:

ffmpeg -i small.mp4 small.gif

To convert just a portion of a video clip to GIF, use the following command:

ffmpeg -t 3 -ss 00:00:02 -i small.webm small-clip.gif

The snippet …

May 29, 2017

Linux 命令行账户管理

执行 sudo vi /etc/suduers, 然后输入:

username ALL=(ALL) NOPASSWD: ALL

useradd -m USERNAME  # add a user
userdel USERNAME  # delete a user
passwd -e  # password expire next time user login

groups username  # view username groups
usermod -G groupname username  # add user to a group
usermod -g groupname username # set user …