How to move files or directories in linux/unix

To move files or directories, you can use the mv command. It supports moving files or directories to another directory, and also supports regular matching.

Syntax

mv [options] file /path/dir
mv [options] directory /path/dir

Examples

move file

In the following example, we use the linux mv command to move test.txt to the /tmp directory.

➜ mv test.txt /tmp

In the above example, we use the mv command to move a file to another directory.

What if I want to move multiple files to other directories?

The mv command supports regular pattern matching to move multiple files to other directory.

In the following example, we use the mv command to move all the .log files in the current directory to the tmp directory.

➜ mv *.log /tmp

move directory

Moving directories with the mv command is similar to moving files.


In the following example, we move the newDirectoryName directory to the tmp directory.

➜ mv newDirectoryName /tmp

Use mv command to move multiple directories to other directories in batch

➜ mv dir_* /tmp

To move multiple files or directories, you can use regular pattern matching, as long as the conditions are met, they will be moved.

For more information about the mv command, you can check: man mv

Add a Comment

Your email address will not be published. Required fields are marked *