
In the world of Linux and Unix, managing files and directories is a regular task. This often involves moving files from one location to another. However, moving hidden files can be a bit tricky, especially when using Bash’s wildcard (*
). In this article, we’ll walk you through the process of moving hidden files using Bash’s wildcard.
To move hidden files using Bash’s wildcard, you can enable the dotglob
option with the command shopt -s dotglob
, which includes hidden files in globbing. Alternatively, you can use a specific glob pattern like .[!.]*
to exclude .
and ..
. Another option is to use brace expansion, such as mv a/{.*,*} b/
, to move both hidden and non-hidden files. Lastly, you can use the find
command with options like -mindepth 1
and -maxdepth 1
to move hidden files.
Understanding Hidden Files
In Unix-based systems, a file is considered hidden if its name begins with a dot (.
). These files are usually configuration files that are hidden to prevent accidental modification or deletion. When you use the ls
command or the wildcard (*
), these hidden files are not included in the output.
Using Bash’s Wildcard
Bash’s wildcard (*
) is a powerful tool that allows you to perform operations on multiple files at once. However, by default, it does not include hidden files. This is where the dotglob
shell option comes into play.
Enabling dotglob
The dotglob
option in Bash includes hidden files in globbing (the process of expanding the wildcard character). To enable dotglob
, you can use the shopt -s dotglob
command:
shopt -s dotglob
mv /tmp/home/rcook/* /home/rcook/
In the above command, shopt -s dotglob
enables the dotglob
option. The mv
command moves all files (including hidden ones) from /tmp/home/rcook/
to /home/rcook/
.
You can make dotglob
the default behavior by adding shopt -s dotglob
to your ~/.bashrc
file.
Using a Specific Glob Pattern
If you don’t want to enable dotglob
, you can use a more specific glob pattern to exclude .
and ..
:
mv /tmp/home/rcook/.[!.]* /home/rcook/
This pattern .[!.]*
matches all files starting with a dot (.
), excluding .
and ..
. If there are files whose name begins with two dots (..something
), also use the pattern ..?*
.
Using Brace Expansion
Brace expansion is another method to move both hidden and non-hidden files:
mv a/{.*,*} b/
This command expands to mv a/.* a/* b/
, moving all files starting with a dot and anything else in the a/
directory to the b/
directory.
Using the Find Command
The find
command is a powerful tool that can be used to move hidden files:
find /tmp/home/rcook/ -mindepth 1 -maxdepth 1 -exec mv {} /home/rcook/ \;
This command finds all files in /tmp/home/rcook/
(excluding subdirectories) and executes mv
to move them to /home/rcook/
. The -mindepth 1
option ensures that find
does not include the current directory in its output, and -maxdepth 1
prevents find
from descending into subdirectories.
Conclusion
Moving hidden files using Bash’s wildcard can be tricky, but with the right commands and options, it becomes a straightforward task. Whether you choose to enable dotglob
, use a specific glob pattern, use brace expansion, or use the find
command, each method has its own advantages. Choose the one that best fits your needs. Remember to adjust the source and destination paths according to your specific scenario. Happy file moving!
To enable dotglob
in Bash, you can use the command shopt -s dotglob
. This command sets the dotglob
shell option, which includes hidden files in globbing.
Yes, you can make dotglob
the default behavior in Bash by adding shopt -s dotglob
to your ~/.bashrc
file. This ensures that dotglob
is enabled every time you start a new Bash session.
If you don’t want to enable dotglob
, you can use a specific glob pattern to exclude .
and ..
. For example, the pattern .[!.]*
matches all files starting with a dot (.
), excluding .
and ..
. If there are files whose name begins with two dots (..something
), also use the pattern ..?*
.
Yes, you can move both hidden and non-hidden files at once using brace expansion. For example, the command mv a/{.*,*} b/
expands to mv a/.* a/* b/
, moving all files starting with a dot and anything else in the a/
directory to the b/
directory.
To move hidden files using the find
command, you can use the following command: find /tmp/home/rcook/ -mindepth 1 -maxdepth 1 -exec mv {} /home/rcook/ \;
. This command finds all files in /tmp/home/rcook/
(excluding subdirectories) and executes mv
to move them to /home/rcook/
. The -mindepth 1
option ensures that find
does not include the current directory in its output, and -maxdepth 1
prevents find
from descending into subdirectories.