We have already introduced mkdir and rmdir for making and removing directories, and cd for navigating directories. We will now look at some of the commands which are useful in inspecting directories:
basename
The command basename filedir returns the name of the file or directory without any path information. The command basename filedir suffix also removes the given suffix, if it matches.
{ecelinux:1} basename ~/public_html/index.html index.html {ecelinux:2} basename ~/public_html/index.html .html index {ecelinux:3} basename ~/public_html/index.html .png index.html {ecelinux:4}
dirname
The command dirname filedir returns the path information to the file or directory. The resulting path will be relative or absolute depending on how the file or directory was specified
{ecelinux:1} dirname index.html . {ecelinux:2} dirname ~/public_html/index.html /home/ece250/public_html {ecelinux:3}
du
The du (directory usage) command list how many blocks are (cumulatively) stored in the current directory. The command du directory lists all of the sub-directories and their disk usage.
Options include:
- -k
- The default unit on a hard drive is the block: any file always occupies an integral number of blocks, even if all the blocks are not filled. If you want the number of kibityes used (multiples of 1024 bytes), use this option.
- -s
- This summarizes the disk usage for all the files and directories below a given directory.
For example,
{ecelinux:1} du -sk ~/public_html 83820 /home/ece250/public_html/ {ecelinux:2}
suggests that the public_html directory for ece250 contains approximately 81.9 MiB.
find
The find directory command lists all the files and subdirectories of the specified directory, including the directory itself.
Some of the more common options are:
- -follow
- Follow symbolic links (necessary for ecelinux if you want to search all your files).
- -name pattern
- Only list those files and directories which match the file name pattern (using *, [...], and ?).
- -type c
- Restrict, for example, the objects printed to files (f) or directories (d).
however, these options must appear after the directory, for example:
{ecelinux:1} find . . ./images ./images/index.html ./images/banner.png ./index.html ./index.content {ecelinux:2}
In order to run a spell checker on all of the html files in your public_html directory, use:
{ecelinux:1} foreach i (`find . -name "*.html" -follow`) foreach? ispell $i foreach? end {ecelinux:2}
The command in `...` is executed, and for each file, the command ispell on that file is executed.
To change the permission modifiers of all the files in your public_html directory to the appropriate type, you could use
{ecelinux:1} chmod 755 `find ~/public_html -follow -type d` {ecelinux:2} chmod 644 `find ~/public_html -follow -type f` {ecelinux:3}
unless, of course, you are using server-side includes, in which case, all html files must also have the permission modifiers set to 755.
Summary
This looks at two commands which can be used to examine directories. The next topic shows how you can easily move between multiple directories using pushd, popd, and dirs.
Copyright ©2005-2012 by Douglas Wilhelm Harder. All rights reserved.