Manipulate directories and files, and use command-line tools to search for their locations.
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.
To create a directory in the GNOME desktop, launch the Files application. Right-click the window background and select from the context menu. In the dialog that opens, enter a name for the directory and then click .
To create a directory on the command line, use the mkdir command with a name for the new directory.
[user@host ~]$ mkdir MyNewFolderThe 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 ~]$lsDesktop Downloads My New Folder Pictures Videos Documents Folder1 Music Public Templates [user@host ~]$ls DocumentsFolder2
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 ~]$lsDesktop Documents Downloads Music Pictures Public Templates Videos [user@host ~]$mkdir -p "Monthly Reports"/July[user@host ~]$ls "Monthly Reports"/July
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.txtDocuments/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.txtDocuments/hello.txt: ASCII text
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 from the context menu. Navigate to the target location, right-click the window background, and select from the context menu.
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 ~]$lsDesktop Downloads My New Folder Pictures Videos Documents example.txt Music Public Templates [user@host ~]$ls Documentsexample.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 Downloadspackages
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 ~]$lsDesktop Downloads Music Public Templates Documents Examples Pictures Videos [user@host ~]$ls Documentsexample.txt [user@host ~]$ls Examplesexample.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 DownloadsDownloads/ └── Examples └── example.txt
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 from the context menu. Navigate to the target location, right-click the window background, and select from the context menu.
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 ~]$lsDesktop Downloads Music Videos Templates Documents example.txt Pictures Public [user@host ~]$mv example.txt Documents[user@host ~]$lsDesktop Downloads Pictures Videos Documents Music Public Templates [user@host ~]$ls Documentsexample.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 DocumentsFolder1 Folder2
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 from the context menu. In the dialog that opens, type a new name and then click , or press Enter.
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 ~]$lsDesktop Downloads Music Videos Templates Documents example.txt Pictures Public [user@host ~]$mv example.txt file.txt[user@host ~]$lsDesktop Downloads Music Videos Templates Documents file.txt Pictures Public
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 . In the window, set to on.
In the Files window, right-click a directory, and then select . A link, or a shortcut, to the selected directory appears in the same window.
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 ExamplesDesktop Downloads Music Videos Templates Documents Examples Pictures Public [user@host ~]$ls Documentsexample.txt [user@host ~]$ls Examplesexample.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 ExamplesExamples: symbolic link to Documents/ [user@host ~]$tree -L 1. ├── Desktop ├── Documents ├── Downloads ├── Examples -> Documents/ ...output omitted...
To remove a file or a directory in the GNOME desktop, right-click the item in the Files application and then select from the context menu.
To confirm that the file has been moved to the Trash, or to recover it after mistakenly moving it there, click the 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.txtUnlike 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/TrashNext, use the mv command to move the file to the Trash directory:
[user@host ~]$mv example.txt Trash[user@host ~]$ls Trashexample.txt
This directory is the same Trash directory location as the one on your GNOME desktop.
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 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.
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.txtLinux 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.txtfind: './Private': Permission denied
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