How to move all files in linux

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.

Читайте также:  Amd radeon vega 3 driver linux

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.

Источник

Move All Files Including Hidden Files Into Parent Directory

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

Hidden files, also called dotfiles, are files whose name starts with the dot (.) character. Typically, these files are used to store configurations — for example, the /home/user/.config file might store configuration settings for a user.

In this tutorial, we’ll discuss how to move all the files including hidden files from a directory into its parent directory using the mv and rsync commands.

2. Using mv Command

The mv command is used to move files and directories from one place to another. We can also use it to rename files and directories.

Читайте также:  Настройка журналов в linux

Let’s see a basic example of how to move all files to the parent directory using the mv command:

This will move all the files from /path/subfolder to /path/ except for hidden files and directories.

So, let’s see how we can also move the hidden files and directories:

The above command expands to:

mv /path/subfolder/* mv /path/subfolder/.* /path/

Note that the asterisk (*) means all the files in the subfolder folder, and dot-asterisk (.*) means all the hidden files in the subfolder folder. So, both file types will be copied to the path destination folder.

Also, since we’re including the -f option, any existing files and directories in the destination folder will be overwritten without prompting for confirmation.

Finally, it’s important to note that the original folder – subfolder in this case – will not be removed as part of the mv operation.

3. Using rsync

rsync is a Linux utility to move or copy files from one directory to another either locally or remotely. It also supports moving groups, permissions, links, and devices.

rsync uses a remote-update protocol to move or copy the files. This allows transferring just the differences between two sets of files.

First, let’s use rsync with the –dry-run option to see what files will be moved without performing the actual move operation:

sudo rsync --dry-run path/subfolder/ path/

Lets now see how to move all files including hidden files using rsync:

sudo rsync --remove-source-files /path/subfolder/ /path/

The above command moves all the files from the directory /path/subfolder/ to its parent, /path/.

Note here that rsync moves all the files, including hidden files, because we’ve used / at the end of the path. This denotes that the complete source directory should be synced with the target directory.

The –remove-source-files option will remove the files from the source directory once they are copied to the target directory. This will also remove the original folder, subfolder, as part of the sync operation.

Also, note we use sudo to execute the rsync command with admin privileges. This avoids any permission issues while creating the target directories.

If we don’t use sudo, and there are any permission issues while creating the target directories, the files won’t be copied, but they’ll still be removed from the original directory.

4. Conclusion

To summarize, we’ve discussed a few commands to move all files including hidden files into a parent directory in Linux. First, we saw how to do this using the mv command, and then we saw how to use rsync to achieve the same result.

Источник

How to move (and overwrite) all files from one directory to another?

I know of the mv command to move a file from one place to another, but how do I move all files from one directory into another (that has a bunch of other files), overwriting if the file already exists?

I know this is old question, but what about «rsync -az src/ dest/» it will copy all files which do not exist in dest/ directory from src/, and then just remove destination directory with «rm -rf dest/»

Читайте также:  Extract zip archive linux

6 Answers 6

-f, --force do not prompt before overwriting 

mv -f /a/b /c/d moves b inside d (so I have /c/d/a ) instead of overwriting d with a (I want /c/a ). How can I do such overwrite?

@Xenos you would need to delete the file d before you can create a directory d . That would require a couple of commands, there’s no force overwrite of a directory with a file that I know of. You could script that with just a few lines, but it would be better taken up as a separate question since it deviates from question the OPasked.

It’s just mv srcdir/* targetdir/ .

If there are too many files in srcdir you might want to try something like the following approach:

cd srcdir find -exec mv <> targetdir/ + 

In contrast to \; the final + collects arguments in an xargs like manner instead of executing mv once for every file.

just a note: on directories with a couple of thousands files, you wont be able to use the * since the argument list will be too long for the shell to handle, so you would need to use «find» instead or similar solution

I’ve tried the following command «find . -exec mv <> ../clean-copy/Ressources/img/ +» but got the following error : «find: -exec: no terminating «;» or «+»» — Any idea why ?

@musiKk Thanks for the tip. Although my comment is a question, it’s here because I feel the answer is 99% helping but missing the 1% that would make it perfect.

It’s also possible by using rsync , for example:

rsync -va --delete-after src/ dst/ 
  • -v , —verbose : increase verbosity
  • -a , —archive : archive mode; equals -rlptgoD (no -H,-A,-X )
  • —delete-after : delete files on the receiving side be done after the transfer has completed

If you’ve root privileges, prefix with sudo to override potential permission issues.

yup, just lost all my files. Shame on me for thinking people online know what they are talking about.

For moving and overwriting files, it doesn’t look like there is the -R option (when in doubt check your options by typing [your_cmd] —help . Also, this answer depends on how you want to move your file. Move all files, files & directories, replace files at destination, etc.

When you type in mv —help it returns the description of all options.

For mv, the syntax is mv [option] [file_source] [file_destination]

To move simple files: mv image.jpg folder/image.jpg

To move as folder into destination mv folder home/folder

To move all files in source to destination mv folder/* home/folder/

Use -v if you want to see what is being done: mv -v

Use -i to prompt before overwriting: mv -i

Use -u to update files in destination. It will only move source files newer than the file in the destination, and when it doesn’t exist yet: mv -u

Tie options together like mv -viu , etc.

Источник

Оцените статью
Adblock
detector