DEV Community

komalta
komalta

Posted on

How to move the files in Linux?

In Linux, you can move files using the "mv" command, which stands for "move." The "mv" command allows you to relocate files or directories from one location to another within the file system. It can also be used to rename files or directories.

To move a file, you need to provide the source location (the current path and filename) and the destination location (the new path and filename). The source and destination can be specified as absolute paths or relative paths, depending on your current working directory.

For example, to move a file named "example.txt" from the current directory to a directory named "destination", you would use the following command:

mv example.txt destination/
Enter fullscreen mode Exit fullscreen mode

If you want to specify a new filename for the file being moved, you can provide the desired filename in the destination path:

mv example.txt destination/newname.txt
Enter fullscreen mode Exit fullscreen mode

In addition to moving files within the same file system, the "mv" command can also move files between different file systems, such as moving a file from one disk partition to another.

If the destination directory does not exist, the "mv" command will treat the destination as a new filename and rename the file accordingly.

It is important to note that when moving files, the "mv" command does not create a copy of the file but rather updates the file's metadata to reflect its new location. This means that after moving a file, the original file will no longer exist in its original location.

To move directories, you can use the "mv" command in a similar way as moving files. However, when moving directories that contain a large number of files or subdirectories, you may need to use the "-r" or "-R" option to enable recursive movement, ensuring that all the contents of the directory are moved as well. By obtaining Linux Training, you can advance your career as a Linux. With this course, you can demonstrate your expertise in Linux professional & help you to run applications, perform desired functions on your system and networks, create a network configuration, and maintain security administration, many more fundamental concepts, and many more critical concepts among others.

For example, to move a directory named "mydir" to the "destination" directory, including all its files and subdirectories, you can use the following command:

mv -r mydir destination/
Enter fullscreen mode Exit fullscreen mode

Remember, when moving files or directories, exercise caution to ensure that the destination location is correct and that you have the necessary permissions to perform the move operation.

Top comments (0)