Move files and subdirectories in linux

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

Good point. If you are using bash, then you can run shopt -s dotglob and then «*» will match hidden files, too.

What happens if there are folders and files with the same name in the destination folder? Are they overwritten?

. it seems folders with the same name are not overwritten. mv: cannot move ‘/a/js’ to ‘/b/js’: Directory not empty

I know I accepted the first answer many years ago, and it’s stupid for me now to change it. But actually, I’ve been always using this method.

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

$ mv source/* /destination/folder/here 

When I try I get mv: overwrite ‘destination/.’? mv: overwrite ‘destination/..’? , but adding -n to mv stops it from trying to overwrite

@Putnik — that’s a good gotcha! what os/distro ? ( I was working on OSX when I was messing around with this. )

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:

$mv /media/dir_source $HOME/Documents/ 

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)

cd /source for f in $(\ls -QA); do eval mv $f /destination/$f; done 

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)

IFS=" " && for f in $(ls -A); do mv "$f" /destination/"$f"; done 

Источник

Читайте также:  Linux консоль узнать версию ос

mv Command in Linux: 7 Essential Examples

mv command in Linux is used for moving and renaming files and directories. In this tutorial, you’ll learn some of the essential usages of the mv command.

mv is one of the must known commands in Linux. mv stands for move and is essentially used for moving files or directories from one location to another.

The syntax is similar to the cp command in Linux however there is one fundamental difference between these two commands.

You can think of the cp command as a copy-paste operation. Whereas the mv command can be equated with the cut-paste operation.

Which means that when you use the mv command on a file or directory, the file or directory is moved to a new place and the source file/directory doesn’t exist anymore. That’s what a cut-paste operation, isn’t it?

mv command can also be used for renaming a file. Using mv command is fairly simple and if you learn a few options, it will become even better.

7 practical examples of the mv command

Let’s see some of the useful examples of the mv command.

1. How to move a file to different directory

The first and the simplest example is to move a file. To do that, you just have to specify the source file and the destination directory or file.

mv source_file target_directory

This command will move the source_file and put it in the target_directory.

2. How to move multiple files

If you want to move multiple files at once, just provide all the files to the move command followed by the destination directory.

mv file1.txt file.2.txt file3.txt target_directory

You can also use glob to move multiple files matching a pattern.

For example in the above example, instead of providing all the files individually, you can also use the glob that matches all the files with the extension .txt and moves them to the target directory.

3. How to rename a file

One essential use of mv command is in renaming of files. If you use mv command and specify a file name in the destination, the source file will be renamed to the target_file.

mv source_file target_directory/target_file

In the above example, if the target_fille doesn’t exist in the target_directory, it will create the target_file.

However, if the target_file already exists, it will overwrite it without asking. Which means the content of the existing target file will be changed with the content of the source file.

Читайте также:  Linux intel power save

I’ll show you how to deal with overwriting of files with mv command later in this tutorial.

You are not obliged to provide a target directory. If you don’t specify the target directory, the file will be renamed and kept in the same directory.

Keep in mind: By default, mv command overwrites if the target file already exists. This behavior can be changed with -n or -i option, explained later.

4. How to move a directory in Linux with mv command

You can use mv command to move directories as well. The command is the same as what we saw in moving files.

mv source_directory target_directory

In the above example, if the target_directory exists, the entire source_directory will be moved inside the target_directory. Which means that the source_directory will become a sub-directory of the target_directory.

5. How to rename a directory

Renaming a directory is the same as moving a directory. The only difference is that the target directory must not already exist. Otherwise, the entire directory will be moved inside it as we saw in the previous directory.

mv source_directory path_to_non_existing_directory

6. How to deal with overwriting a file while moving

If you are moving a file and there is already a file with the same name, the contents of the existing file will be overwritten immediately.

This may not be ideal in all the situations. You have a few options to deal with the overwrite scenario.

To prevent overwriting existing files, you can use the -n option. This way, mv won’t overwrite existing files.

mv -n source_file target_directory

But maybe you want to overwrite some files. You can use the interactive option -i and it will ask you if you want to overwrite existing file(s).

mv -i source_file target_directory mv: overwrite 'target_directory/source_file'?

You can enter y for overwriting the existing file or n for not overwriting it.

There is also an option for making automatic backups. If you use -b option with the mv command, it will overwrite the existing files but before that, it will create a backup of the overwritten files.

mv -b file.txt target_dir/file.txt ls target_dir file.txt file.txt~

By default, the backup of the file ends with ~. You can change it by using the -S option and specifying the suffix:

mv -S .back -b file.txt target_dir/file.txt ls target_dir file.txt file.txt.back

You can also use the update option -u when dealing with overwriting. With the -u option, source files will only be moved to the new location if the source file is newer than the existing file or if it doesn’t exist in the target directory.

  • -i : Confirm before overwriting
  • -n : No overwriting
  • -b : Overwriting with backup
  • -u : Overwrite if the target file is old or doesn’t exist
Читайте также:  Linux manjaro file manager

7. How to forcefully move the files

If the target file is write protected, you’ll be asked to confirm before overwriting the target file.

mv file1.txt target mv: replace 'target/file1.txt', overriding mode 0444 (r--r--r--)? y

To avoid this prompt and overwrite the file straightaway, you can use the force option -f.

If you do not know what’s write protection, please read about file permissions in Linux.

You can further learn about mv command by browsing its man page. However, you are more likely to use only these mv commands examples I showed here. FYI, you may also use rename command for renaming multiple files at once.

I hope you like this article. If you have questions or suggestions, please feel free to ask in the comment section below.

Источник

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

5 Answers 5

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

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.

Источник

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