How to move all files including hidden files into parent directory via *
Categories:
Mastering File Movement: Including Hidden Files to Parent Directory in Linux
Learn effective and safe methods to move all files, including hidden ones, from a subdirectory to its parent directory using various shell commands in Linux.
Moving files around in the Linux command line is a fundamental skill. While moving visible files is straightforward, including hidden files (those starting with a dot .
) requires a bit more nuance. This article will guide you through several robust methods to move all files, including hidden ones, from a source directory to its parent directory, ensuring no data is left behind.
Understanding the Challenge with Hidden Files
By default, shell globbing patterns like *
(wildcard) do not match hidden files or directories. This behavior is a safety feature to prevent accidental deletion or modification of configuration files. When you run a command like mv * ..
, it will move all non-hidden files and directories, but any files starting with a .
(e.g., .bashrc
, .config/
) will remain untouched in the source directory. Overcoming this requires specific approaches to ensure these hidden items are also included in the move operation.
flowchart TD A[Start Move Operation] --> B{Source Directory Contains Hidden Files?} B -->|No| C[Use 'mv * ..'] B -->|Yes| D{Default Globbing 'mv * ..'} D --> E[Hidden Files Ignored] E --> F[Incomplete Move] D --> G{Need to Include Hidden Files} G --> H[Use 'mv .[!.]* ..'] G --> I[Use 'shopt -s dotglob'] G --> J[Use 'find . -maxdepth 1 -exec mv {} .. \;'] C --> K[Move Complete] H --> K I --> K J --> K
Decision flow for moving files, highlighting the challenge with hidden files.
Method 1: Using mv
with Specific Globbing Patterns
This method involves using multiple globbing patterns with the mv
command to explicitly include hidden files and directories, while carefully excluding the current directory (.
) and parent directory (..
) themselves. This is a common and generally safe approach.
mv .[!.]* * ..
Moving all files, including hidden ones, using specific globbing patterns.
Let's break down the globbing patterns:
.[!.]*
: This matches all files and directories that start with a dot (.
) but are not.
or..
. The[!.]
part means "any character except a dot".*
: This matches all non-hidden files and directories...
: This is the destination, representing the parent directory.
This command effectively combines both sets of files into a single mv
operation. It's crucial to ensure that the current directory is not empty after the move, as mv
will try to move .
and ..
if not explicitly excluded by the pattern.
ls -A
or echo .[!.]* *
in the source directory first to see exactly what files will be matched before executing the mv
command. This helps prevent accidental data loss or unexpected behavior.Method 2: Temporarily Enabling dotglob
Option
The dotglob
shell option, when enabled, changes the behavior of *
to include files and directories starting with a dot. This provides a cleaner syntax for moving all files, but it's important to disable it afterward to restore default behavior.
shopt -s dotglob
mv * ..
shopt -u dotglob
Using dotglob
to move all files, including hidden ones.
Here's how it works:
shopt -s dotglob
: This command enables thedotglob
option for the current shell session.mv * ..
: Now,*
will match all files and directories, including hidden ones, except for.
and..
themselves (which are special and not matched by*
even withdotglob
).shopt -u dotglob
: This disablesdotglob
, returning the shell to its default behavior. It's good practice to always disable options you temporarily enable.
dotglob
in scripts, especially if other commands rely on the default globbing behavior. Always disable it immediately after your operation to avoid unintended side effects.Method 3: Using find
for Granular Control
For more complex scenarios or when you need very precise control, the find
command offers a powerful solution. It allows you to locate files based on various criteria and then execute a command on each found item. This method is particularly useful if you want to exclude certain hidden files or apply additional filters.
find . -maxdepth 1 -mindepth 1 -exec mv {} .. \;
Moving all files using find
with exec
.
Let's break down the find
command:
find .
: Start searching from the current directory.-maxdepth 1
: Limit the search to the current directory only (do not descend into subdirectories).-mindepth 1
: Exclude the current directory (.
) itself from the results.-exec mv {} .. \;
: Execute themv
command for each found item.{}
is a placeholder for the current file/directory found byfind
, and\;
terminates the-exec
command.
This command will find all files and directories (including hidden ones) directly within the current directory, and then move each one to the parent directory. It's robust and handles all file types correctly.
find
command is highly versatile. You can add more options like -type f
to only move files, or -not -name '.git'
to exclude specific hidden directories (e.g., a .git
repository).