某文件夹下有很多文件, 将文件夹中的文件拆分到多个目录中

cd file_path
ls | head -20000 | xargs -i {} mv {} /new_path

说明: xargs -i 是将输出按照行为单位输出给后面的命令, 例如:

find ./logs -name "*log" | xargs -i echo {}

说明: xargs -p 逐条确认执行,每次输出,都会让你手动确认下是否执行,y 执行, n 不执行,例如:

find ./logs -name "*log" | xargs -p -i echo {}

用 xargs file 输出文件类型

find . -type f -print | xargs file

文件批量修改后缀

rename 's/\.jpg\.jpeg/g'

找出不符合命名规则的文件

find -type f -print |grep "jpg" | grep -vE "[A-Za-z0-9_\/ ]+\([0-9]+\).jpg" |xargs -i cp {} /data/doit

说明: 在 shell 下使用 grep 空格 直接使用空格就好, s 貌似不好使 , d 貌似也不好使, 直接使用 [0-9], 是不是真这样呢?

批量压缩图片

进入目录后进行转换, 用 find 命令配合 xargs 会报错,但是在目录下操作就没问题

ls | xargs -i convert -resize 1200 -quality 95% {} ../new path/{}

批量整理文件名

很多文件, 文件名类似 name_1.jpg, name_2.jpg, 需要整理出不重复的 name. 文件名比较有规律, 所以用 substr 方法截取.
ls -l | xargs -i expr substr {} 1 9 | sort -u > 1.txt

redis 根据条件清理 key

通过 pattern 匹配

redis-cli -a password --scan --pattern '*history*' | xargs redis-cli -a password del {}

通过 shell 匹配

#! /bin/bash

if [ ${#1} -eq 56 ]
then
     redis-cli -a [email protected] del $1
fi

redis-cli -a --scan --pattern 'history' | xargs -i /root/check.sh {}


#### 创建 swap 交换磁盘

dd if=/dev/zero of=swap.disk bs=1M count=3k && mkswap -f swap.disk && swapon swap.disk

有时候,你会莫名奇妙的遇到 mysql 不能登录的场景,也有可能是你忘记了你的 root 密码, 这时候怎么办呢?

首先,你需要停止你的 mysql 服务 然后用 safe 模式下 skip the grant tables 方式运行, 在安全模式下, 更新下你的密码.

过程如下:

service mysqld stop
mysqld_safe --skip-grant-tables
mysql -u root -p

此时无需密码即可登录,接着就可以修改密码了:

use mysql;
update user set Password=PASSWORD('new-password') where user='root';
flush privileges;
exit

此时, kill mysql_safe 进程, 重新启动mysql, 在尝试一下用新密码登录吧。

You can login to a remote Linux server without entering password in 3 simple steps using ssky-keygen and ssh-copy-id as explained in this article.

ssh-keygen creates the public and private keys. ssh-copy-id copies the local-host’s public key to the remote-host’s authorized_keys file. ssh-copy-id also assigns proper permission to the remote-host’s home, ~/.ssh, and ~/.ssh/authorized_keys.

This article also explains 3 minor annoyances of using ssh-copy-id and how to use ssh-copy-id along with ssh-agent.
Step 1: Create public and private keys using ssh-key-gen on local-host

[email protected]$ [Note: You are on local-host here]

[email protected]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is:
33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 [email protected]

Step 2: Copy the public key to remote-host using ssh-copy-id

[email protected]$ ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
[email protected]'s password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

Note: ssh-copy-id appends the keys to the remote-host’s .ssh/authorized_key.
Step 3: Login to remote-host without entering the password

[email protected]$ ssh remote-host
Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2
[Note: SSH did not ask for password.]

[email protected]$ [Note: You are on remote-host here]

The above 3 simple steps should get the job done in most cases.

We also discussed earlier in detail about performing SSH and SCP from openSSH to openSSH without entering password.

If you are using SSH2, we discussed earlier about performing SSH and SCP without password from SSH2 to SSH2 , from OpenSSH to SSH2 and from SSH2 to OpenSSH.
Using ssh-copy-id along with the ssh-add/ssh-agent

When no value is passed for the option -i and If ~/.ssh/identity.pub is not available, ssh-copy-id will display the following error message.

[email protected]$ ssh-copy-id -i remote-host
/usr/bin/ssh-copy-id: ERROR: No identities found

If you have loaded keys to the ssh-agent using the ssh-add, then ssh-copy-id will get the keys from the ssh-agent to copy to the remote-host. i.e, it copies the keys provided by ssh-add -L command to the remote-host, when you don’t pass option -i to the ssh-copy-id.

[email protected]$ ssh-agent $SHELL

[email protected]$ ssh-add -L
The agent has no identities.

[email protected]$ ssh-add
Identity added: /home/jsmith/.ssh/id_rsa (/home/jsmith/.ssh/id_rsa)

[email protected]$ ssh-add -L
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79DyBV
aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow== /home/jsmith/.ssh/id_rsa

[email protected]$ ssh-copy-id -i remote-host
[email protected]'s password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.
[Note: This has added the key displayed by ssh-add -L]

在linode不同账户之间拷贝转移VPS,大体思路是利用linode本身的工具Finnix,ssh命令结合dd,完成整个磁盘的拷贝工作。

需要特别说明的是:之前有些网友不太熟悉dd命令,看英文教程也看的很仓促,结果dd反了,将源数据全毁了,所以谨慎起见,拷贝磁盘之前对重要文件做好备份工作。

  1. 发送端:开启紧急维护模式,linode采用lish登录。
  2. 接收端:创建磁盘,开启紧急维护模式,设置ssh密码,开启ssh服务, 接收端,创建磁盘,需要大于发送端磁盘大小,采用lish 登录。
  3. 在发送端输入:
dd if=/dev/xvda | ssh -C 106.186.xx.xx” dd of=/dev/xvda”

根据需要拷贝的磁盘的大小,来决定dd磁盘的时间,我测试的这1G磁盘拷贝,大概130秒完成。

  1. 拷贝完成,去接收端 dash board 对vps进行设置(edit Configuration profile), 保存后开启VPS, 完成整个VPS在不同linode账户间迁移过程.

参考: https://www.linode.com/docs/migrate-to-linode/disk-images/copying-a-disk-image-to-a-different-account