lsコマンド・findコマンドで、ファイル・ディレクトリのフルパス名を取得する方法

2021-06-22

概要

ls で下記のようなコマンドで実行した場合、ファイル・ディレクトリをみてもフルパスは表示されません

$ ls -l /etc/httpd/conf/
合計 92
-rw-r--r-- 1 root root 35165 10月 17 12:42 2014 httpd.conf
-rw-r--r-- 1 root root 13139 7月 23 23:18 2014 magic

$ ls -l /etc/httpd/conf.d/
合計 40
-rw-r--r-- 1 root root 392 7月 23 23:18 2014 README
-rw-r--r-- 1 root root 674 9月 30 14:59 2014 php.conf
-rw-r--r-- 1 root root 220 8月 7 16:17 2012 proxy_ajaxterm.conf
-rw-r--r-- 1 root root 9473 7月 18 15:27 2014 ssl.conf
-rw-r--r-- 1 root root 27 9月 21 00:13 2014 vhosts.conf
-rw-r--r-- 1 root root 299 7月 18 15:27 2014 welcome.conf

findでファイル・ディレクトリのフルパスを表示する方法

findだと簡単に下記のコマンドとかで、検索してHITした結果はフルパスで表示されます
使用例)find <対象ディレクトリ> -name <対象ファイル名>

$ find /etc/httpd/ -name httpd.conf
/etc/httpd/conf/httpd.conf

$ find /etc/httpd/ -name *.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/proxy_ajaxterm.conf
/etc/httpd/conf.d/vhosts.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.d/ssl.conf
/etc/httpd/conf.d/php.conf

ls コマンドでファイル・ディレクトリのフルパス表示方法

ls コマンドで findと同じようにフルパスを表示する場合は、オプションで【d】を指定します
使用例)ls -dl <対象のディレクトリ>
※ディレクトリの最後は /* とします 例) /home/hoge/* 等

  
$ ls -dl /etc/httpd/conf/*
-rw-r--r-- 1 root root 35165 10月 17 12:42 2014 /etc/httpd/conf/httpd.conf
-rw-r--r-- 1 root root 13139 7月 23 23:18 2014 /etc/httpd/conf/magic

$ ls -dl /etc/httpd/conf.d/*
-rw-r--r-- 1 root root 392 7月 23 23:18 2014 /etc/httpd/conf.d/README
-rw-r--r-- 1 root root 674 9月 30 14:59 2014 /etc/httpd/conf.d/php.conf
-rw-r--r-- 1 root root 220 8月 7 16:17 2012 /etc/httpd/conf.d/proxy_ajaxterm.conf
-rw-r--r-- 1 root root 9473 7月 18 15:27 2014 /etc/httpd/conf.d/ssl.conf
-rw-r--r-- 1 root root 27 9月 21 00:13 2014 /etc/httpd/conf.d/vhosts.conf
-rw-r--r-- 1 root root 299 7月 18 15:27 2014 /etc/httpd/conf.d/welcome.conf

但し、ls -ldで対象のディレクトリを指定しない場合は何も表示されません

 
$ cd /etc/httpd/conf
$ pwd
/etc/httpd/conf
$ ls -ld
drwxr-xr-x 3 root root 4096 5月 13 16:04 .
$ ls -ld .
drwxr-xr-x 3 root root 4096 5月 13 16:04 .
$ ls -ld ./*
-rw-r--r-- 1 root root 11810 5月 13 15:59 ./httpd.conf
-rw-r--r-- 1 root root 13077 3月 13 00:07 ./magic
drwxr-xr-x 2 root root 4096 6月 1 13:37 ./vhosts

カレントディレクトリを表示する場合は、$PWDの環境変数が指定して実行する必要があります
使用例) ls -ld $PWD/*

 
$ cd /etc/httpd/conf
$ ls -ld $PWD/*
-rw-r--r-- 1 root root 11810 5月 13 15:59 /etc/httpd/conf/httpd.conf
-rw-r--r-- 1 root root 13077 3月 13 00:07 /etc/httpd/conf/magic
drwxr-xr-x 2 root root 4096 6月 1 13:37 /etc/httpd/conf/vhosts

スポンサーリンク

0
0

LinuxCentOS 6,CentOS 7,CentOS 8

Posted by admin