- How do I move all files from one folder to another using the command line?
- 12 Answers 12
- Terminal Basics #8: Move Files and Directories (Cut-Paste Operation)
- Moving or cut-paste?
- Moving files
- Moving multiple files
- Moving files with caution
- Move but only update
- Troubleshoot: Target is not a directory
- Moving directories
- Rename files and directories
- Test your knowledge
- Hidden Features! 25 Fun Things You Can Do With DuckDuckGo Search Engine
- What is TTY in Linux?
- Beautifully Monitor CPU Utilization in Linux Terminal With Stress Terminal UI
- How to Access the GRUB Menu in Virtual Machine
- FOSS Weekly #23.28: China’s Linux OS, Linux Exit Codes, Btrfs Origins and More
- Using cp Command in Linux
- Become a Better Linux User
How do I move all files from one folder to another using the command line?
I would like to know how could I move all files from a folder to another folder with a command line. Let’s say I’m in my Downloads folder and there are a 100 files that I would like to move to my Videos folder, without having to write all the files name.
You’re asking about moving files but people showing you how to move not only files but folders as well. Is that OK?
@Hontvári Levente gave an answer a year ago that seems to be by far the best, clean, simple, and it works. So how did it get only 3 votes (as compared to 262 for the answer currently at the top)?
12 Answers 12
Open a terminal and execute this command:
It will move all the files and folders from Downloads folder to Videos folder.
To move all files, but not folders:
If you are interested in moving all files (but not folders) from Downloads folder to Videos folder, use this command
find ~/Downloads/ -type f -print0 | xargs -0 mv -t ~/Videos
To move only files from the Download folders, but not from sub-folders:
If you want to move all files from the Downloads folder, but not any files within folders in the Download folder, use this command:
find ~/Downloads/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ~/Videos
here, -maxdepth option specifies how deep find should try, 1 means, only the directory specified in the find command. You can try using 2 , 3 also to test.
See the Ubuntu find manpage for a detailed explanation
Nb. your -print0 | xargs -0 mv -t ~/Videos can be more efficiently done with -exec mv -t ~/Videos \ \+ 🙂\>
for hidden files trick folder/.* (note the dot) works — it says some busy errors, but the move still happens => source: askubuntu.com/a/399632
It will move all the files including subfolders in the directory you want to mv . If you want to cp (copy) or rm (remove) you will need the -r (recursive) option to include subfolders.
@MarkDoliner, yes, mv doesn’t need recursive option to include subfolders. One can use it also for renaming.
If you want to move dot (hidden) files too, then set the dotglob shell option.
shopt -s dotglob mv ~/Downloads/* ~/Videos
This leaves the shell option set.
For one time dotglob use, run the commands in a subshell:
(shopt -s dotglob; mv ~/Downloads/* ~/Videos)
A note for myself: The last option (shopt -s dotglob; mv ~/Downloads/* ~/Videos) only moves (cuts) the contents (including the hidden files). In this case, both the origin and destination folders must exist already. At the end, the origin directory becomes empty.
It works only under certain conditions (namely, if length of files is less than getconf ARG_MAX bytes)
Yes. On Ubuntu 20.04 getconf ARG_MAX is 2MiB. Therefore this method works if there are less than about 250,000 files in the directory.
It’s possible by using rsync , for example:
rsync -vau --remove-source-files src/ dst/
-v , —verbose : Increase verbosity.
-a , —archive : Archive mode; equals -rlptgoD (no -H , -A , -X ).
-u , —update : Skip files that are newer on the receiver.
—remove-source-files This tells rsync to remove from the sending side the files (meaning non-directories) that are a part of the transfer and have been successfully duplicated on the receiving side.
If you’ve root privileges, prefix with sudo to override potential permission issues.
WARNING! the —delete-after option as noted doesn’t work the way you might expect. It doesn’t delete the source files after successful copy. IT DELETES ALL THE REMAINING/OTHER FILES IN THE DESTINATION. (as @kenorb noted. but I didn’t read carefully enough! DOH)
To move a directory with or without content to its new name just like how you would use the mv command to rename a file:
- -T treats the destination as a normal file
- dir1 is the original name of the directory
- dir2 is the new name of the directory
NB: dir2 doesn’t have to exist.
I hope this saves someone a lot of time, as a noob, before this, I would create a directory with the new name and then move the contents of the directory to the directory created earlier.
Use for subdirectories
This command is useful when many files have been saved in a subfolder of the target directory i.e. Downloads/mp4 . In this example, running mv -T Downloads/mp4 Videos will result in mp4 subfolder being removed and all files contained inside are moved to Videos folder.
Terminal Basics #8: Move Files and Directories (Cut-Paste Operation)
In the eighth chapter of the Terminal Basics series, learn about moving files and directories using the mv command in Linux.
Cut, copy and paste are part of everyday computing life. In the previous chapter, you learned about copying files and folders (directories) in the terminal. In this part of the Terminal Basics series, you’ll learn about the cut-paste operation (moving) in the Linux terminal.
Moving or cut-paste?
Alright! Cut-paste is not the correct technical term here. It is called moving files (and folders). Since you are new to the command line, you may find the term ‘moving’ confusing. When you copy a file to another location using the cp command, the source file remains in the same location. When you move a file to another location using the mv command, the source file no longer remains in the origin location. This is the same cut-paste operation (Ctrl+X and Ctrl+V) you do in a graphical file explorer.
Basically, moving files in the command line can be thought same as cut-paste in a graphical environment.
Moving files
Linux has a dedicated mv command (short for move) for moving files and directories to other locations. And using the mv command is quite simple:
mv source_file destination_directory
The role of path comes to play here as well. You can use either the absolute or relative path. Whichever suits your need. Let’s see this with an example. You should practice along with it by replicating the example scenarios on your system. This is the directory structure in the example:
[email protected]:~/moving_files$ tree . ├── dir1 │ ├── file_2 │ └── file_3 ├── dir2 │ └── passwd ├── dir3 ├── file_1 ├── file_2 ├── file_3 ├── file_4 ├── passwd └── services 3 directories, 9 files
Moving multiple files
mv file1 file2 fileN destination_directory
mv file_2 file_3 file_4 dir3
Moving files with caution
If the destination already has files with the same name, the destination files will be replaced immediately. At times, you won’t want that. Like the cp command, the mv command also has an interactive mode with option -i . And the purpose is the same. Ask for confirmation before replacing the files at the destination.
[email protected]:~/moving_files$ mv -i file_3 dir1 mv: overwrite 'dir1/file_3'?
You can press N to deny replacement and Y or Enter to replace the destination file.
Move but only update
The mv command comes with some special options. One of them is the update option -u . With this, the destination file will only be replaced if the file being moved is newer than it.
mv -u file_name destination_directory
[email protected]:~/moving_files$ ls -l file_2 file_3 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_2 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:06 file_3
In the destination directory dir1, file_2 was last modified at 10:37 and file_3 was modified at 10:39.
[email protected]:~/moving_files$ ls -l dir1 total 0 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:37 file_2 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_3
In other words, in the destination directory, the file_2 is older and file_3 is newer than the ones being moved. It also means that file_3 won’t me moved while as file_2 will be updated. You can verify it with the timestamps of the files in the destination directory after running the mv command.
[email protected]:~/moving_files$ mv -u file_2 file_3 dir1 [email protected]:~/moving_files$ ls -l dir1 total 0 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_2 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_3 [email protected]:~/moving_files$ date Tue Apr 4 10:41:16 AM IST 2023 [email protected]:~/moving_files$
As you can see, the move command was executed at 10:41 and only the timestamp of file_2 has been changed.
You can also use the backup option -b . If the destination file is being replaced, it will automatically create a backup with the filename~ pattern.
Troubleshoot: Target is not a directory
If you are moving multiple files, the last argument must be a directory. Otherwise, you’ll encounter this error:
target is not a directory
Here, I create a file which is named dir . The name sounds like a directory, but it is a file. And when I try to move multiple files to it, the obvious error is there: But what if you move a single file to another file? In that case, the target file is replaced by the source file’s content while the source file is renamed as the target file. More on this in later sections.
Moving directories
So far, you have seen everything about moving files. How about moving directories? The cp and rm commands used recusrive option -r to copy and delete folders respectively. However, there is no such requirement for the mv command. You can use the mv command as it is for moving directories.
Here’s an example where I move the dir2 directory to dir3 . And as you can see, dir2 along with its content is moved to dir3 . You can move multiple directories the same way.
Rename files and directories
mv filename new_name_in_same_or_new_location
Let’s say you want to rename a file in the same location. Here’s an example where I rename file_1 to file_one in the same directory. You can also move and rename the files. You just have to provide the directory path and the file name of the destination. Here, I rename services file to my_services while moving it to dir3 .
[email protected]:~/moving_files$ ls dir dir1 dir3 file_2 file_3 file_one passwd services [email protected]:~/moving_files$ mv services dir3/my_services [email protected]:~/moving_files$ ls dir3 dir2 my_services
You cannot rename multiple files directly with mv command. You have to combine it with other commands like find etc.
Test your knowledge
Time to practice what you just learned. Create a new folder to practice the exercise. In here, create a directory structure like this:
. ├── dir1 ├── dir2 │ ├── dir21 │ ├── dir22 │ └── dir23 └── dir3
Copy the file /etc/passwd to the current directory. Now rename it secrets . Make three new files named file_1 , file_2 and file_3 . Move all the files to dir22 . Now move the dir22 directory to dir3 . Delete all contents of dir2 now. In the penultimate chapter of the Terminal Basics series, you’ll learn about editing files in the terminal. Stay tuned.
Hidden Features! 25 Fun Things You Can Do With DuckDuckGo Search Engine
What is TTY in Linux?
Beautifully Monitor CPU Utilization in Linux Terminal With Stress Terminal UI
How to Access the GRUB Menu in Virtual Machine
FOSS Weekly #23.28: China’s Linux OS, Linux Exit Codes, Btrfs Origins and More
Using cp Command in Linux
Become a Better Linux User
With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world