Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

SHELL命令行快捷键

  1. Ctrl+a:光标回到命令行首
  2. Ctrl+e:光标回到命令行尾
  3. Ctrl+b:光标向行首移动一个字符
  4. Ctrl+f:光标向行尾移动一个字符
  5. alt +b: 光标向行首移动一个单词
  6. alt +f: 光标向行首移动一个单词
  7. Ctrl+w: 删除光标处到行首的字符
  8. alt+d: 删除光标后一个单词
  9. Ctrl+k:删除光标处到行尾的字符
  10. Ctrl+u:删除整个命令行文本字符
  11. Ctrl+y: 粘贴Ctrl+u,Ctrl+k,Ctrl+w删除的文本
  12. ctrl+shift+c: 复制
  13. ctrl+shift+v: 黏贴
  14. Ctrl+z:使正在运行在终端的任务,运行于后台。 (可用bg,fg恢复)
  15. Ctrl+c:中断终端中正在执行的任务
  16. Ctrl+d: 在空命令行的情况下可以退出终端

shell脚本添加到默认路径

自己写的shell脚本;不放到PATH路径下,命令只能在脚本所在路径下运行,为了能像ls等系统命令可以在任意路径下使用,需要进行如下设置:

  • 现在用户根目录下创建 bin目录,可以是任意目录,习惯上命名为 bin

  • 添加到 .bashrc 文件中

  • 更新 .bashrc 文件

    1
    2
    3
    4
    5
    6
    7
    $ cd ~
    $ mkdir bin

    $ vim .bashrc
    $ export PATH=$PATH:~/bin

    $ source .bashrc

    参考文献:REF

编写SHELL脚本

1
2
#!/bin/bash
echo 'this is shell?'

统计文件个数

1
2
3
ls -l | grep "Train" | wc -l            

ps -ef | grep matlab |wc -l

批量移动清单中的文件

  • 有需要移动的文件名清单 filename.txt
  • 文件在files1文件夹下有filename.txt中的文件
  • 从files1文件夹移动filename.txt中的文件到files2
1
2
3
4
5
6
7
8
9
10
$ cat mv_from_filelistname.sh
#!/bin/bash
mkdir tmp
for I in `cat $1`
do
mv $2/$I tmp/
done

# useage
./mv_from_filelistname.sh bdgt_test.txt clipbdgt

参考文献:everfigh

批量文件重命名

文件夹下有文件 a.prj, a.sta a.tfw a.tif a.txt,批量重命名为 b.prj, b.sta b.tfw b.tif b.txt

1
2
3
4
5
6
7
8
9
10
11
rename [options] <expression> <replacement> <file>...
Options:
-v, --verbose explain what is being done
-s, --symlink act on the target of symlinks
-n, --no-act do not make any changes
-a, --all replace all occurrences
-l, --last replace only the last occurrence
-o, --no-overwrite dont overwrite existing files
-i, --interactive prompt before overwrite

rename a b a.*

批量添加前缀由 a.mp3 重命名为 s1_a.mp3

1
for i in *; do mv -- "$i" "s1_$i"; done

批量重命名

微信图片时间标签名批量修改为年-月-日

1
2
3
4
5
6
7
8
9
#!/bin/bash
for i in $(ls)
do
name=$(echo $i |cut -b 1-10)
echo $name
namen=$(date -d "1970-01-01 $name seconds" +"%Y-%m-%d_%H%M%S")
echo $namen
rename.ul $name $namen $i
done

神奇的1234567890秒转换为年月日date -d "1970-01-01 $name seconds" +"%Y-%m-%d_%H%M%S"

CentOS下shell显示-bash-4.1

CentOS下新增加一个用户,登录进去会发现shell脚本信息没有显示用户名和主机名,反而显示的是-bash-4.1$

而不是我们经常看到的username@hostname$的组合,看起来特别别扭不舒服。

问题的原因是:没有配置.bash_profile的问题,或者说没有这个文件的问题,可以通过配置或者新建这个文件来解决问题。

解决方案:

1
2
3
4
5
1.在新建用户的~目录下新建或者更改.bash_profile; 

2.在.bash_profile中添加以下内容:export PS1='[\u@\h \W]\$'

3.在新建用户下运行一下命令:source ~/.bash_profile

这样就可以正常显示用户名和主机名了, 参考文献

评论