Bookmark this page

Manipulating Files and Directories

Objectives

  • Manipulate directories and files, and use command-line tools to search for their locations.

Manipulating Files and Directories

Creating, copying, moving, and renaming files and directories are everyday operations for any computer user. In Linux, you perform these tasks in the Files application on the GNOME desktop, or on the command line.

Creating Directories

To create a directory in the GNOME desktop, launch the Files application. Right-click the window background and select New Folder from the context menu. In the New Folder dialog that opens, enter a name for the directory and then click Create.

Figure 4.4: Creating a directory in Files

To create a directory on the command line, use the mkdir command with a name for the new directory.

[user@host ~]$ mkdir MyNewFolder

The terminal interprets a space as the delimiter for a new argument or command, so you must enclose the directory name in quotation marks if you want to include spaces.

[user@host ~]$ mkdir "My New Folder"

The mkdir command can create several directories with one command line. You can also create directories inside other directories.

[user@host ~]$ mkdir Folder1 Documents/Folder2
[user@host ~]$ ls
Desktop     Downloads   My New Folder   Pictures    Videos
Documents   Folder1     Music           Public      Templates
[user@host ~]$ ls Documents
Folder2

Create a directory with a subdirectory at the same time by using the mkdir command with -p option. In the following example, you create the July directory and the Monthly Reports parent directory in the home directory.

[user@host ~]$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[user@host ~]$ mkdir -p "Monthly Reports"/July
[user@host ~]$ ls "Monthly Reports"/
July

Creating Files

To create a file in a desktop environment, you use an application, such as gedit. After you save a file you that are working on, the application creates the file. However, on the command line, you can create an empty file with the touch command, followed by the file path of the file that you intend to create.

[user@host ~]$ touch Documents/example.txt
[user@host ~]$ file Documents/example.txt
Documents/example.txt: empty

You can also create a file by using shell redirection. By redirecting standard output into a file, the shell creates the file if it does not already exist.

[user@host ~]$ echo "hello world" > Documents/hello.txt
[user@host ~]$ file Documents/hello.txt
Documents/hello.txt: ASCII text

Copying Files and Directories

To copy a file or a directory in the GNOME desktop, launch the Files application. Click the file or directory that you want to copy and with the Control key pressed, drag the item to a new location and then release the Control key.

Alternatively, you can right-click a file or directory and select Copy from the context menu. Navigate to the target location, right-click the window background, and select Paste from the context menu.

Figure 4.5: Copying a directory in Files

To copy a file or directory on the command line, use the cp command, followed by the name of the file that you intend to copy and its target location.

[user@host ~]$ cp example.txt Documents
[user@host ~]$ ls
Desktop     Downloads    My New Folder   Pictures     Videos
Documents   example.txt  Music           Public       Templates
[user@host ~]$ ls Documents
example.txt

When copying a directory, use the --recursive (or -r) option to copy the directory and all its contents. Without the --recursive option, the cp command cannot copy a directory.

[user@host ~]$ cp --recursive packages Downloads
[user@host ~]$ ls Downloads
packages

Copying files and directories does not mean that you must also move them to a different working directory. You can copy an item in place by giving it a new name.

[user@host ~]$ cp --recursive Documents Examples
[user@host ~]$ ls
Desktop     Downloads   Music     Public   Templates
Documents   Examples    Pictures  Videos
[user@host ~]$ ls Documents
example.txt
[user@host ~]$ ls Examples
example.txt

You can also copy a file to a new location and give it a new name.

[user@host ~]$ cp --recursive Documents Downloads/Examples
[user@host ~]$ tree Downloads
Downloads/
└── Examples
    └── example.txt

Moving Files and Directories

To move a file or directory to a new location in the GNOME desktop, launch the Files application. Click the file or directory that you intend to move, drag it to a new location, and then release the item.

Alternatively, you can right-click a file or directory and select Cut from the context menu. Navigate to the target location, right-click the window background, and select Paste from the context menu.

Figure 4.6: Moving a directory in Files

To move a file or directory on the command line, use the mv command, followed by the file that you intend to move and its target location.

[user@host ~]$ ls
Desktop     Downloads    Music      Videos   Templates
Documents   example.txt  Pictures   Public
[user@host ~]$ mv example.txt Documents
[user@host ~]$ ls
Desktop     Downloads   Pictures    Videos
Documents   Music       Public      Templates
[user@host ~]$ ls Documents
example.txt

To move several files and directories at the same time, list all the items that you intend to move, and then a single existing destination.

[user@host ~]$ mkdir Folder1 Folder2 Folder3
[user@host ~]$ mv Folder1 Folder2 Documents
[user@host ~]$ ls Documents
Folder1    Folder2

Renaming Files and Directories

To rename a file in the GNOME desktop, launch the Files application and locate the item that you intend to rename. Right-click the file or directory that you want to rename and select Rename from the context menu. In the Folder name dialog that opens, type a new name and then click Rename, or press Enter.

Figure 4.7: Renaming a file or directory in Files

Using the command line to rename a file or directory is the same as moving the item to the directory it is already in, but with a new name.

[user@host ~]$ ls
Desktop     Downloads    Music      Videos   Templates
Documents   example.txt  Pictures   Public
[user@host ~]$ mv example.txt file.txt
[user@host ~]$ ls
Desktop     Downloads    Music      Videos   Templates
Documents   file.txt     Pictures   Public

Creating Shortcuts to Files and Directories

You can store a file or directory in two different locations at the same time by creating a link. By default, links are disabled in the GNOME Files application. To enable links, click the main menu in the upper right corner of the Files window and click Preferences. In the Preferences window, set Create Link to on.

Figure 4.8: Enabling links in the Files application

In the Files window, right-click a directory, and then select Create Link. A link, or a shortcut, to the selected directory appears in the same window.

Figure 4.9: Creating a link in the Files application

You can create shortcuts to files in the same way. When you open a shortcut to a file, you open the linked file.

To create a link to a file or a directory on the command line, use the ln command with the --symbolic option. The command requires an item that exists, followed by the shortcut that you intend to create.

[user@host ~]$ ln --symbolic Documents Examples
Desktop     Downloads    Music      Videos   Templates
Documents   Examples     Pictures   Public
[user@host ~]$ ls Documents
example.txt
[user@host ~]$ ls Examples
example.txt

Similar to a pointer in programming, a link is a resource that helps you quickly access a file or directory. Using links can be useful to access directories with long paths, or items with long or complicated names.

You can confirm that a file is a link by using the file or the tree command.

[user@host ~]$ file Examples
Examples: symbolic link to Documents/
[user@host ~]$ tree -L 1
.
├── Desktop
├── Documents
├── Downloads
├── Examples -> Documents/
...output omitted...

Removing Files and Directories

To remove a file or a directory in the GNOME desktop, right-click the item in the Files application and then select Move to Trash from the context menu.

Figure 4.10: Deleting a file or directory in Files

To confirm that the file has been moved to the Trash, or to recover it after mistakenly moving it there, click the Trash icon in the left panel of the Files window.

GNOME Desktop Manager (GDM) uses the Trash directory to keep deleted files or directories in case you want to restore them. GDM creates the Trash directory under the .local hidden directory of the user's home directory. On the command line, you can explore the files sent to the Trash directory at the /home/student/.local/share/Trash/files file path.

To remove a file or a directory on the command line, you can use the rm command, or use the mv command to safely move items to the Trash directory. After you remove a file by using the rm command, there is no easy way to recover it, and no guarantee that the file can be recovered intact even with recovery software.

[user@host ~]$ rm file.txt

Warning

Unlike removing a file in the Files application, the rm command removes the file from the system. The rm command is dangerous because it does not use a Trash directory, so there is no way to undo the removal.

A safer method is to create a link to the Trash directory by using the ln command:

[user@host ~]$ ln --symbolic /home/user/.local/share/Trash/files /home/user/Trash

Next, use the mv command to move the file to the Trash directory:

[user@host ~]$ mv example.txt Trash
[user@host ~]$ ls Trash
example.txt

This directory is the same Trash directory location as the one on your GNOME desktop.

Figure 4.11: Contents of the Trash directory in Files

Finding Files and Directories

If you are not sure where a file is located, then you can search for it by using the GNOME desktop or the command line. To search for a file or directory, launch the Files application and click the Magnifying Glass icon in the top toolbar. Type the file name, or some part of it, into the text field and review the results in the Files window.

Figure 4.12: Searching files in Files

On the command line, you can find a file by using the find command. To find a file by its name (ignoring capitalization), use the find command followed by the path that you want to search, and then the -iname option along with the file name.

[user@host ~]$ find /home/user -iname example.txt
/home/user/.local/share/Trash/files/example.txt
/home/user/Downloads/Examples/example.txt
/home/user/Documents/example.txt

Linux users commonly use the find command with redirection. This technique is useful because the find command can produce a large amount of output, and the command does not separate the expected results from the errors. By using redirection, you can send standard output and standard error to separate files. This way, you can focus on analyzing results or errors when viewing the resulting files.

[user@host ~]$ find /home/user -iname example.txt 1> results.txt 2> errors.txt
[user@host ~]$ cat results.txt
/home/user/.local/share/Trash/files/example.txt
/home/user/Downloads/Examples/example.txt
/home/user/Documents/example.txt
[user@host ~]$ cat errors.txt
find: './Private': Permission denied

References

cp(1), mkdir(1), touch(1) ln(1), mv(1), rm(1), and find(1) man pages

For information about additional commands related to organizing files and directories, refer to the rmdir(1), locate(1), and updatedb(8) man pages.

For more information about recovering files, refer to How to Prevent and Recover from Accidental Data Deletion at https://learn.spidernet.pl/sysadmin/recover-file-deletion-linux

Revision: rh104-9.1-3d1f2bc