四个提高工作效率的小技巧:
一)有没有那么一个命令,创建目录并切换至新目录下?
在工作中是不是经常使用mkdir创建目录,然后cd到创建的目录下,如果经常这样执行的话是不是特烦,抱怨为什么没有一个现成的命令供使用
纠结中。。。。。
如果这个时候有个方式可以满足你的需求是不是感觉很兴奋,然我们见证实现方式吧:
在当前登录的账号的~/.bash_profile文件中添加如下代码:
function mkdircd () {
mkdir -p "$@" && eval cd "\"\$$#\"";
}
然后执行 source ~/.bash_profile 使修改文件生效
这个时候你的服务器下就会有个新的命令,mkdircd
Example:
#mkdircd /tmp/a/b/c
#pwd
/tmp/a/b/c
是不是感觉很炫~~~~~~~
二)如果快捷的在一个目录下创建深层目录?
目录结构如:
# tree a
a
├── b
│ ├── e
│ ├── f
│ └── g
├── c
│ ├── e
│ ├── f
│ └── g
└── d
├── e
├── f
└── g
这个时候你想怎么做,传统的方式是mkdir a,然后cd a ,然后 mkdir b c d ,然后继续cd,mkdir。。。。
如果子目录有很多的时候,这种操作方式是不是感觉到很绝望
其实mkdir有个参数-p:
-p, --parents
no error if existing, make parent directories as needed
可以通过 mkdir -p a/{b,c,d}/{e,f,g}创建上面的目录结构,是不是感觉很方便
三)如果在一个目录下快速创建指定文件名字前缀的多个文件?
如:file1,file2,file3,....
这个时候你的第一直觉是不是,cd到需要创建文件的目录,然后执行 touch file2 file2 file3 ....
我相信如果需要的创建的文件大约五个的话,感觉就会让你心跳加快。。。
touch创建有个快捷操作方式可以批量创建这种文件,而不用担心创建的文件多少:
# touch file{1..9}
# tree
.
├── file1
├── file2
├── file3
├── file4
├── file5
├── file6
├── file7
├── file8
└── file9
四)如何复制并创建目标目录,当它不存在的时候?
cp a/b/c d
如果直接这样执行是不报报的,但是结果不是自己想要的
这个时候看看d目录下的结果是什么样的:
#tree d
d
└── f
0 directories, 1 file
但是我想的目录结构是:
# tree d
d
└── a
└── b
└── f
2 directories, 1 file
其实cp命令有一个参数
‘--parents’
Form the name of each destination file by appending to the target directory a slash and the specified name of the source file. The last argument given to cp must be the name of an existing directory. For example, the command:
cp --parents a/b/c existing_dir
大概意思是:通过在目标目录中附加斜杠和源文件的指定名称来形成每个目标文件的名称。 最后一个参数必须是存在的目录。
cp --parents a/b/f d/
参考:
http://www.gnu.org
/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation](http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html