Move All Files Including Hidden Files Into Parent Directory in Linux


Introduction

In Linux, hidden files, also known as dotfiles, are files whose names begin with a dot (.) character. These files are often used to store configuration data or other important information that should not be changed or deleted by the user. If you have a directory with a large number of hidden files and you want to move them all to the root directory, there are several ways to do this. In this tutorial, we will discuss two methods for moving all files, including hidden files, from a directory to its home directory in Linux: the mv command and the rsync command.

Using the mv command

The mv command is used to move files and directories from one location to another, as well as to rename files and directories.

Using the mv command to move all files

To move all files from a subdirectory to its parent directory using the mv command, you can use the following syntax −

$ mv /path/subdirectory/* /path/

This command will move all files in the subdirectory folder to the path directory, except for hidden files and directories.

Using mv command to move all files including the hidden files

To move all files, including hidden files and directories, you can use the following syntax −

$ mv -f /path/subdirectory/{.,}* /path/

This command expands to −

$ mv /path/subdirectory/* /path/subdirectory/.* /path/

Here, the asterisk (*) symbol represents all files in the subdirectory folder, and the dot-asterisk (.*) symbol represents all hidden files in the subdirectory folder. Both types of files will be moved to the path directory.

The “-f” option allows you to overwrite any existing files and directories in the destination folder without being prompted. It is important to note that the original subdirectory folder will not be removed as part of the mv operation.

Using the rsync command

rsync is a Linux utility used to move or copy files from one directory to another, locally or remotely. It also supports moving groups, permissions, links and devices. rsync uses a remote update protocol to move or copy files, allowing only the differences between two sets of files to be transferred.

Using rsync to preview files to move

To see which files will be moved without actually performing the move operation, you can use the rsync command with the “--dry-run” option −

$ sudo rsync --dry-run /path/subdirectory/ /path/

Using rsync to move all files

To move all files, including hidden files, from the subdirectory directory to the root directory using rsync, you can use the following command −

$ sudo rsync --remove-source-files /path/subdirectory/ /path/

This command will move all files from the subdirectory directory to the path directory. The “--remove-source-files” option will remove files from the source directory after they have been copied to the destination directory. This option will also remove the original subdirectory folder as part of the rsync operation.

It is important to note that you will need to use sudo to run the rsync command with administrator privileges. This will help avoid permissions issues when creating target directories. If you don't use sudo and there are permission issues while creating the destination directories, the files won't be copied, but they will still be removed from the original directory.

Handling permission issues with rsync

If you experience permission problems when using the rsync command, you can try adding the “--chmod=ugo=rwX” option to your command. This option will set the permissions of copied files and directories to allow read and write access for owner, group and others.

For example, the following command will move all files, including hidden files, from the subdirectory directory to the root directory and set the permissions of the copied files and directories to allow read and write access to the owner, group and others

$ sudo rsync --chmod=ugo=rwX --remove-source-files /path/subdirectory/ /path/

Conclusion

In this tutorial, we have discussed two methods for moving all files, including hidden files, from a directory to its home directory in Linux: the mv command and the rsync command. Both methods allow you to easily move all files in a directory, including hidden files, to a new location. Regardless of whether you choose to use the mv or rsync command, moving files on Linux is a simple process that can be done with just a few commands.

Updated on: 25-Jan-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements