ファイル・ディレクトリ操作で使うコマンド

ファイル・ディレクトリ操作で使うコマンド

ディレクトリを操作するための基本コマンド

pwd (present working directory)コマンドで現在の場所を確認

pwdコマンドの主なオプション

pwdコマンドは、非常によく使う便利なコマンドです。自分がどこにいるのかわからなくなったときは、pwdコマンドをササッと実行すべし!

cd(Change Directory)コマンドでディレクトリを移動

1cd [-]  [移動先ディレクトリ]

どこにいても「cd」と実行すれば、自分のホームに戻れる!

ls(List files)コマンドでファイルやディレクトリの一覧表示

引数に指定されたファイルの情報を表示するコマンド
ディレクトリを指定した場合はディレクトリ内のファイルやディレクトリの情報を表示します

1ls [オプション]  [ファイル名またはディレクトリ名]

lsコマンドの便利なオプション

【練習】

3つのコマンドを使ってみます

1[user01@localhost ~]$ pwd
2/home/user01
3[user01@localhost ~]$ cd /
4[user01@localhost /]$ pwd
5/
6[user01@localhost /]$ ls
7bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
8boot  etc  lib   media  opt  root  sbin  sys  usr
9[user01@localhost /]$ ls etc/sysconfig
10anaconda    crond             iptables-config  netconsole       run-parts
11authconfig  ebtables-config   irqbalance       network         (以下省略)
12[user01@localhost /]$ ls /etc/sysconfig
13anaconda    crond             iptables-config  netconsole       run-parts
14authconfig  ebtables-config   irqbalance       network         (以下省略)

【練習】

1[user01@localhost /]$ ls –l
2合計 20
3lrwxrwxrwx.   1 root root    7  2月 16 20:00 bin -> usr/bin
4dr-xr-xr-x.   5 root root 4096  2月 16 20:21 boot
5drwxr-xr-x   20 root root 3240  2月 19 08:06 dev
6drwxr-xr-x.  76 root root 8192  2月 19 10:29 etc
7drwxr-xr-x.   8 root root  114  2月 18 13:14 home
8lrwxrwxrwx.   1 root root    7  2月 16 20:00 lib -> usr/lib
9lrwxrwxrwx.   1 root root    9  2月 16 20:00 lib64 -> usr/lib6
10(以下省略)
11[user01@localhost /]$ cd
12[user01@localhost /]$ pwd
13/home/user01
14[user01@localhost ~]$ ls -a
15.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc

ファイルの詳細情報

ファイルを操作するための基本コマンド

cat(conCATenate)コマンドでファイル内容を表示する

/etc/hostsファイルを表示してみましょう

1[user01@localhost ~]$cat /etc/hosts
2127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
3::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

catコマンドの実行

複数ファイルも指定できます

1[user01@localhost ~]$cat /etc/hosts /etc/profile
2127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
3::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
4# /etc/profile
5 
6# System wide environment and startup programs, for login setup
7# Functions and aliases go in /etc/bashrc
8 
9# It's NOT a good idea to change this file unless you know what you
10# are doing. It's much better to create a custom.sh shell script in
11# /etc/profile.d/ to make custom changes to your environment, as this
12# will prevent the need for merging in future updates.
13 
14pathmunge () {   (以下省略)

lessコマンドで1ページずつ表示する

1ページに収まりきれない大きなファイルをページを送りながら表示するとき便利です。

/etc/servicesファイルを表示してみましょう

1[user01@localhost ~]$less /etc/services

less コマンドの実行

表示結果

1# /etc/services:
2# $Id: services,v 1.55 2013/04/14 ovasik Exp $
3#
4# Network services, Internet style
5# IANA services version: last updated 2013-04-10
6#
7# Note that it is presently the policy of IANA to assign a single well-known
8# port number for both TCP and UDP; hence, most entries here have two entries
9# even if the protocol doesn't support UDP operations.
10# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
11# are included, only the more common ones.
12#
13# The latest IANA port assignments can be gotten from
14#       http://www.iana.org/assignments/port-numbers (以下省略)

less コマンドや cat コマンドの実行

/etc/sysconfig以下のファイルを閲覧する場合を考えてください

1[user01@localhost ~]$less /etc/sysconfig/network
2[user01@localhost ~]$cat /etc/sysconfig/crond
3[user01@localhost ~]$less /etc/sysconfig/firewalld

同じディレクトリで作業が続く場合は、カレントディレクトリを変更すると便利です

1[user01@localhost ~]$cd /etc/sysconfig
2[user01@localhost sysconfig]$

1[user01@localhost sysconfig]$less network
2[user01@localhost sysconfig]$cat crond
3[user01@localhost sysconfig]$less firewalld


NEXT>> シンボリックリンクとハードリンク(ファイルを別名でアクセスする)

f