Chapter 7. Working with the file-system

Table of Contents
Moving around the filesystem
Working with files and folders
Mass Rename/copy/link Tools

The working with the file-system chapter explains a number of commands that you use to move around the file system hierarchy and manipulate the files. Also explained are finding files and how to mass-rename files.

Moving around the filesystem

cd

Change directory. Use “ cd ..” to go up one directory.

One dot '.' represents the current directory while two dots '..' represent the parent directory.

“ cd -” will return you to the previous directory (a bit like an “undo”).

You can also use cd absolute path or cd relative path (see below):

Absolute paths

An “ absolute path” is easily recognised from the leading forward slash, /. The / means that you start at the top level directory and continue down.

For example to get to /boot/grub you would type:

cd /boot/grub

This is an absolute path because you start at the top of the hierarchy and go downwards from there (it doesn't matter where in the filesystem you were when you typed the command).

Relative paths

A “ relative path” doesn't have a preceding slash. Use a relative path when you start from a directory below the top level directory structure. This is dependent on where you are in the filesystem.

For example if you are in root's home directory and want to get to /root/music, you type:

cd music

Please note that there is no / using the above cd command. Using a / would cause this to be an absolute path, working from the top of the hierarchy downward.

ls

List files and directories. Typing “ls” will list files and directories, but will not list hidden files or directories that start with a leading full stop “.”.

Example options:

  • ls -l --- long style, this lists permissions, file size, modification date, ownership.

  • ls -a --- this means "show all", this shows hidden files, by default any file or directory starting with a '.' will not be shown.

  • ls -d --- list directory entires rather than contents (see example below)

  • ls -F --- append symbols to particular files, such as * (asterisk) for executable files.

  • ls -S --- sort the output of the command in decending order sorted by size.

  • ls -R --- (recursive) to list everything in the directories below as well as the current directory.

Command syntax, either:

ls -options

This simply lists everything in the current directory, the options are not required (options such as -l, -a et cetera).

ls -options string

This lists files using a certain string. The string can contain standard wildcards to list multiple files, to learn more about standard wildcards please read the Section called Standard Wildcards (globbing patterns) in Chapter 20

You can use ls -d to show directories that match an exact string, or use standard wildcards. Type “ ls -d */” to list all subdirectories of the current directory. Depending on the setup of your aliases (see Chapter 4) you may simply be able to type lsd as the equivalent to ls -d */.

Examples for ls -d:

ls -d */

Lists all subdirectories of current directory.

ls -d string*

Lists directories that start with "string".

ls -d /usr/*/*/doc

Lists all directories that are two levels below the /usr/ directory and have a directory called “doc”, this trick can come in quite handy sometimes.

You can also use: Depending on how your aliases (see Chapter 4) are setup you can also use l, la (list all) and ll (list long) to perform the above commands

pwd

Print working directory. Print the absolute (complete) path to the directory the user is currently in.

Command syntax:

pwd

This will tell you the full path to the directory you are in, for example it may output “/usr/local/bin” if you are currently in that directory.

tree

Outputs an ASCII text tree/graph starting at a given directory (by default the current directory). This command recursively lists all files and all directories.

In other words, it will list files within the directories below the current one, as well as all files in the current directory.

tree has a large number of options, refer to the manual page for details.

Command syntax:

tree

or

tree -option(s) /optional/directory/to/list

Finding files

find

find is a tool which looks for files on a filesystem. find has a large number of options which can be used to customise the search (refer to the manual/info pages).

Note that find works with standard wildcards,the Section called Standard Wildcards (globbing patterns) in Chapter 20, and can work with regular expressions, the Section called Regular Expressions in Chapter 20.

Basic example:

find / -name file

This would look for a file named “file” and start at the root directory (it will search all directories including those that are mounted filesystems).

The `-name' option is case sensitive you can use the `-iname' option to find something regardless of case.

Use the '-regex' and '-iregex' to find something according to a regular expression (either case sensitive or case insensitive respectively).

The '-exec' option is one of the more advanced find operations. It executes a command on the files it finds (such as moving or removing it or anything else...).

To use the -exec option: use find to find something, then add the -exec option to the end, then:

command_to_be_executed (1)  then '{}' (curly brackets) (2) then the arguments (for example a new directory)  and finally a ';' (3). 

See below for an example of use this command.

(1)
This is the tool you want to execute on the files find locates. For example if you wanted to remove everything it finds then you would use -exec rm -f
(2)
The curly brackets are used in find to represent the current file which has been found. ie. If it found the file shopping.doc then {} would be substituted with shopping.doc. It would then continue to substitute {} for each file it finds. The brackets are normally protected by backslashes (\) or single-quotation marks ('), to stop bash expanding them (trying to interpret them as a special command eg. a wildcard).
(3)
This is the symbol used by find to signal the end of the commands. It's usually protected by a backslash (\) or quotes to stop bash from trying to expand it.
find / -name '*.doc' -exec cp '{}' /tmp/ ';'

The above command would find any files with the extension '.doc' and copy them to your /tmp directory, obviously this command is quite useless, it's just an example of what find can do. Note that the quotation marks are there to stop bash from trying to interpret the other characters as something.

Excluding particular folders with find can be quite confusing, but it may be necessary if you want to search your main disk (without searching every mounted filesystem). Use the -path option to exclude the particular folder (note, you cannot have a '/' (forward slash) on the end) and the -prune option to exclude the subdirectories. An example is below:

find / -path '/mnt/win_c' -prune -o -name "string" -print

This example will search your entire directory tree (everything that is mounted under it) excluding /mnt/win_c and all of the subdirectories under /mnt/win_c. When using the -path option you can use wildcards.

Note that you could add more -path '/directory' statements on if you wanted.

find has many, many different options, refer to the manual (and info) page for more details.

slocate

slocate outputs a list of all files on the system that match the pattern, giving their full path name (it doesn't have to be an exact match, anything which contains the word is shown).

Replaces locate: Secure locate is a replacement for locate, both have identical syntax. On most distributions locate is an alias to slocate.

Commmand syntax:

slocate string

This won't work unless: You need to run either updatedb (as root) or slocate -u (as root) for slocate to work.

whereis

whereis locates the binary, source, and manual page for a particular program, it uses exact matches only, if you only know part of the name use slocate.

Command syntax:

whereis program_name
which

Virtually the same as whereis, except it only finds the executable (the physical program). It only looks in the PATH (environment variable) of a users shell.

Use the -a option to list all occurances of the particular program_name in your path (so if theres more than one you can see it).

Command syntax:

which program_name