Learn Linux 04: Manipulating Files And Directories




This chapter includes five of the most frequently used Linux commands. This chapter will introduce the following commands:

  • cp - Copy files and directories
  • mv - Move/rename files and directories
  • mkdir - Create directories
  • rm - Remove files and directories
  • ln - Create hard and symbolic links


Before we begin using our commands, we need to talk about a shell feature that makes these commands so powerful. Because the shell uses filenames so much, it provides special characters to help you rapidly specify groups of filenames. These special characters are called wildcards. Using wildcards (which is also known as globbing) allows you to select filenames based on patterns of characters. Wildcards can be used with any command that accepts filenames as arguments.

Here is a list of wildcards and what they match:

  • * - Matches any characters.
  • ? - Matches any signle character.
  • [characters] - Matches any character that is a member of the set characters.
  • [!characters] - Matches any character that is not a member of the set characters.
  • [[:class:]] - Matches any character that is a member of the specified class.

Here is a list of commonly used character classes:

  • [:alnum:] - Matches any alphanumeric character.
  • [:alpha:] - Matches any alphabetic character.
  • [:digit:] - Matches any numeral.
  • [:lower:] - Matches any lowercase letter.
  • [:upper:] - Matches any uppercase letter.

Here is a list of examples of patterns and what they match:

  • * - Matches all files.
  • g* - Matches any file beginning with g.
  • c*.txt - Matches any file beginning with c followed by any characters and ending with .txt.
  • Back?? - Matches any file beginning with Back followed by exactly two characters.
  • [xyz]* - Matches any file beginning with either an x, y, or z.
  • BACKUP.[0-9][0-9][0-9] - Matches any file beginning with BACKUP. followed by exactly three numerals.
  • [[:upper:]]* - Matches any file beginning with an uppercase letter.
  • [![:digit:]]* - Any file not beginning with a numeral.
  • *[[:lower:]123] - Any file ending with a lowercase letter or the numerals 1, 2, or 3.


The mkdir command is used to create directories.

[user@linux ~]$ mkdir dir1 dir2 dir3


The cp command copies files or directories.

[user@linux ~]$ cp item1 item2


The mv command performs both file moving and file renaming, depending on how it is used.

[user@linux ~]$ mv item1 item2


The rm command is used to remove (delete) files and directories.

[user@linux ~]$ rm item1 item2


The ln command is used to create either hard or symbolic links. It is used in one of two ways. The following creates a hard link:

ln file link

The following creates a symbolic link:

ln -s item link

Hard links are the original Unix way of creating links, compared to symbolic links, which are more modern. By default, every file has a single hard link that gives the file its name. When we create a hard link, we create an additional directory entry for a file. Hard links have two important limitations.

  • A hard link cannot reference a file outside its own file system. This means a link cannot reference a file that is not on the same disk partition as the link itself.
  • A hard link may not reference a directory.

A hard link is indistinguishable from the file itself. Unlike a symbolic link, when you list a directory containing a hard link, you will see no special indication of the link. When a hard link is deleted, the link is removed, but the contents of the file itself continue to exist (that is, its space is not deallocated) until all links to the file are deleted.

It is important to be aware of hard links because you might encounter them from time to time, but modern practice prefers symbolic links, which we will cover next.

Symbolic links were created to overcome the limitations of hard links. They work by creating a special type of file that contains a text pointer to the referenced file or directory. In this regard, they operate in much the same way as a Windows shortcut, though of course they predate the Windows feature by many years.

A file pointed to by a symbolic link and the symbolic link itself are largely indistinguishable from one another. For example, if you write something to the symbolic link, the referenced file is written to. When you delete a symbolic link, however, only the link is deleted, not the file itself. If the file is deleted before the symbolic link, the link will continue to exist but will point to nothing. In this case, the link is said to be broken. In many implementations, the ls command will display broken links in a distinguishing color, such as red, to reveal their presence.


In this chapter we’ve covered a lot, and it will take a while for it all to fully sink in. The concept of links is a little confusing at first, but take the time to learn how they work. The key in learning is practice.