# Manage directories and files

Previously, you explored how to manage the file system using Linux commands. The following commands were introduced: <var>mkdir</var>, <var>rmdir</var>, <var>touch</var>, <var>rm</var>, <var>mv</var>, and <var>cp</var>. In this reading, you’ll review these commands, the nano text editor, and learn another way to write to files.

## Creating and modifying directories

### **mkdir**

The <var>mkdir</var> command creates a new directory. Like all of the commands presented in this reading, you can either provide the new directory as the absolute file path, which starts from the root, or as a relative file path, which starts from your current directory.

For example, if you want to create a new directory called <var>network</var> in your <var>/home/analyst/logs</var> directory, you can enter <var>mkdir /home/analyst/logs/network</var> to create this new directory. If you’re already in the <var>/home/analyst/logs</var> directory, you can also create this new directory by entering <var>mkdir network</var>.

**Pro Tip**: You can use the <var>ls</var> command to confirm the new directory was added.

### **rmdir**

The <var>rmdir</var> command removes, or deletes, a directory. For example, entering <var>rmdir /home/analyst/logs/network</var> would remove this empty directory from the file system.

**Note**: The <var>rmdir</var> command cannot delete directories with files or subdirectories inside. For example, entering <var>rmdir /home/analyst</var> returns an error message.

## Creating and modifying files

### **touch and rm**

The <var>touch</var> command creates a new file. This file won’t have any content inside. If your current directory is <var>/home/analyst/reports</var>, entering <var>touch permissions.txt</var> creates a new file in the <var>reports</var> subdirectory called <var>permissions.txt</var>.

The <var>rm</var> command removes, or deletes, a file. This command should be used carefully because it’s not easy to recover files deleted with <var>rm</var>. To remove the permissions file you just created, enter <var>rm permissions.txt</var>.

**Pro Tip:** You can verify that <var>permissions.txt</var> was successfully created or removed by entering <var>ls</var>.

### **mv and cp**

You can also use <var>mv</var> and <var>cp</var> when working with files. The <var>mv</var> command moves a file or directory to a new location, and the <var>cp</var> command copies a file or directory into a new location. The first argument after <var>mv</var> or <var>cp</var> is the file or directory you want to move or copy, and the second argument is the location you want to move or copy it to.

To move <var>permissions.txt</var> into the <var>logs</var> subdirectory, enter <var>mv permissions.txt /home/analyst/logs</var>. Moving a file removes the file from its original location. However, copying a file doesn’t remove it from its original location. To copy <var>permissions.txt</var> into the <var>logs</var> subdirectory while also keeping it in its original location, enter <var>cp permissions.txt /home/analyst/logs</var>.

**Note**: The <var>mv</var> command can also be used to rename files. To rename a file, pass the new name in as the second argument instead of the new location. For example, entering <var>mv permissions.txt perm.txt</var> renames the <var>permissions.txt</var> file to <var>perm.txt</var>.

## nano text editor

**nano** is a command-line file editor that is available by default in many Linux distributions. Many beginners find it easy to use, and it’s widely used in the security profession. You can perform multiple basic tasks in nano, such as creating new files and modifying file contents.

To open an existing file in nano from the directory that contains it, enter <var>nano</var> followed by the file name. For example, entering <var>nano permissions.txt</var> from the <var>/home/analyst/reports</var> directory opens a new nano editing window with the <var>permissions.txt</var> file open for editing. You can also provide the absolute file path to the file if you’re not in the directory that contains it.

You can also create a new file in nano by entering <var>nano</var> followed by a new file name. For example, entering <var>nano authorized\_users.txt</var> from the <var>/home/analyst/reports</var> directory creates the <var>authorized\_users.txt</var> file within that directory and opens it in a new nano editing window.

Since there isn't an auto-saving feature in nano, it’s important to save your work before exiting. To save a file in nano, use the keyboard shortcut <var>Ctrl + O</var>. You’ll be prompted to confirm the file name before saving. To exit out of nano, use the keyboard shortcut <var>Ctrl + X</var>.

**Note**: Vim and Emacs are also popular command-line text editors.

## Standard output redirection

There’s an additional way you can write to files. Previously, you learned about standard input and standard output. **Standard input** is information received by the OS via the command line, and **standard output** is information returned by the OS through the shell.

You’ve also learned about piping. **Piping** sends the standard output of one command as standard input to another command for further processing. It uses the pipe character (<var>|</var>).

In addition to the pipe (<var>|</var>), you can also use the right angle bracket (<var>&gt;</var>) and double right angle bracket (<var>&gt;&gt;</var>) operators to redirect standard output.

When used with <var>echo</var>, the <var>&gt;</var> and <var>&gt;&gt;</var> operators can be used to send the output of <var>echo</var> to a specified file rather than the screen. The difference between the two is that <var>&gt;</var> overwrites your existing file, and <var>&gt;&gt;</var> adds your content to the end of the existing file instead of overwriting it. The <var>&gt;</var> operator should be used carefully, because it’s not easy to recover overwritten files.

When you’re inside the directory containing the <var>permissions.txt</var> file, entering <var>echo "last updated date" &gt;&gt; permissions.txt</var> adds the string “last updated date” to the file contents. Entering <var>echo "time" &gt; permissions.txt</var> after this command overwrites the entire file contents of <var>permissions.txt</var> with the string “time”.

**Note:** Both the <var>&gt;</var> and <var>&gt;&gt;</var> operators will create a new file if one doesn’t already exist with your specified name.

## Key takeaways

Knowing how to manage the file system in Linux is an important skill for security analysts. Useful commands for this include: <var>mkdir</var>, <var>rmdir</var>, <var>touch</var>, <var>rm</var>, <var>mv</var>, and <var>cp</var>. When security analysts need to write to files, they can use the nano text editor, or the <var>&gt;</var> and <var>&gt;&gt;</var> operators.