2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

linuxで新しいコマンドを作ってみる

Posted at
  • linuxで新しいコマンドを作ってみようの巻です。

新しいコマンドがどういうことかというと、
newfileコマンドを打てば、「newfile.txt」ファイルを作成させることが出来るのです。

  • どういう仕組み?

aliasコマンドを使用します。

  1. newfileコマンドを打てば
  2. 裏でtouch newfile.txtを裏で実行させ
  3. 「newfile.txt」ファイルを作成させているのです。
  • 登録のコマンド
alias 別名のコマンド='コマンド'
  • 削除のコマンド
unalias 別名のコマンド

コマンドを登録する

※注意

aliasは、ターミナルの画面を閉じたり、ログアウトしたりするとリセットされるので注意が必要です。

一時的に登録

  • 登録
alias newfile='touch newfile.txt'
  • 登録の確認
$ alias | grep newfile
==========
alias newfile='touch newfile.txt'
==========

登録できていました!

  • 実行
$ ls
(何も返ってこなかった)

$ newfile

$ ls
newfile.txt

newfile.txt出来ていました!

永続的に登録

  • 登録
vi  ~/.bashrc
==========
追加
alias newfile='touch newfile.txt'
==========
  • ログインし直す
exit
  • 登録の確認
$ alias | grep newfile
==========
alias newfile='touch newfile.txt'
==========

登録できていました!

  • 実行
$ ls
(何も返ってこなかった)

$ newfile

$ ls
newfile.txt

newfile.txt出来ていました!

コマンドを削除する

一時的に登録した場合の削除

  • 登録の確認
$ alias | grep newfile
==========
alias newfile='touch newfile.txt'
==========
  • 削除
unalias newfile
  • 削除の確認
$ alias | grep newfile
(何も返ってこない)

永続的に登録した場合の一時削除

  • 登録の確認
$ alias | grep newfile
==========
alias newfile='touch newfile.txt'
==========
  • 一時削除
unalias newfile
  • 削除の確認
$ alias | grep newfile
(何も返ってこない)

※ただし、ログインし直しなどしたらコマンドは使えるようになっています。

永続的に登録した場合の永続的削除

  • 登録の確認
$ alias | grep newfile
==========
alias newfile='touch newfile.txt'
==========
  • 削除
vi  ~/.bashrc
==========
削除
alias newfile='touch newfile.txt'
==========
  • ログインし直す
exit
  • 削除の確認
$ alias | grep newfile
(何も返ってこない)

※こちらは、改めてログインし直ししてもコマンドは削除されたままになります。

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?