记不住的:Linux常用命令

虽然每天用着Windows,但是这些Linux常用命令你不能不知道。

bash相关命令

  • ln -s file1 file1soft#创建软链接(符号链接,目录也可以)
  • find /etc/ -name 'iptables' -ls#查找文件
  • grep "git" ./*#查找当前目录下所有文件中包含”git”关键字的行

tar

1
2
3
4
5
6
7
8
9
10
#压缩
tar -zcf ~/hadoop.master.tar.gz ./hadoop

#解压
tar -jxvf file.tar.bz2
tar -zxvf file.tar.gz
tar -zxf ~/redis.master.tar.gz -C /usr/local

#排除.git
tar -zcvf xxxxx.tar.gz xxxxx/ --exclude build --exclude .gradle --exclude .git --exclude .gitlab --exclude .idea

7z

1
2
#排除.git
D:\apps\7-Zip\7zG.exe a -tzip allinone.zip allinone -xr!build -xr!.gradle -xr!.git -xr!.gitlab -xr!.idea

iconv文件编码转换

1
2
3
4
5
6
单个文件转换
$ iconv -f GBK -t UTF-8 file1 -o file2
批量转换
$ find default -type d -exec mkdir -p utf/{} \;
$ find default -type f -exec iconv -f GBK -t UTF-8 {} -o utf/{} \;
这两行命令将default目录下的文件由GBK编码转换为UTF-8编码,目录结构不变,转码后的文件保存在utf/default目录下。

去掉文件中的\r

1
sed -i 's/\r$//' lbpkg.sh

把文件中的tab换成4 space

1
2
3
find . -type f -exec sed -i.orig 's/\t/    /g' {} +

The original file is saved as [filename].orig.

自动mount

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#查看uuid
$ lsblk -f # sudo blkid
sdb
├─sdb3 ext4 f10e535e-2fa7-4a66-9737-714ae53e382b

$ sudo vim /etc/fstab
#在文件末尾追加
#/dev/sdb3 这个可能会变
UUID=f10e535e-2fa7-4a66-9737-714ae53e382b /mnt/apps ext4 errors=remount-ro 0 1

#给当前用户分配一个目录
$ sudo mkdir /mnt/apps/andys
$ sudo chown andy andys
$ ln -s /mnt/apps/andys ~/apps

jdk

1
2
3
4
5
6
7
# change java runtime version
update-alternatives --config java

# change javac compile version
update-alternatives --config javac

update-java-alternative -l

权限、用户

1
sudo chown -R root:root /usr/local/redis

netstat

1
2
3
4
5
6
netstat -tap | grep redis 
netstat -an | grep 6379
ps -ef | grep redis
sudo service --status-all
sudo service redis-server status
sudo service redis-server start

几个重要的目录

  • /opt 这是大多数商业软件包的默认安装路径。
  • /usr/local#源码编译安装的软件一般放在这里,如nginx。
  • /usr/bin /usr/local中安装的软件的软链适合放在这里。
  • ~/bin自己创建放在PATH里,放一些只有当前用户使用的第三方软件软链
    1
    2
    3
    mkdir ${HOME}/bin

    echo 'export PATH=${HOME}/bin:$PATH' >> ~/.bashrc

源码编译安装的基本过程

参考:CentOS 7下的软件安装方法及策略
参考:Linux下源代码的编译安装
参考:怎么卸载用 make install 编译安装的软件

  1. MD5校验md5sum nginx.tar.gz
  2. 解包
    # tar zxvf nginx.tar.gz -C /usr/local/src/
    # cd /usr/local/src/
  3. 安装
    Linux下的习惯,源码编译安装的第三方软件一般都安装到/usr/local/opt目录下
  • ./configure./configure --prefix=/opt/nginx#配置安装参数

  • make >& LOG_make &#编译,将源代码文件变为二进制的可执行程序

  • make install >& LOG_install &#安装