Useful Tips

Copy, move, rename and delete files and directories on Linux

Pin
Send
Share
Send
Send


Our experienced team of editors and researchers contributed to this article and tested it for accuracy and completeness.

The wikiHow content management team carefully monitors the work of editors to ensure that each article meets our high quality standards.

In this article, you will learn how to copy and paste a file on a Linux computer. This can be done using the terminal, keyboard shortcuts or the context menu (if you have a Linux distribution with a graphical user interface).

Basic Linux File System Commands

There are only a few commands that allow you to flexibly manage the file system. All file managers from sets of various graphical shells for Linux and UNIX systems are based on these commands. The set of these commands includes:

  • cd - moving through the directory tree of the file system. The destination is indicated as a parameter - any directory along a relative or absolute path,
  • ls - view the contents of the directory. By default, displays the contents of the current (active) directory, if the target directory is not specified explicitly through the parameter,
  • cp - copy files and directories. The first parameter is the source, the second is the final copy object,
  • rm - delete files. Can also be used to delete directories,
  • rmdir - delete directories,
  • mv - Moving or renaming files or directories. As parameters, first indicate the source, then the final object. To rename, the absolute (or relative) paths for the source and target must match,
  • pwd - definition of the current (active) directory.

Total teams less than a dozen, but this is an impressive arsenal.

Navigate the file system tree

To get to the right place in the file system, for example, to the directory where the Apache virtual host settings files are stored, you need to give the command (for Ubuntu, for example):

Now the pwd command will print this path, the final directory of which (sites-available) has become active:

Next, the team ls You can view the contents of this directory and find out which virtual hosts are available to the web server:

Or, what, the same thing:

Using the ls command, you can also view hidden files and directories (whose names begin with a dot), as well as access modes by adding the -a and -l keys. It should be noted that if the -a option is used, pseudo directories with the names “.” And “will also be shown. . ”, Denoting the current and directory level up, respectively. For example, if you want to run any executable file (script or program) in the current directory, you should give the following command:

To go to the directory, one level up:

Go two levels higher:

Go to the somedir1 directory, which is located in the directory one level up:

"Is used to indicate the home directory of the current user, for example, the following command:

will transfer the user to the directory with the site on the CMS Drupal (for example ...) located in the htdocs directory of his home directory. If there are space characters in the file or directory name, then in this case it is necessary to enclose such fragments in double quotes:

Actions with files and directories

Some commands for their full use must be performed using additional keys and mechanisms. For example, the rmdir command cannot delete a directory if it is not empty. To use the cp command in some cases, the source directory must be the active directory. To process several files or directories, you can use a mask to select them, as well as recursion using the -R option.

Syntax and Options

General syntax cp looks like this:

$ cp options file source file receiver

$ cp options file source directory destination /

After executing the command, the source file will be completely transferred to the destination file. If a slash is specified at the end, the file will be written to the specified directory with the original name.

The utility has several interesting options that can greatly help with non-standard copy tasks, so let's look at them:

  • --attributes-only - do not copy the contents of the file, but only the access and owner flags,
  • -f, --force - overwrite existing files,
  • -i, --interactive - ask whether to overwrite existing files,
  • -L - copy not symbolic links, but what they point to,
  • -n - do not overwrite existing files,
  • -P - do not follow symbolic links,
  • -r - copy the Linux folder recursively,
  • -s - Do not copy files on Linux, but create symbolic links,
  • -u - copy the file only if it has been modified,
  • -x - do not go beyond this file system,
  • -p - save the owner, timestamps and access flags when copying,
  • -t - consider the destination file as a directory and copy the source file to this directory.

Copy

When, for example, you need to create a configuration for a new virtual host, then in this case it is easiest to use a copy of the existing configuration by default:

Now you can make the settings specific to the new virtual host in the drupal8-devtest.conf file.

In order to copy all the files in a directory, it is very desirable to make the source directory active, i.e., first go to it:

As a result, all files (with all subdirectories and their contents) from the / etc / skel / directory are copied to the current user's home directory. The -R switch allows you to copy recursively, that is, for all subdirectories, the -p switch tells the cp command to save information about the owner and file access modes. By default, the cp command overwrites existing files when copying. In order to force cp to request permission to rewrite each time, you need to add the -i switch (interactive mode). You can also tell cp to never overwrite files — use the -n switch to do this. The period “.” Symbol indicates that all files in the current directory are processed.

When you need to copy certain files, for example, starting with “drupal” or with a certain extension, you can use the mask task using the asterisk “*” symbol:

Examples of copying files in linux

Now that you know the basic options, you can move on to practice. For example, we want to copy a certain picture from the home folder to the pictures subdirectory:

Or we can explicitly specify the name of the new picture:

Copying folders using the key -r:

After executing this copy command

/ folder will be copied to the folder

/Documents. The main thing is do not forget to put a slash at the end of the expression or use the option -t. Otherwise the folder

/ documents will be overwritten.

Default command cp Linux overwrites existing files or folders, but you can force the utility to ask if you want to overwrite each file if you are not sure about the correctness of the command:

There is an opposite option. -n, meaning "never overwrite existing files".

Option -u Useful in the following case: you know or assume that the directory where the file is copied has its old version, then the -u operator will replace it with the new version:

Cp also supports special replacement characters * and ?. For example, the following command will copy all files starting with test:

If you need to apply more complex regular expressions, you have to combine the utility cp from find or egrep.

In case it is important to preserve access rights to the file and its owner, you need to use the option -p:

To simplify the use of the command, you can use curly brace syntax. For example, to back up a file, run:

A file with the same name and extension .bak will be created

Default to cp file copy progress is not displayed, which is very inconvenient when working with large files, but it can be easily viewed using the utility cv.

Moving and renaming files

Moving differs from renaming in that when moving at the source and the receiver, the paths (absolute and / or relative) must be different. When renaming, only the file / directory name can differ. Next command:

just rename the drupal8-devtest.conf file to drupal8-devtest_back.conf.

will transfer the same file to the backup directory of the current user's home directory. And give it a new name drupal8-devtest_back.conf.

To rename a directory, as in the case of the cp command, you often need the directory in which the renaming takes place or from which the transfer is made, you need to make it active:

Moving the file (or directory with all its contents) to another place, you need to give the command:

For the mv command, you can also use a mask to select only specific files / directories:

Team rm - one of the most dangerous. With careless and inattentive use, you can delete the whole file system, which will then have to be restored. To delete a file, run the following command:

Using the -i option is highly recommended:

This command, in addition to all files and directories in the downloads directory, will also delete (by default) this directory itself. Therefore, it is extremely important to control its actions using the -i option. You can also use masks with the asterisk character for the rm command.

To familiarize yourself with all the options available for each command, use the –help switch to display a quick reference.

If you find an error, please select a piece of text and press Ctrl + Enter.

Linux special copying of files using tar

Linux is interesting in that it allows you to perform the same action in different ways. Copying on Linux can also be done not only with cp. When transferring system files to another directory, backing up system files, etc. It is important that the attributes, values ​​of the file owners and symbolic links are preserved as they are, without any modification.

Utility cp can also cope with such a task? if you specify an option -pbut you can use the archiving utility tar. We will not create any archive files, but build a tunnel. The first part of the command packs the file and sends it to standard output, and the other immediately unpacks it to the desired folder:

tar cf - / var | (cd / mnt / var && tar xvf -)

Here we completely copy the contents of the / var folder to the / mnt / var folder. So you can copy the Linux folder, and absolutely any or even the whole operating system.

Now you know how to copy files from Ubuntu to Linux in general. As you can see, in the terminal this is much faster and more efficient than using the graphical interface, if you remember the necessary commands. If you have any questions, ask in the comments!

Pin
Send
Share
Send
Send