Linux move folder with subfolders

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?

6 Answers 6

From the man page:

It’s just mv srcdir/* targetdir/ .

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

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

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

  • -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.

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.

If you simply need to answer «y» to all the overwrite prompts, try this:

In linux shell, many commands accept multiple parameters and therefore could be used with wild cards. So, for example if you want to move all files from folder A to folder B, you write:

If you want to move all files with a certain «look» to it, you could do like this:

Which copies all files that are blablabla.txt to folder B

Star (*) can substitute any number of characters or letters while ? can substitute one. For example if you have many files in the shape file_number.ext and you want to move only the ones that have two digit numbers, you could use a command like this:

Or more complicated examples:

For files that look like fi _ .e

Unlike many commands in shell that require -R to (for example) copy or remove subfolders, mv does that itself.

Remember that mv overwrites without asking (unless the files being overwritten are read only or you don’t have permission) so make sure you don’t lose anything in the process.

Читайте также:  Update windows activation key

For your future information, if you have subfolders that you want to copy, you could use the -R option, saying you want to do the command recursively. So it would look something like this:

By the way, all I said works with rm (remove, delete) and cp (copy) too and beware, because once you delete, there is no turning back! Avoid commands like rm * -R unless you are sure what you are doing.

Источник

How do I move files out of nested subdirectories into another folder in ubuntu? (Trying to strip off many subfolders)

How do I move files and not directories into another folder/parent folder?

I have a folder structure that is extremely ugly, with some .mp3 files buried 6 levels deep in a sub-folder.

I want to end up with all of the files (mostly .mp3 but not all) in one directory, with no subdirectories at all, using Ubuntu.

5 Answers 5

There is a great answer in the askubuntu-QA.

To do so, 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:

But, If you are interested to move all files (but not folders) from Downloads folder to Videos folder, use this command

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:

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.

Solution

The command will find all regular files under /src/dir (including all subdirectories) and move them to the /dst/dir by use of the command mv . Just replace the directories by yours. Files with the same names will be renamed automatically.

Selecting files to move

If you want to move just MP3 files, add -iname «*.mp3» option to the find command after -type f .

Comparison to the reply by c0dev

Only the second command in the c0dev’s reply answers the question. Below is how does it compare to this reply. The points 3. and 4. can be resolved in the other reply the same way as here.

  1. Except mv the solution with -exec + does not need to call an additional command like xargs or parallel and hand over the file names twice.
  2. The other reply will silently overwrite files which have the same name. Here the files will be automatically renamed thanks to the option —backup=numbered . Unfortunately these backups with suffix like

will be hidden in most of the file manages by default. Unfortunately mv does not allow changing of the suffix but it could be easily post-processed by additional commands. This is a GNU extension.

  • Contrary to -print0 -exec command <> + is a part of IEEE Std 1003.1 (POSIX), ISO/IEC 9945 and The Single UNIX Specification standards. Thus it should be more portable. See IEEE Std 1003.1, 2004 Edition, IEEE Std 1003.1, 2013 Edition and 0000243: Add -print0 to «find». But anyway the required -t switch of mv is a GNU extension so the whole command is not portable between POSIX systems.
  • Note: In the case find would be able to produce paths starting with — (I do not know of any such implementation of find at the moment.) the <> should be preceded by the end-of-options indicator: — .

    Источник

    How to move all files and folders via mv command [duplicate]

    How can I move all files and folders from one directory to another via mv command?

    7 Answers 7

    (D) to include dot-files.

    This works for me in Bash (I think this depends on your shell quite a bit. )

    This works for me in Bash 4.2.46, it moves all files and folders including hidden files and folders to another directory

    Notice that .[^.]* means all hidden files except . and ..

    I’d say it’s a bit boring, but really bullet-proof (GNU) way is:

    cd /SourceDir && find ./ -maxdepth 1 -mindepth 1 -exec mv -t /Target/Dir <> +

    P. S. Now you can possibly see why lots of people do prefer Midnight Commander, though.

    If you only want to do a cut and paste-like action there is a simple way that worked for me:

    It will move the folder named dir_source located in /media to the directory $HOME/Documents/

    yet another way just for the heck of it (because I love convoluted ways to do things I guess)

    the -Q and the -A are not POSIX, however the -A is fairly prevalent, and to not use the -Q you need to change the IFS (which then means you don’t need the eval but need to quote the variable)

    Not the answer you’re looking for? Browse other questions tagged linux command rename or ask your own question.

    Linked

    Hot Network Questions

    site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

    By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

    Источник

    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.

    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

    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:

    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

    /Videos/») won’t move dot files.

    /Videos can be more efficiently done with -exec mv -t

    /Videos/ does not work for hidden files

    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.

    / in the prefix of the folder names doesn’t always work (doesn’t work on bash and git atleast)

    For the simple case:

    If you want to move dot (hidden) files too, then set the dotglob shell option.

    This leaves the shell option set.

    For one time dotglob use, run the commands in a subshell:

    /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’s possible by using rsync , for example:

    -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.

    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.

    Источник

    Linux Move Folder

    By Priya Pedamkar

    Introduction to Linux Move Folder

    The move command in Linux is a command-line utility that allows one to move one or more files or directories from the source to destination. The Move command can move single or multiple files or directories from source but the destination should be only one path or file. It also gives a prompt before we overwrite and it also gives an option to move only the new files in the destination. In this Topic, we are going to learn about Linux Move Folder and we will show you how to use the Linux Move command with its options. Each option is explained briefly with syntax and screenshots for better understanding.

    Syntax of Linux Move Folder:

    Web development, programming languages, Software testing & others

    Below are different syntax which can be used as per the requirement to move folders from source to destination in Linux.

    mv source target

    mv folder1 folder2 target

    mv file folder target

    mv -options source target

    The Move ‘mv’ command can move single or multiple files or directories from source but the destination should be only one path or file. It also gives a prompt before we overwrite and it also gives an option to move only the new files in the destination.

    How to move a folder in Linux?

    Below are the options that can be used with mv command in Linux.

    Options Descriptions
    –backup[=CONTROL] Helps to make a backup of each existing destination file in the server
    -b It is similar to –backup option but it does not keep an argument
    -f, –force It will not prompt any option before overwriting
    -i, –interactive It will prompt options before we overwrite.
    -n, –no-clobber It does not overwrite an existing file
    –strip-trailing-slashes This option will remove any trailing slashes from each of the SOURCE argument
    -S, –suffix=SUFFIX It overrides the general backup suffix
    -t, –target-directory=DIRECTORY moves all the SOURCE arguments into the target DIRECTORY
    -T, –no-target-directory It treats the target DESTINATION as a casual file
    -u, –update It will allow us to move only when the SOURCE file or directory is newer than the target-directory or file or if the destination file is not present.
    -v, –verbose It explains what is going to be done
    –help It will display all the options using the help option and will exit
    –version It displays the output version information and exits

    Examples of Linux Move Folders

    Here are the options listed below on how to use move commands when options are passed through it.

    Example #1 – No Option

    When the move command is used without passing any options, then it will directly overwrite the folder. Syntax and example are given below for better understanding.

    Источник

    Читайте также:  Destroy windows spying dws win 10
    Оцените статью