LoginSignup
4
8

More than 5 years have passed since last update.

Linuxを扱えるようになろう(基本操作編)

Last updated at Posted at 2018-02-13

こんにちわ
グローバスセンス株式会社のskanehiraです。

前回では、Linuxの環境を作りましたので、
今回はLinuxをのディレクトリとファイルの基本操作について書いていきます。

コマンドでファイル・ディレクトリ作成したり、移動したり、削除したりとなれないと思いますが、
使っていけばそのうち慣れていきますので、気長に行きましょっ

アジェンダ

  • ディレクトリ操作系
  • ファイル操作系

ファイル・ディレクトリ操作

  • 現在自身がいるディレクトリ
# 現在いるディレクトリ
[skanehira@localhost ~]$ pwd
/home/skanehira
[skanehira@localhost ~]$
  • ディレクトリ内一覧
# ディレクトリ内を表示
[skanehira@localhost ~]$ ls
test  test.txt
[skanehira@localhost ~]$

# オプションの-lをつけるとファイル・ディレクトリの詳細情報が表示される
[skanehira@localhost ~]$ ls -l
合計 0
drwxrwxr-x. 2 skanehira skanehira 6  2月 13 23:12 test
-rw-rw-r--. 1 skanehira skanehira 0  2月 13 23:13 test.txt
[skanehira@localhost ~]$

# aをつけると隠しファイル・フォルダが表示される
[skanehira@localhost ~]$ ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  test  test.txt
[skanehira@localhost ~]$

# もちろんオプションを複数同時に付けることはできる
[skanehira@localhost ~]$ ls -la
合計 12
drwx------. 3 skanehira skanehira 111  2月 13 23:13 .
drwxr-xr-x. 3 root      root       23  2月 10 10:19 ..
-rw-------. 1 skanehira skanehira   0  2月 10 11:33 .bash_history
-rw-r--r--. 1 skanehira skanehira  18  8月  3  2017 .bash_logout
-rw-r--r--. 1 skanehira skanehira 193  8月  3  2017 .bash_profile
-rw-r--r--. 1 skanehira skanehira 231  8月  3  2017 .bashrc
drwxrwxr-x. 2 skanehira skanehira   6  2月 13 23:12 test
-rw-rw-r--. 1 skanehira skanehira   0  2月 13 23:13 test.txt

# tをつけると更新日付の昇順に表示される
[skanehira@localhost ~]$ ls -lt
合計 0
-rw-rw-r--. 1 skanehira skanehira 0  2月 13 23:13 test.txt
drwxrwxr-x. 2 skanehira skanehira 6  2月 13 23:12 test
[skanehira@localhost ~]$

  • 現在いるディレクトリを移動
# cd 移動したいディレクトリのパス
[skanehira@localhost ~]$ ls
test  test.txt
[skanehira@localhost ~]$ cd test
[skanehira@localhost test]$ pwd
/home/skanehira/test
[skanehira@localhost test]$

# 一つ前のディレクトリに戻る
[skanehira@localhost test]$ pwd
/home/skanehira/test
[skanehira@localhost test]$ cd ..
[skanehira@localhost ~]$ pwd
/home/skanehira
[skanehira@localhost ~]$

# 絶対パスによる移動
[skanehira@localhost ~]$ pwd
/home/skanehira
[skanehira@localhost ~]$ cd  /home/skanehira/test
[skanehira@localhost test]$ pwd
/home/skanehira/test
[skanehira@localhost test]$

  • ディレクトリの作成

# mkdir ディレクトリ名
[skanehira@localhost ~]$ mkdir qiita
[skanehira@localhost ~]$ ls
qiita  test  test.txt
[skanehira@localhost ~]$

# pをつけると一気にディレクトリの階層を作成できる
[skanehira@localhost ~]$ mkdir -p dir/child
[skanehira@localhost ~]$ ls dir
child
[skanehira@localhost ~]$

# -pを付けないと作ろうとすると…
[skanehira@localhost ~]$ mkdir dir1/child
mkdir: ディレクトリ `dir1/child' を作成できません: そのようなファイルやディレクトリはありません
[skanehira@localhost ~]$
  • ディレクトリ移動
# mv 移動元 移動先
[skanehira@localhost ~]$ ls
qiita  test  test.txt  work
[skanehira@localhost ~]$ mv test work/
[skanehira@localhost ~]$ ls work/
child  test
[skanehira@localhost ~]$

# 移動コマンドだが、ディレクトリ名変更もできる
[skanehira@localhost ~]$ ls
qiita  test.txt  work
[skanehira@localhost ~]$ mv work/ dir
[skanehira@localhost ~]$ ls
dir  qiita  test.txt
[skanehira@localhost ~]$

# iをつけると同じディレクトリがある場合は上書きするかどうかメッセージが表示される
[skanehira@localhost ~]$ ls dir
child  test
[skanehira@localhost ~]$ mkdir test
[skanehira@localhost ~]$ ls
dir  qiita  test  test.txt
[skanehira@localhost ~]$ mv -i test dir
mv: `dir/test' を上書きしますか? y
[skanehira@localhost ~]$ ls
dir  qiita  test.txt
[skanehira@localhost ~]$ ls dir/
child  test
[skanehira@localhost ~]$
  • ディレクトリのコピー
# cp コピー元 コピー先
# ディレクトリの場合はrをつける必要がある
[skanehira@localhost ~]$ ls
test  test.txt  work
[skanehira@localhost ~]$ cp work work2
cp: ディレクトリ `work' を省略しています
[skanehira@localhost ~]$ cp -r work work2
[skanehira@localhost ~]$ ls
test  test.txt  work  work2
[skanehira@localhost ~]$
  • ディレクトリ削除
# rm -r 対象ディレクトリ
# ディレクトリを削除するにはrmにオプションrをつける必要がある
[skanehira@localhost ~]$ ls
dir  qiita  test.txt
[skanehira@localhost ~]$ rm qiita
rm: `qiita' を削除できません: ディレクトリです
[skanehira@localhost ~]$
[skanehira@localhost ~]$ rm -r qiita
[skanehira@localhost ~]$ ls
dir  test.txt
[skanehira@localhost ~]$

  • ファイル作成
# touch ファイル名
# ファイルが存在する場合はタイムスタンプが更新される
[skanehira@localhost ~]$ ls
test  test.txt  work  work2
[skanehira@localhost ~]$ touch file.txt
[skanehira@localhost ~]$ ls
file.txt  test  test.txt  work  work2
# タイムスタンプ更新
[skanehira@localhost ~]$ ls -l
合計 0
-rw-rw-r--. 1 skanehira skanehira  0  2月 13 23:55 file.txt
(中略)
[skanehira@localhost ~]$ touch file.txt
[skanehira@localhost ~]$ ls -l
合計 0
-rw-rw-r--. 1 skanehira skanehira  0  2月 13 23:58 file.txt
(中略)
[skanehira@localhost ~]$
  • ファイル移動
# ディレクトリの移動と同じコマンド
# ファイル名更新もディレクトリ時と同じやり方
# ファイルの場合はrを付ける必要がない
[skanehira@localhost ~]$ ls
file2  work
[skanehira@localhost ~]$ mv file2 work/
[skanehira@localhost ~]$ ls
work
[skanehira@localhost ~]$ ls work/
file2
[skanehira@localhost ~]$
[skanehira@localhost ~]$ cd work/
[skanehira@localhost work]$ ls
file2
[skanehira@localhost work]$ mv file2 file
[skanehira@localhost work]$ ls
file
[skanehira@localhost work]$
  • ファイル削除
# rm 対象ファイル
[skanehira@localhost ~]$ ls
file.txt  test  test.txt  work  work2
[skanehira@localhost ~]$ rm test.txt
[skanehira@localhost ~]$ ls
file.txt  test  work  work2
[skanehira@localhost ~]$

# iをつけるとファイルを削除するかどうかのメッセージが表示される
[skanehira@localhost ~]$ ls
file.txt  test  test.txt  work  work2
[skanehira@localhost ~]$ rm test.txt
[skanehira@localhost ~]$ ls
file.txt  test  work  work2
[skanehira@localhost ~]$ rm -i file.txt
rm: 通常の空ファイル `file.txt' を削除しますか? n
[skanehira@localhost ~]$ ls
file.txt  test  work  work2
[skanehira@localhost ~]$ rm -i file.txt
rm: 通常の空ファイル `file.txt' を削除しますか? y
[skanehira@localhost ~]$ ls
test  work  work2
[skanehira@localhost ~]$

  • ファイルコピー
# cp コピー元 コピー先
[skanehira@localhost ~]$ touch file
[skanehira@localhost ~]$ ls
file  test  work  work2
[skanehira@localhost ~]$ cp file file2
[skanehira@localhost ~]$ ls
file  file2  test  work  work2
[skanehira@localhost ~]$

# iをつけるとコピー先のファイルがすでに存在している場合は上書きするかどうかのメッセージが表示される
[skanehira@localhost ~]$ cp file file2
[skanehira@localhost ~]$ ls
file  file2  test  work  work2
[skanehira@localhost ~]$ cp -i file file2
cp: `file2' を上書きしますか? n
[skanehira@localhost ~]$ cp -i file file2
cp: `file2' を上書きしますか? y
[skanehira@localhost ~]$
  • ファイルの中身を表示
# cat 対象ファイル
[skanehira@localhost ~]$ cat file
aaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccc
dddddddddddddddddddddddd
[skanehira@localhost ~]$

# ファイルの中身が大量の場合はlessを使用する
# less 対象ファイル
# ※コマンドラインに戻る時ははqを押下
aaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccc
dddddddddddddddddddddddd
file (END)

  • ファイルの中身を検索
# grep 検索文字列 対象ファイル
# ※検索したい文字に空白が含まれている場合は''で検索文字列を囲う
[skanehira@localhost ~]$ grep a file
aaaaaaaaaaaaaaaaaaaaaaaa
[skanehira@localhost ~]$

終わりに

基本中の基本となるファイル・ディレクトリ操作は以上になります。
最後にまとめるとこんな感じになります。

コマンド 意味
ls ディレクトリ内一覧
pwd 現在いるディレクトリのパス
cd 移動先パス 現在いるディレクトリを移動する
mkdir ディレクトリ名 ディレクトリを作成
rm ディレクトリ名/ファイル名 ディレクトリ/ファイルを削除
mv 移動元 移動先 ファイル名/ディレクトリ名を変更・移動
cp コピー元 コピー先 ファイル/ディレクトリのコピー
touch ファイル名 新規ファイル作成
cat ファイル名 ファイルの中身を表示(標準出力)
less ファイル名 ファイルの中身を表示

基本的な操作方法を抑えたところで、次回はテキストエディタであるviについて書いていきます。
Linuxでテキストを編集する時はviエディタを使う事が主流になりますので、
使い方を覚えておきましょうー

4
8
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
4
8