Automating Directory Creation with mv Command in Linux/Unix Systems

Nov 21, 2025 · Programming · 10 views · 7.8

Keywords: Linux | mv command | directory creation | shell scripting | system administration

Abstract: This technical paper explores methods to automatically create target directories when using the mv command in Linux/Unix systems. Through detailed analysis of the mkdir -p command combined with the $_ parameter, it presents a comprehensive solution for creating directory chains and moving files in one step. The paper includes complete code examples, execution demonstrations, and compatibility analysis across different shell environments, providing practical command-line techniques for system administrators and developers.

Problem Background and Requirements Analysis

In daily Linux/Unix system operations, file movement is a fundamental and frequent task. The standard mv command requires the target directory to exist, otherwise the operation fails. This creates inconvenience in practical work, especially when files need to be moved to multi-level nested directory structures.

Core Solution: Combination of mkdir -p and $_ Parameter

By combining the mkdir -p command with the $_ parameter, automatic directory creation and file movement can be achieved. The mkdir -p command creates directories and all parent directories, while the $_ parameter references the last argument of the previous command, which is the newly created directory path.

Detailed Implementation Steps

The complete command sequence is as follows:

mkdir -p ./some/path/
mv yourfile.txt $_

Let's analyze this solution step by step:

  1. mkdir -p ./some/path/: Creates the specified directory path, including all intermediate directories
  2. mv yourfile.txt $_: Moves the file to the newly created directory, with $_ automatically expanding to the last argument of the previous command

Practical Application Example

Here is a complete usage scenario demonstration:

$ > ls
$ > touch yourfile.txt
$ > ls
yourfile.txt
$ > mkdir -p ./some/path/
$ > mv yourfile.txt $_
$ > ls -F
some/
$ > ls some/path/
yourfile.txt

Technical Details Analysis

The -p parameter (or --parents) in the mkdir -p command is crucial, as it ensures:

The $_ parameter is a special variable in bash shell that stores the last argument of the previous command. This design makes command combinations more concise and efficient.

Compatibility Considerations

While this solution works well in bash shell, adjustments may be needed in other shell environments:

Alternative Solutions Comparison

Another viable approach uses command substitution:

mkdir -p $(dirname /destination/moved_file_name.txt)
mv /full/path/the/file.txt /destination/moved_file_name.txt

This method extracts the directory path using the dirname command and then creates the directory. While functionally similar, it is less concise than the $_ parameter solution.

Best Practice Recommendations

Based on practical experience, we recommend:

Conclusion

Through the combination of mkdir -p and the $_ parameter, we have achieved an efficient integrated solution for file movement and directory creation. This method not only simplifies command-line operations but also improves work efficiency, making it a valuable technique in Linux/Unix system administration.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.