How to move multiple files or directories?

QuestionsQuestions8 SkillsProDec, 17 2025
0116

That's a fantastic and very common question! Labby here. The mv command is quite powerful when it comes to handling multiple files or directories.

You can definitely move multiple items (files or directories) at once using the mv command. The syntax is to list all the source items you want to move, followed by the single destination directory.

The general syntax looks like this:

mv [option] source1 source2 source3 ... destination_directory

Here's how it works with examples:

Suppose you have the following files and directories in your current project directory:

  • file_a.txt
  • file_b.txt
  • dir_x/
  • temp_files/ (this is your target destination directory)

Let's imagine you want to move file_a.txt, file_b.txt, and dir_x/ into temp_files/.

Example 1: Moving multiple files to a directory

To move file_a.txt and file_b.txt into temp_files/:

mv file_a.txt file_b.txt temp_files/

After this command, file_a.txt and file_b.txt will no longer be in the current directory; they will be inside temp_files/.

Example 2: Moving multiple directories to a directory

If you had dir_x/ and dir_y/ and wanted to move them both into temp_files/:

mv dir_x/ dir_y/ temp_files/

Example 3: Moving a mix of files and directories to a directory

If you want to move file_a.txt, file_b.txt, and dir_x/ all into temp_files/:

mv file_a.txt file_b.txt dir_x/ temp_files/

Important considerations:

  • The last argument must be a directory: When you list multiple sources, the final argument provided to mv is always treated as the destination directory. If you specify multiple sources and the last argument is not a directory or doesn't exist, mv will usually throw an error.
  • Wildcards (*): You can use wildcards to select multiple files or directories. For instance, mv *.txt temp_files/ would move all files ending with .txt into temp_files/. Be careful with wildcards as they can match more than you intend!
  • Interactivity (-i): If you're concerned about overwriting existing files in the destination, you can use mv -i (interactive mode), which will prompt you before overwriting.
  • Force (-f): To force an overwrite without prompting, use mv -f. (Use with caution!)

Would you like to try moving some of the files and directories you created in the lab (like file1_copy.txt and new_testdir) into a new directory? We can create a temporary directory for practice!

0 Comments

no data
Be the first to share your comment!