1. Overview
Hidden files, also called dotfiles, are files whose name starts with the dot (.) character. Typically, these files are used to store configurations — for example, the /home/user/.config file might store configuration settings for a user.
In this tutorial, we’ll discuss how to move all the files including hidden files from a directory into its parent directory using the mv and rsync commands.
2. Using mv Command
The mv command is used to move files and directories from one place to another. We can also use it to rename files and directories.
Let’s see a basic example of how to move all files to the parent directory using the mv command:
mv /path/subfolder/* /path/
This will move all the files from /path/subfolder to /path/ except for hidden files and directories.
So, let’s see how we can also move the hidden files and directories:
mv -f /path/subfolder/{.,}* /path/
The above command expands to:
mv /path/subfolder/* mv /path/subfolder/.* /path/
Note that the asterisk (*) means all the files in the subfolder folder, and dot-asterisk (.*) means all the hidden files in the subfolder folder. So, both file types will be copied to the path destination folder.
Also, since we’re including the -f option, any existing files and directories in the destination folder will be overwritten without prompting for confirmation.
Finally, it’s important to note that the original folder – subfolder in this case – will not be removed as part of the mv operation.
3. Using rsync
rsync is a Linux utility to move or copy files from one directory to another either locally or remotely. It also supports moving groups, permissions, links, and devices.
rsync uses a remote-update protocol to move or copy the files. This allows transferring just the differences between two sets of files.
First, let’s use rsync with the –dry-run option to see what files will be moved without performing the actual move operation:
sudo rsync --dry-run path/subfolder/ path/
Lets now see how to move all files including hidden files using rsync:
sudo rsync --remove-source-files /path/subfolder/ /path/
The above command moves all the files from the directory /path/subfolder/ to its parent*, /path/*.
Note here that rsync moves all the files, including hidden files, because we’ve used / at the end of the path. This denotes that the complete source directory should be synced with the target directory.
The –remove-source-files option will remove the files from the source directory once they are copied to the target directory. This will also remove the original folder, subfolder, as part of the sync operation**.**
Also, note we use sudo to execute the rsync command with admin privileges. This avoids any permission issues while creating the target directories.
If we don’t use sudo, and there are any permission issues while creating the target directories, the files won’t be copied, but they’ll still be removed from the original directory.
4. Conclusion
To summarize, we’ve discussed a few commands to move all files including hidden files into a parent directory in Linux. First, we saw how to do this using the mv command, and then we saw how to use rsync to achieve the same result.