File and Directory Operations

Tarih: 2026-06-11 | Kategori: Linux

Etiketler: Linux

In Linux's powerful command-line interface, there are numerous commands to work with files and directories. In this section, you will learn how to create, copy, move, delete, and search for files and directories. Additionally, we will look at some basic commands used to change file and directory permissions.

Creating Files and Directories

In Linux, the `touch` command is used to create files. This command can also be used to update the access and modification times of existing files, but if the file specified does not exist, it will create a new empty file.

user@hackerbox:~$ touch readme.txt

The command above creates an empty file named `readme.txt` in your current directory. To create directories, use the `mkdir` (make directory) command.

user@hackerbox:~$ mkdir documents

This command creates a new directory named `documents`.

Copying Files and Directories

To copy files, the `cp` command is used. The general usage of the command is `cp source_file target_file`.

user@hackerbox:~$ cp readme.txt readme_copy.txt

This command creates a copy of `readme.txt` named `readme_copy.txt`. When copying directories, you must use the `-r` (recursive) option, which ensures that all subdirectories and files within the directory are also copied.

user@hackerbox:~$ cp -r documents documents_copy

This command copies the `documents` directory and all its contents to a new directory named `documents_copy`.

Moving or Renaming Files and Directories

To move or rename files and directories, use the `mv` command. This command can move files and directories to a new location or rename them.

user@hackerbox:~$ mv readme_copy.txt readme_moved.txt

This command renames the file `readme_copy.txt` to `readme_moved.txt`.

user@hackerbox:~$ mv readme_moved.txt documents/

This command moves `readme_moved.txt` to the `documents` directory.

Deleting Files and Directories

To delete files, use the `rm` (remove) command.

user@hackerbox:~$ rm readme_moved.txt

This command deletes the file `readme_moved.txt`. To delete directories, you can use the `rm -r` command. This command deletes the directory along with its contents.

user@hackerbox:~$ rm -r documents

This command deletes the `documents` directory and its contents.