TLCL: 4 - Manipulating Files and Directories

This chapter introduces the concept of wildcards (globbing), along with the cp, mv, mkdir, rm, and ln commands.

A wildcard in Unix-like systems is a special character that allows for the specification of a certain filename. It is easier to show than explain.

Let’s say we had a directory full of .txt files and we wanted to move them all to the parent directory using the shell. With the mv command, we can enter each file in the command and execute it to move all files. Fortunately there is a better way via the use of wildcards (the use of a wildcard is called globbing). The * wildcard matches any characters, so we can run a command like mv *.txt ...

Below is a list of wildcards:

Below is a list of commonly used character classes:

Below is a list of examples:

The mkdir command is used to create one or more directories (if the do not exist). The command mkdir bob alice elliot creates the directories bob, alice, and elliot. A useful option of the mkdir command is the -p option, which allows for mkdir -pbob/alice/elliot`. This will create all non-existent parent directories.

The cp command is used to copy one or more files/directories. It is important to remember, that unlike the mkdir command, copying will overwrite the destination if it exists. Similar to the mkdir command, we can pass multiple items which will all be copied. A useful option provided by the cp command, is the -u or --update option. This option only copies files that don’t exist or are newer than the existing file in the destination directory.

The mv command is used for both moving and renaming files. Like the cp command, it is important to remember that the mv command will overwrite the destination if it exists. Also like the cp command, a useful option for the mv command is the -u or --update option, which only moves files that either don’t exist or are newer. Another useful option is -i or --interactive, which prompts for confirmation before overwriting an existing file.

The rm command is used to remove/delete files. This is the most dangerous command, and should be used with caution. It is dangerous, because the rm command does not send the removed file(s) to the trash, but removes it entirely from the system. Like the mv command, the rm command has the -i or --interactive option, which prompts the user for confirmation before deleting file(s). Another useful option is the -f or --force, which ignores nonexistent files/arguments and overrides the prompt from the -i/--interactive option.

The ln command is used to create hard/symbolic links. Every file has a single hard link, which is what gives the file it’s name.

Hard links are two limitations that are important to remember.

  1. A hard link is not able to reference a file outside its own filesystem
  2. A hard link is not able to reference a directory

Symbolic links came about to overcome the limitations of hard links, by creating a special type of file that contains a text pointer to the referenced file(s).

Related:

Tags:

#tlcl #shell