Linux File Management
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
Use:
$ cp [options] destination file
The cp command copies the 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: Copy 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 writing over the destination;
- -i: Ask the user before copying each file over the destination location;
- -v: Shows the name of each file copied.
Examples:
$ cp file1 file2
Copy file1 to file2 in the same directory
$ cp file1 /tmp
Copy file:1 to the /tmp-directory
$ cp /tmp/file1.
Copy file1 located in the /tmp directory to the directory local. The symbol “.” (dot) specifies the directory as local.
$ cp —Rf /var/log/ loglocal
Copy the entire contents of the /var/log directory, including all of the subdirectories for the loglocal directory.
MV command
Use:
$ mv [options] source destination
MV moves or renames files and directories. It does not change the attributes of the files or directories moved if the transfer is the same file system. If the destination where are the files or directories that are moved do not exist, the command renames the source, otherwise the data is recorded on top.
The most common options are:
- -f: Forces the files to be moved by suppressing confirmation messages to save on top.
- -i: Ask the user before moving each file.
Examples:
$ mv file1 file2
Rename file 1 to file 2
$ mv file1 /tmp
Move file1 to the /tmp directory.
rm command
Use:
$ rm [options] files
The rm command is used to remove files. To remove files, you must have permission to write to directory where the file is located. Using the —R and —f options also removes empty directories.
The options are:
- -f Forces the removal of files without asking the user.
- -R Removes a directory and all of its content.
Examples:
$ in file1
Removes file1.
$ rm —Rf documents
Removes the documents directory and all of its contents.
Mkdir command
Use:
$ mkdir [options] directory
Creates one or more directories below the local directory. You need to have permissions to write to the directory to run 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: Configures the permission of the created directory. Possible permissions are rxwrxwrxw (we’ll see later).
Examples:
$ mkdir documents
Creates the documents directory.
$ mkdir —p documents/letters
Create the letters directory below the documents directory. If Documents don’t exist, create them first before the letters.
$ mkdir —m 777 documents/templates
Creates the templates directory under the documents directory with read, write, and execute permissions for the directory owner, his group, and other users.
Rmdir command
Use:
$ rmdir [options] directories
Removes one or more directories from the system. The directory must be empty.
Touch control
Use:
$ touch [options] files
The touch control changes the date and time of access and/or modification of the files.
The most 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 date-time: Changes the time and date to the defined date/time. The format is AAAAMMddHHmm. For example: The representation of October 11, 2003 at 19:00 is 200310111900.
Example:
$ touch file1
Change the date and time of file1 to the current time and date.
File command
Use:
$ file [options] files
O The file command tells you what the type of a file is through a series of tests.
On Linux the files are recognized and treated not by their extension, but through the first two bytes of the file, which are called “magic numbers”. Depending on the type of file, it will be recognized by the shell, treated and interpreted accordingly.
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 register here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it?
Share