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?

More than 3 years have passed since last update.

Linuxのcp, mv, rmコマンドについてまとめる

Posted at

今回のお題

Linuxのcp, mv, rmコマンドについてまとめます。

lsコマンドについては以下の記事をどうぞ。

cpコマンド

ファイル、フォルダのコピーをするためのコマンドです。

ディレクトリ状況
|-- dir1
|    |-- dir3
|    |-- test1.txt
|
|-- dir2

以下の作業については基本的にdir1で行います。

通常コピー
# cp コピーしたいファイルのパス コピー先のパス
% cp test1.txt ../dir2
# dir2の中にtest1.txtがコピーされる
% ls ../dir2
>> test.txt
コピーと同時に名前を変更
# cp コピーしたいファイルのパス コピー先のパス/コピー後のファイル名
% cp test.txt ../dir2/test2.txt
# dir2の中にtest.txtがコピーされ、test2.textとして保存される。
% ls ./dir2
>> test2.txt
同一フォルダ内へのコピー
# 同じディレクトリ内にコピーする場合にはファイル名の変更が必須
% cp test.txt test2.txt
% ls
>> test.txt test2.txt dir3
ディレクトリごとコピー
% cp -r ../dir1 ../dir2
# dir2の中にdir1のコピーが作られる
% ls ../dir2
>> dir1
ディレクトリの中身だけをコピー
% cd dir1
% cp -r . ../dir2
# dir1の中身だけがdir2にコピーされる
% ls ../dir2
>> test.txt

mvコマンド

ファイル・ディレクトリの移動や名前の変更に使います。

cpコマンドとは異なり、-rオプションは不要です。

ファイル名の変更
# mv 現在のファイル名 変更後のファイル名
% mv test.txt test1.txt
% ls
test1.txt dir3
フォルダ名の変更
# mv 現在のフォルダ名 変更後のフォルダ名
% mv dir3 dir4
# 他のカレントディレクトリ外のフォルダを操作する場合には、変更前変更後ともにパスを指定する
% mv ../dir2 ../dir2
ファイル/ディレクトリの移動
# mv 移動させたいファイル・ディレクトリ 移動先のフォルダのパス
% mv dir3 ../dir2
% ls ../dir2
>> dir3

注意

フォルダの移動と名称変更はともに

mv フォルダ名 フォルダ名

という書式になるため、フォルダ移動の際に移動先として存在しないディレクトリ名を指定してしまうとフォルダ名の変更として扱われてしまう。

rmコマンド

ファイル・ディレクトリの削除に用いるコマンドです。

# ファイルの削除は rm ファイル名
% rm test.txt

# フォルダの削除は rm -r フォルダ名
% rm -r ../dir2

# -fオプションで削除しても良いかどうかの確認を省略可能
$ rm -f test.txt
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?