0
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?

【Linux】alias(エイリアス)設定

Last updated at Posted at 2024-12-22

■環境

 ・CentOS9

■現状

ls -lコマンドを入力すると、%Yが表示されない。
なので、ls -lのエイリアスとして、「ls -ls --time-style=+"%Y/%m/%d %H:%M:%S"」を設定していこうと思う。

■目的

ls -lコマンドを入力したときに、タイプスタンプが「%Y/%m/%d %H:%M:%S」で表示されること。

ll
合計 0
-rw-r--r-- 1 root root 0  7月  1 23:00 fileA.log
-rw-r--r-- 1 root root 0  8月  1 23:00 fileB.log
-rw-r--r-- 1 root root 0  9月  1 23:00 fileC.log
-rw-r--r-- 1 root root 0 10月  1 23:00 fileD.log

このままだと「%Y」が表示されない。

ls -ls --time-style=+"%Y/%m/%d %H:%M:%S"
合計 0
0 -rw-r--r-- 1 root root 0 2024/07/01 23:00:00 fileA.log
0 -rw-r--r-- 1 root root 0 2024/08/01 23:00:00 fileB.log
0 -rw-r--r-- 1 root root 0 2024/09/01 23:00:00 fileC.log
0 -rw-r--r-- 1 root root 0 2024/10/01 23:00:00 fileD.log

ls -lコマンドを入力して、↑の表記になればOK。

■手順

1.以下のコマンドを入力して、エイリアスを設定する。

alias ll='ls -ls --time-style=+"%Y/%m/%d %H:%M:%S"'

⇒エラーとならないこと

ll
合計 0
0 -rw-r--r-- 1 root root 0 2024/07/01 23:00:00 fileA.log
0 -rw-r--r-- 1 root root 0 2024/08/01 23:00:00 fileB.log
0 -rw-r--r-- 1 root root 0 2024/09/01 23:00:00 fileC.log
0 -rw-r--r-- 1 root root 0 2024/10/01 23:00:00 fileD.log

⇒「%Y」が表示されること

このままだとOSを再起動すると、↑の設定が消えてしまうので、
.bashrcというファイルを編集していきます。
(.bashrcファイルが何かは参考URLを参照)

2..bashrcにエイリアスを定義する
以下のコマンドを入力し、.bashrcファイルを開く。

vi ~/.bashrc

⇒エラーとならないこと
#iを入力し、コマンドモードからインサートモードに移行して、以下を記述し、

alias ll='ls -ls --time-style=+"%Y/%m/%d %H:%M:%S"'

:wqで保存する。
⇒エラーとならないこと

3.OSを再起動し、設定が読み込まれたことを確認する。
以下のコマンドを入力し、OSを再起動する。

reboot

4.設定の反映を確認する。
ファイルがあるディレクトリまで行って、以下のコマンドを実行する。

ll
合計 0
0 -rw-r--r-- 1 root root 0 2024/07/01 23:00:00 fileA.log
0 -rw-r--r-- 1 root root 0 2024/08/01 23:00:00 fileB.log
0 -rw-r--r-- 1 root root 0 2024/09/01 23:00:00 fileC.log
0 -rw-r--r-- 1 root root 0 2024/10/01 23:00:00 fileD.log

⇒エラーとならないこと
無事設定が反映されていました。

cat ~/.bashrc | grep alias
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ll='ls -ls --time-style=+"%Y/%m/%d %H:%M:%S"'

ちなみに、設定自体は消えていませんでした。

再起動後、設定が反映されていない場合、項番5に進む。

5..bash_profileに.bashrcを読み込ませる
以下のコマンドを入力し、.bash_profileファイルを開く。

vi ~/.bash_profile

⇒エラーとならないこと
#iを入力し、コマンドモードからインサートモードに移行して、以下を記述し、

source ~/.bashrc

:wqで保存する。
⇒エラーとならないこと

■参考URL

0
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
0
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?