Moving files and folders in 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.

Читайте также:  Pantum m6500 linux driver

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

/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 including hidden files into parent directory via *

Its must be a popular question but I could not find an answer.

How to move all files via * including hidden files as well to parent directory like this:

This will move all files to parent directory like expected but will not move hidden files. How to do that?

7 Answers 7

You can find a comprehensive set of solutions on this in UNIX & Linux’s answer to How do you move all files (including hidden) from one directory to another?. It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.

Читайте также:  Как открыть disk management windows

You can use these two commands together:

Which expands to:

(example: echo a<.,>b expands to a.b ab )

Note this will show a couple of warnings:

Just ignore them: this happens because /path/subfolder/<.,>* also expands to /path/subfolder/. and /path/subfolder/.. , which are the directory and the parent directory (See What do “.” and “..” mean when in a folder?).

If you want to just copy, you can use a mere:

This will copy all files, both normal and hidden ones, since /path/subfolder/. expands to «everything from this directory» (Source: How to copy with cp to include hidden files and hidden directories and their contents?)

I think this is the most elegant, as it also does not try to move .. :

This will move all files to parent directory like expected but will not move hidden files. How to do that?

You could turn on dotglob :

In order to turn off dotglob , you’d need to say:

By using the find command in conjunction with the mv command, you can prevent the mv command from trying to move directories (e.g. .. and . ) and subdirectories. Here’s one option:

There are problems with some of the other answers provided. For example, each of the following will try to move subdirectories from the source path:

Also, 2) includes the . and .. files and 3) misses files like ..foobar, . barfoo, etc.

You could use, mv /source/path/<.[!.]. >* /destination/path , which would include the files missed by 3), but it would still try to move subdirectories. Using the find command with the mv command as I describe above eliminates all these problems.

Alternative simpler solution is to use rsync utility:

Note: Above command will show what is going to be changed. To execute the actual changes, remove —dry-run .

The advantage is that the original folder ( subfolder ) would be removed as well as part of the command, and when using mv examples here you still need to clean up your folders, not to mention additional headache to cover hidden and non-hidden files in one single pattern.

In addition rsync provides support of copying/moving files between remotes and it would make sure that files are copied exactly as they originally were ( -a ).

The used -u parameter would skip existing newer files, -r recurse into directories and -v would increase verbosity.

Источник

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

Читайте также:  Защищенный ноутбук astra linux

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

Источник

Move files and folders recursively on Linux

How do I move the contents of /public-back/templates recursively with permissions into /public/templates ?

8 Answers 8

Unless I am misunderstanding the question, this would work:

Also, unless you have a huge list of files, adding -i will ask before it overwrites anything, which add some safety when using wildcards like * .

The man page for cp states:

When moving items from my thumb drive to my OSMC system, I’ve found the following very useful:

Explanation on how it works below.

BTW, Don’t forget to add a backslash before any spaces in the source or destination directory names (see above).

Effectively, you are finding all files and all folders and moving them one by one (or if a directory gets found first, you are moving that directory and the contents in it). This starts a new process for each move and is very inefficient. Only use this when the regular commands fail.

So create hard links in the destination directory and remove the source dir. ‘mv’ simply will not work in your case, and in general works only when source dir and dest have no common subtrees.

Note that I’m assuming that the word ‘move’ in the question means that the source dir should be gone after the operation.

mv doesn’t seem to do this. But you can use this little trick, works like a charm:

and preserves permissions and all.

Note: none of the above worked for me, that’s why this workaround.

It is possible to move instead of copy with rsync by using the —remove-source-files argument. This will preserve properties such as permissions and dates modified. It has the added benefit of checking whether files don’t need to be moved to the target directory (i.e., if a newer file with the same name already exists there).

Of course you can also copy the files and remove the original directory.

This is my recommended parameters for rsync but there are other arguments for preserving various properties or handling links and compression/encryption of large files. This command also supports copying to remote file systems via ssh tunnels.

As noted above, on the same filesystem mv is faster than cp . By example, the following preserves timestamps and ownerships, and recursively moves directories and files including hidden files and directories.

Initial conditions:

  • note ownership of dir02/* is root:victoria

Move those files, directories:

This appears to preserve:

  • timestamps
  • recursively moves files, directories (including hidden files, directories)
  • preserves ownerships

After the move:

None of the answers in this thread fit my usecase, so I came up with one on my own as a shell script.

At the heart of it is this function:

Which you could invoke like so (for the OP usecase):

If source is a file or a directory for which the destination does not exist yet, it simply moves it with mv -u . If source is a directory for which the destination already exists, it iterates through its contents and recursively performs the same check followed by move|recurse with each of its members.

I used -u because I only needed old files freshened and newer files untouched, but you could replace it with -f for an unconditional move or -i for an interactive one. Or not change anything and simply rm -rf your source after the script is done moving things.

[A slightly modified repost from Server Fault]

Источник

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