Move all files and folder linux

How to Move Files Using Linux Commands or File Managers

Learn how to move files with Linux commands in this tutorial from our archives.

There are certain tasks that are done so often, users take for granted just how simple they are. But then, you migrate to a new platform and those same simple tasks begin to require a small portion of your brain’s power to complete. One such task is moving files from one location to another. Sure, it’s most often considered one of the more rudimentary actions to be done on a computer. When you move to the Linux platform, however, you may find yourself asking “Now, how do I move files?”

If you’re familiar with Linux, you know there are always many routes to the same success. Moving files is no exception. You can opt for the power of the command line or the simplicity of the GUI – either way, you will get those files moved.

Let’s examine just how you can move those files about. First we’ll examine the command line.

Command line moving

One of the issues so many users, new to Linux, face is the idea of having to use the command line. It can be somewhat daunting at first. Although modern Linux interfaces can help to ensure you rarely have to use this “old school” tool, there is a great deal of power you would be missing if you ignored it all together. The command for moving files is a perfect illustration of this.

The command to move files is mv . It’s very simple and one of the first commands you will learn on the platform. Instead of just listing out the syntax and the usual switches for the command – and then allowing you to do the rest – let’s walk through how you can make use of this tool.

The mv command does one thing – it moves a file from one location to another. This can be somewhat misleading, because mv is also used to rename files. How? Simple. Here’s an example. Say you have the file testfile in /home/jack/ and you want to rename it to testfile2 (while keeping it in the same location). To do this, you would use the mv command like so:

mv /home/jack/testfile /home/jack/testfile2

or, if you’re already within /home/jack:

mv testfile testfile2

The above commands would move /home/jack/testfile to /home/jack/testfile2 – effectively renaming the file. But what if you simply wanted to move the file? Say you want to keep your home directory (in this case /home/jack) free from stray files. You could move that testfile into /home/jack/Documents with the command:

mv /home/jack/testfile /home/jack/Documents/

With the above command, you have relocated the file into a new location, while retaining the original file name.

What if you have a number of files you want to move? Luckily, you don’t have to issue the mv command for every file. You can use wildcards to help you out. Here’s an example:

You have a number of .mp3 files in your

/ – is an easy way to represent your home directory – in our earlier example, that would be /home/jack/) and you want them in

/Music. You could quickly move them with a single command, like so:

That command would move every file that ended in .mp3 from the Downloads directory, and move them into the Music directory.

Should you want to move a file into the parent directory of the current working directory, there’s an easy way to do that. Say you have the file testfile located in

/Downloads and you want it in your home directory. If you are currently in the

Читайте также:  Vnc client windows 10 что это

/Downloads directory, you can move it up one folder (to

The “../” means to move the folder up one level. If you’re buried deeper, say

/Downloads/today/, you can still easily move that file with:

Just remember, each “../” represents one level up.

As you can see, moving files from the command line, isn’t difficult at all.

There are a lot of GUIs available for the Linux platform. On top of that, there are a lot of file managers you can use. The most popular file managers are Nautilus (GNOME) and Dolphin (KDE). Both are very powerful and flexible. I want to illustrate how files are moved using the Nautilus file manager (on the Ubuntu 13.10 distribution, with Unity as the interface).

Nautilus has probably the most efficient means of moving files about. Here’s how it’s done:

Open up the Nautilus file manager.

Locate the file you want to move and right-click said file.

From the pop-up menu (Figure 1) select the “Move To” option.

When the Select Destination window opens, navigate to the new location for the file.

Once you’ve located the destination folder, click Select.

This context menu also allows you to copy the file to a new location, move the file to the Trash, and more.

If you’re more of a drag and drop kind of person, fear not – Nautilus is ready to serve. Let’s say you have a file in your home directory and you want to drag it to Documents. By default, Nautilus will have a few bookmarks in the left pane of the window. You can drag the file into the Document bookmark without having to open a second Nautilus window. Simply click, hold, and drag the file from the main viewing pane to the Documents bookmark.

If, however, the destination for that file is not listed in your bookmarks (or doesn’t appear in the current main viewing pane), you’ll need to open up a second Nautilus window. Side by side, you can then drag the file from the source folder in the original window to the the destination folder in the second window.

If you need to move multiple files, you’re still in luck. Similar to nearly every modern user interface, you can do multi-select of files by holding down the Ctrl button as you click each file. After you have selected each file (Figure 2), you can either right-click one of the selected files and the choose the Move To option, or just drag and drop them into a new location.

The selected files (in this case, folders) will each be highlighted.

Moving files on the Linux desktop is incredibly easy. Either with the command line or your desktop of choice, you have numerous routes to success – all of which are user-friendly and quick to master.

Источник

How to move all files in current folder to subfolder?

I am at the path:

And I create a sub folder:

Now I want to move all files and folders/sub-folders in the downloads folder to the sub-folder.

how can I do this?

But move doesn’t take the -R switch it seems.

6 Answers 6

should do the trick. If it doesn’t work, run shopt -s extglob first.

To also move hidden files/directories (that beginning with a dot), run also shopt -s dotglob first.
So, to sum up:

(it is always better to unset dotglob to avoid bad surprises).

I found something like this but is a bit simpler to understand, and it might work well for you too:

Adding an explanation to the above solution:

Explained by step:

  • ls will list the files in current directory
  • grep -v new will return piped to that is not match new
  • xargs mv -t new will move the files piped to it from grep -v to the target directory

Just use mv * subdir1 and ignore the warning.

You can just use mv * subdir1 . You will see a warning message relating to trying to move subdir1 into itself, like this:

But it will move all of the other files and directories to subdir1 correctly.

Читайте также:  Перестали работать все usb порты windows

Simple idea. Assuming you are in /myuser, rename downloads to new, create a new downloads directory then move new into it.

If you want to move all the files from a folder to one of its subfolders you can use the following command:

It will find all the files and then move them to your subfolder.

@waltinator: added -type d -name ‘new’ -prune to prevent traversal of /myuser/downloads/new .

You can try this alternative process –– remain in the path

but, instead of first creating the /myuser/downloads/new/ directory, instead create a folder in the /myuser/ directory, with the command mkdir ../new , then move all the files in downloads to new , and finally move new into downloads . You can do this in one line, while in the /myuser/downloads/ path, with the command:

In this case, you don’t have to worry about any sort of «filtering» of files/folders, since new is on the same level of the path as downloads , so you can just move everything in downloads to new , and then move new into downloads`.

However, if you already have the subfolder new created and don’t want to create another one, not to worry –– just change the mkdir command on the left-hand side of the first && in the command shown above to an mv command, pushing new up in the path; in other words, while you’re still in /myuser/downloads/ , you can change mkdir ../new to mv new .. . Then the subfolder new [in the path /myuser/downloads/new/ ] gets pushed up to /myuser/new/ , at the same level as /myuser/downloads/ , and then you can run the rest of the command as it is shown above. All together, we have, starting from the path /myuser/downloads/ :

and, since you wanted to «move all files and folders/sub-folders in the downloads folder to the sub-folder [ new ]», you’re done! If you had wanted to move only files (or only folders or [insert more granular object movement]), then you’d have to use other commands that can «filter» objects, such as grep . The commands written above are sufficient for your purposes, though.

Источник

Move all files with a certain extension from multiple subdirectories into one directory

I have a bunch of .zip files in several directories:

How would I do move them all to a common base folder?

7 Answers 7

Go to the toplevel directory of the tree containing the zip files ( cd … ), then run

This works out of the box in zsh. If your shell is bash, you’ll need to run shopt -s globstar first (you can and should put this command in your

/.bashrc ). If your shell is ksh, you’ll need to run set -o globstar first (put it in your

Alternatively, use find , which works everywhere with no special preparation but is more complicated:

If you want to remove empty directories afterwards, in zsh:

and repeat as long as there are empty directories to remove. Alternatively, in any shell

If you only want to move the .zip files, you could do something like

If you’re using bash version 4 or higher or zsh you can also use recursive globbing:

This will move ONLY the files and not their relative paths, so collisions might occur.

This one is safe when moving data and error free which supported most of all distro regardless versions. This command will scan subdirectories and then move or copy to your new destination directory.

  • Run this command at the main directory that contains sub-directories with the files you wish to move.
  • Where you can change *.flac to anything like *.zip in your case. Or *.doc just any extension works.
  • and mv is the command to move files, or you can use cp to copy data instead of moving.
  • ./flac/ is the destination directory that I want to move all FLAC files to. You can also give it a full path like /home/myid/flac/

Full example. (in this case, there are many subdirectories with music artist name at /home/myid/Music/ and then FLAC files are all over different subdirectories level. Since I don’t need artist folders but want to have all FLAC files into one directory at /home/myid/Music/flac/

Читайте также:  Mac os изменить масштаб экрана

Источник

How do you move all files (including hidden) from one directory to another?

How do I move all files in a directory (including the hidden ones) to another directory?

For example, if I have a folder «Foo» with the files «.hidden» and «notHidden» inside, how do I move both files to a directory named «Bar»? The following does not work, as the «.hidden» file stays in «Foo».

Try it yourself.

12 Answers 12

(Leave out the (N) if you know the directory is not empty.)

Ksh93

If you know the directory is not empty:

Standard (POSIX) sh

If you’re willing to let the mv command return an error status even though it succeeded, it’s a lot simpler:

GNU find and GNU mv

Standard find

If you don’t mind changing to the source directory:

Here’s more detail about controlling whether dot files are matched in bash, ksh93 and zsh.

There’s also the more flexible GLOBIGNORE variable, which you can set to a colon-separated list of wildcard patterns to ignore. If unset (the default setting), the shell behaves as if the value was empty if dotglob is set, and as if the value was .* if the option is unset. See Filename Expansion in the manual. The pervasive directories . and .. are always omitted, unless the . is matched explicitly by the pattern.

Ksh93

Set the FIGNORE variable. If unset (the default setting), the shell behaves as if the value was .* . To ignore . and .. , they must be matched explicitly (the manual in ksh 93s+ 2008-01-31 states that . and .. are always ignored, but this does not correctly describe the actual behavior).

You can include dot files in a pattern by matching them explicitly.

To have the expansion come out empty if the directory is empty, use the N pattern matching option:

. and .. are never matched, even if the pattern matches the leading . explicitly.

You can include dot files in a specific pattern with the D glob qualifier.

Add the N glob qualifier to make the expansion come out empty in an empty directory: *(DN) .

Note: you may get filename expansion results in different orders (e.g., none followed by .one followed by ..two ) based on your settings of the LC_COLLATE , LC_ALL , and LANG variables.

From man bash

dotglob If set, bash includes filenames beginning with a ‘.’ in the results of pathname expansion.

A simple way to do this in bash is

But this will also move directories.

If you want to move all files including hidden but don’t want to move any directory you can use a for loop and test.

One way is to use find :

The -type f restricts the find command to finding files. You should investigate the -type , -maxdepth , and -mindepth options of find to customize your command to account for subdirectories. Find has a lengthy but very helpful manual page.

Try the copy command cp :

cp -r means copy recursive, so all folders and files will be copied.

You can use the remove command rm to remove a folder:

I find that this works well for bash and there’s no need to change shell options

So as G-man stated, my original answer is not posix compliant and is pretty much the same as ndemou’s answer above with one change, which is to use brace expansion to create lists of strings which are then acted on. This just means you don’t need to cd into the source directory. Not that big a change really, but it is different.

example: let’s say you have the following layout already.

The original question only mentioned hidden files with a single period, but let’s say there are some with two or more periods at the start of the name. You can just add in an additional expression into the braces. We can then execute

This gets expanded to the following:

You should now see all files located in destdir:

You can do some pretty cool things with braces in bash with even more additions in 4.x. Check out bash-hackers for some nifty examples.

Источник

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