Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

UNIX(Linux)
Beginner

  1. What is UNIX (Linux)? - Japanese Only
  2. What are Distributions? - Japanese Only
  3. About CentOS, macOS, and CUI - Japanese Only
  4. Installing CentOS with Virtualization Software and macOS CUI Environment - Japanese Only
  5. Login, root User, and Shutdown - Japanese Only
  6. Command Structure and Important Notes - Japanese Only
  7. File System and Navigating Directories (cd and pwd) - Japanese Only
  8. Key Directories and Listing Contents (ls) - Japanese Only
  9. Advanced Directory Navigation (cd) and Paths - Japanese Only
  10. Advanced ls Command (Part 1) and Dotfiles - Japanese Only
  11. Advanced ls Command (Part 2) - Japanese Only
  12. Tab Completion for Input - Japanese Only
  13. Creating Directories (mkdir) - Japanese Only
  14. Quick File Creation (touch) and About Files - Japanese Only
  15. Standard I/O, Redirection (> and >>), echo, and cat - Japanese Only
  1. Deleting Files and Directories (rm) - Japanese Only
  2. Copying Files and Directories (cp) - Japanese Only
  3. Moving and Renaming Files and Directories (mv) - Japanese Only
  4. What is the vi Editor? - Japanese Only
  5. Basic vi Usage (Part 1) - Japanese Only
  6. Basic vi Usage (Part 2) - Japanese Only
  7. Remote Operations via Terminal (SSH and Tera Term) - Japanese Only
  8. Installing Software (yum and Package Managers) - Japanese Only
  9. User Overview, Verification, Creation, and Switching (adduser and su) - Japanese Only
  10. User Passwords, Deletion, Modification, and sudo (passwd, userdel, usermod) - Japanese Only
  11. About Groups and the groups Command - Japanese Only
  12. Adding, Deleting, and Modifying Groups (groupadd, groupdel, groupmod) - Japanese Only
  13. About Permissions - Japanese Only
  14. Setting Permissions (chmod and chown) - Japanese Only
  1. Home
  2. UNIX(Linux)Beginner - Moving and Renaming Files and Directories (mv)

Moving and Renaming Files and Directories (mv) - Japanese Only

みなさまこんばんちわわです。

今回はファイルとかディレクトリの移動とか名前変更とかが出来る『mv』コマンドくんについてです。

これまで『rm』とか『cp』とかやってきましたが使い方はほとんど同じ感じで『mv 元のファイル名とか 移動先のファイル名とか』って感じになります。

ただ指定した移動先によって結果がかなり変わることになるので少し注意が必要です、いつも通りそれぞれみていくことにしましょう。

まず『touch』コマンドくんで適当にファイルを作ります。

[root@localhost ~]# touch test.txt
[root@localhost ~]# ls
test.txt

そしたら以下のようなコマンドを叩いてみます。

[root@localhost ~]# mv test.txt test1.txt

『mv 元のファイル名とか 移動先のファイル名とか』って記法になるのでこれは同じディレクトリの『test.txt』から同じディレクトリに移動させているって事ですね。これを『ls』コマンドくんで結果を見てみると

[root@localhost ~]# mv test.txt test1.txt
[root@localhost ~]# ls
test1.txt

『test.txt』ってファイルが消えて『test1.txt』が出来上がっていますね。これどういう事かというと「ファイル名が変更された」って事になります。よりわかりやすいコマンドにすると以下になりますね。

[root@localhost ~]# mv ./test.txt ./test1.txt

『./』は現在のカレントディレクトリを指すって記号なので「test.txtを同じディレクトリにtest1.txtって名前で移動させた」ってことになりつまり「ファイル名を変更した」ってことになります。

ここがちょっとややこしいのですがUNIX系OSでは「名前変更」もファイルとかを移動させることも全て『mv』コマンドくんで行います。多分WindowsOSとかMacOSとかに慣れてる方だと変な感じがするかもですがこれは慣れちゃって下さい。

ちなみに以下のように同じディレクトリに同じ名前で移動させようとすると怒られちゃいます。

[root@localhost ~]# mv test.txt test.txt
mv: `test.txt' and `test.txt' are the same file

「同じファイルっすよー!」って言われちゃってますね。

続いてカレントディレクトリじゃない違うディレクトリに移動する場合です。まず適当に『test』ってディレクトリを作ります。

[root@localhost ~]# mkdir test
[root@localhost ~]# ls
test  test1.txt

そして『test1.txt』を『test』ディレクトリに同じ名前で移動させたい場合は以下でOKです。

[root@localhost ~]# mv test1.txt test/
[root@localhost ~]# ls test/
test1.txt

移動と同時に違うファイル名にしたい、といった場合は以下となります。

[root@localhost ~]# mv test1.txt test/test.txt
[root@localhost ~]# ls test/
test.txt

上記のコマンドは『test1.txt』を『test』ディレクトリの中に移動させると同時にファイル名を『test.txt』に変更してる感じですね。こんな感じでファイル名の変更も同時に出来るので覚えておきましょう。

そして「特定のファイルとかを現在のカレントディレクトリに持ってきたい」って場合は現在のカレントディレクトリを表す『.』を使うことで以下のように簡単に記述することができます。第2引数の『.』に注目ですね。

[root@localhost ~]# ls test/
test.txt
[root@localhost ~]# mv test/test.txt .
[root@localhost ~]# ls
test  test.txt

このように『.』をちょんっと書くだけで現在のカレントディレクトリに移動が完了できちゃったりしますのでとっても便利です。ぜひ使ってみて下さい。

そして「ディレクトリを移動させたいぜー」といった場合も『mv』ならとっても簡単です。オプションも何も付けずそのまま書けばOKでございます。

[root@localhost ~]# ls
test.txt  test
[root@localhost ~]# mv test test1
[root@localhost ~]# ls
test.txt  test1

『rm』とか『cp』だと『-r』オプションを付けてましたが『mv』は要らない感じですね。『rm』と『cp』とごちゃごちゃになりがちな部分ですが間違えないように気をつけて下さい。

というわけで以上になります。次の記事では『vi』についてやっていきましょう。ではではこの辺で失礼致します。

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .