Files and Directories

Let’s create a new directory called thesis using the command mkdir QE (which has no output):

mkdir qe

As you might guess from its name, mkdir means “make directory”. Since qe is a relative path (i.e., doesn’t have a leading slash), the new directory is created in the current working directory:

Since we’ve just created the qe directory, there’s nothing in it yet:

ls -F qe
ls: qe: No such file or directory

Moving Files Around

We can move our new file into the new directory with the move command, mv. The syntax of mv is $ mv file_being_moved location_moving_to. Moving our new file “QE” to our new directory “qe” can be done as follows:

mv qe QE
mv: rename qe to QE: No such file or directory
$ cd QE

Let’s change our working directory to QE using cd, then run a text editor called vi to create a file called si.in :

$ vi si.in

Which Editor?

Text editors can be used for writing code, editing text files such as configuration files, creating user instruction files, and many more. In Linux, text editors are of two kinds that is the graphical user interface (GUI) and command-line text editors (console or terminal).

Vi/Vim Editor

Vim is a powerful command-line based text editor that has enhanced the functionalities of the old Unix Vi text editor. It is one the most popular and widely used text editors among System Administrators and programmers that is why many users often refer to it as a programmer’s editor.

Vi/Vim Save and Quit command

The procedure to save a file in vi/vim and quit the editor is as follows:

  1. open the file with vi filename( e.g vi si.in)
  2. to save a file and quit press Esc key , type :wq ( or `:x`)
  3. hit Enter key

Creating Files in Different way

We have seen how to create text files using the vi/vim editor. Now, try the following command in your home directory:

touch QE/si.out

Copying Files

We can also copy files, leaving the original file while a second version is created either elsewhere or in the same location. The copy command is cp and its syntax is the same as for mv: $ cp file_being_copied location_copying_to. We can create a copy of “QE” into “qe” directory as follows:

cp QE qe 
cp: QE: No such file or directory

Removing Files and Directories

If we try to remove the entire thesis directory using rm QE, we get an error message:

rm QE
Using rm Safely

What happens when we execute rm -i QE/si.out? Why would we want this protection when using rm?

rm: remove regular file 'QE/si.out'?

The -i flag will prompt before every removal. The Unix shell doesn’t have a trash bin, so all the files removed will disappear forever. By using the -i flag, we have the chance to check that we are deleting only the files that we want to remove.

With Great Power Comes Great Responsibility

Removing the files in a directory recursively can be a very dangerous operation. If we’re concerned about what we might be deleting we can add the “interactive” flag -i to rm which will ask us for confirmation before each step

$ rm -r -i QE
  rm: descend into directory ‘QE’? y
  rm: remove regular file ‘QE/si.in’? y
  rm: remove regular file ‘QE/si.out’? y
  rm: remove directory ‘QE’? y
Key Points
  • cp old new copies a file.

  • mkdir path creates a new directory.

  • mv old new moves (renames) a file or directory.

  • rm path removes (deletes) a file.

  • * matches zero or more characters in a filename, so *.txt matches all files ending in .txt.

  • ? matches any single character in a filename, so ?.txt matches a.txt but not any.txt.

  • Use of the Control key may be described in many ways, including Ctrl-XControl-X, and ^X.

  • The shell does not have a trash bin: once something is deleted, it's really gone.

  • Depending on the type of work you do, you may need a more powerful text editor than vi.