Linux File Management: Create, Copy, Move, and Delete
Learn about the commands that create, read, write, modify, copy, move, and delete files and directories. They are absolutely necessary for any administrator.
cp command
Usage:
$ cp [opções] arquivo destino
The cp command copies files to other files or to directories. The cp command can copy one file or multiple files.
The most common options are:
- -d: Preserves links when copying files;
- -p: Preserves all file attribute information, such as file owner, group, permissions, and date;
- -R: Copies files recursively. Useful for copying files and directories below the specified directory;
- -a: Does the same as the combined "-dpR" options;
- -f: Forces the copy by overwriting the destination;
- -i: Prompts the user before copying each file over the destination location;
- -v: Shows the name of each copied file.
Examples:
$ cp arquivo1 arquivo2
Copies arquivo1 to arquivo2 in the same directory
$ cp arquivo1 /tmp
Copies arquivo1 to the /tmp directory
$ cp /tmp/arquivo1 .
Copies arquivo1 located in the /tmp directory to the local directory. The "." (dot) symbol specifies the local directory.
$ cp –Rf /var/log/ loglocal
Copies all contents of the /var/log directory, including all subdirectories, to the loglocal directory.
mv command
Usage:
$ mv [opções] origem destino
The mv moves or renames files and directories. It does not change the attributes of the moved files or directories if the transfer is on the same filesystem. If the destination where the files or directories are being moved to does not exist, the command renames the source, otherwise the data is overwritten.
The most common options are:
- -f: Forces the movement of files by suppressing confirmation messages to overwrite.
- -i: Prompts the user before moving each file.
Examples:
$ mv arquivo1 arquivo2
Renames arquivo1 to arquivo2
$ mv arquivo1 /tmp
Moves arquivo1 to the /tmp directory.
rm command
Usage:
$ rm [opções] arquivos
The rm command is used to remove files. To remove files you need write permission in the directory where the file is located. Using the –R and –f options it also removes directories.
The options are:
- -f Forces the removal of files without asking the user.
- -R Removes a directory and all its contents.
Examples:
$ rm arquivo1
Removes arquivo1.
$ rm –Rf documentos
Removes the documentos directory and all its contents.
mkdir command
Usage:
$ mkdir [opções] diretório
Creates one or more directories below the current directory. You need write permissions in the directory to execute mkdir.
The most common options are:
- -p: Creates the specified directory even if the parent directory does not exist. In this case, it also creates the parent directory;
- -m: Sets the permission of the created directory. The possible permissions are rxwrxwrxw (we will see later).
Examples:
$ mkdir documentos
Creates the documentos directory.
$ mkdir –p documentos/cartas
Creates the cartas directory below the documentos directory. If documentos does not exist, it creates it first before cartas.
$ mkdir –m 777 documentos/modelos
Creates the modelos directory below the documentos directory with read, write, and execute permissions for the directory owner, their group, and other users.
rmdir command
Usage:
$ rmdir [opções] diretórios
Removes one or more directories from the system. The directory must be empty.
touch command
Usage:
$ touch [opções] arquivos
The touch command changes the access and/or modification date and time of files.
The most commonly used options are:
- -a: Changes only the access date and time to the current one;
- -m: Changes only the modification date and time to the current one;
- -t datetime: Changes the time and date to the defined datetime. The format is YYYYMMDDhhmm. For example: The representation of October 11, 2003 at 19:00 is 200310111900.
Example:
$ touch arquivo1
Changes the date and time of arquivo1 to the current date and time.
file command
Usage:
$ file [opções] arquivos
The file command tells you what type a file is, through a series of tests.
In Linux, files are recognized and handled not by their extension, but through the first two bytes of the file, which are called "magic numbers". Depending on the file type, it will be recognized by the shell, handled, and interpreted appropriately.
Example:
$ file file teste.php
teste.php: PHP script, ASCII text
#file nginx
nginx: ELF 64-bit LSB executable, x86-64, version 1
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Like this? Share it
That was just one piece. The course teaches the whole path — from your first command to exam day.