How to move folder in linux

Linux command to move a directory

This is a perfectly good question that belongs on StackOverflow. Granted, it is a naive mistake — but that’s what SO is for; it helps people overcome naivety by allowing them to ask questions and learn from answers. See SO Blog and Podcast #53 (Joel says no question is too simple for Stack Overflow. ).

5 Answers 5

You should use mv -if old/* new/ without the trailing * .

This is because it unrolled to

mv -if old/foo old/bar old/baz new/foo new/bar new/baz 

i.e. move everything into new/baz

This is not what you wanted.

reef@localhost:/tmp/experiment$ ls a 11 22 33 reef@localhost:/tmp/experiment$ ls b 22 33 reef@localhost:/tmp/experiment$ ls a b reef@localhost:/tmp/experiment$ mv a/* b reef@localhost:/tmp/experiment$ ls a reef@localhost:/tmp/experiment$ ls b 11 22 33 

It works. What are You trying to achieve? Could You please write a short example of what the input data should look like and what the output data should look like? The truth is I have no idea what You are trying to do 🙂 Help me help You.

note that mv a/* b/ don’t move files .* (file name start with ‘.’) in a/ to b/

$ mkdir -p a/d b && touch a/f a/.f a/d/.f $ mv a/* b/ $ ls -a a/ . .. .f 

If you are copying from an ext2/3/4 file system to a FAT32 file system, and a filename has an invalid character for FAT32 naming conventions, you get this terribly annoying and incorrect as hell error message. How do I know? I wrestled with this bug — yes, it’s a KERNEL BUG — for 6 hours before it dawned on me. I thought it was a shell interpreter error, I thought it was an «mv» error — I tried multiple different shells, everything. Try this experiment: on an ext file system, «touch ‘a:b'» them «mv» it to a FAT32 file system. Try it, you’ll enjoy (hate) the results. The same is true for » (\074 and \076).

Thanks for «man mv» — that’s a real big help, don’t quit your day job.

Might be you got the answer but above answer is not working for me. and finally lots of researching I got the answer. (Issue is due to files-ownership)
and just put sudo before the command and its working. 🙂 Same thing for cp and mv command.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

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

Источник

Linux fundamentals: How to copy, move, and rename files and directories

Two windows with a small pane open

Copying, moving, and renaming files and directories are standard tasks for sysadmins and end users. Depending on your Linux distribution, you can accomplish these operations in various ways.

Читайте также:  Linux get users home directory

The Bash shell is usually the most efficient tool for file management. This article assumes you already have a basic understanding of how to open a Linux terminal and enter commands. (See How to access the Linux terminal if you want a refresher.) Connect to your Linux terminal with your regular user account, and get ready to reorganize.

Change to your home directory and create a new directory named mydir for the exercises. The command to create a new directory is mkdir :

Move files and directories

The mv command moves both directories and files. Check its options and parameters from the —help results below:

$ mv --help Usage: mv [OPTION]. [-T] SOURCE DEST or: mv [OPTION]. SOURCE. DIRECTORY or: mv [OPTION]. -t DIRECTORY SOURCE. Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY. Mandatory arguments to long options are mandatory for short options too. --backup[=CONTROL] make a backup of each existing destination file -b like --backup but does not accept an argument -f, --force do not prompt before overwriting -i, --interactive prompt before overwrite -n, --no-clobber do not overwrite an existing file If you specify more than one of -i, -f, -n, only the final one takes effect. --strip-trailing-slashes remove any trailing slashes from each SOURCE argument -S, --suffix=SUFFIX override the usual backup suffix -t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY -T, --no-target-directory treat DEST as a normal file -u, --update move only when the SOURCE file is newer than the destination file or when the destination file is missing -v, --verbose explain what is being done -Z, --context set SELinux security context of destination file to default type --help display this help and exit --version output version information and exit The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup option or through the VERSION_CONTROL environment variable. Here are the values: none, off never make backups (even if --backup is given) numbered, t make numbered backups existing, nil numbered if numbered backups exist, simple otherwise simple, never always make simple backups

Rename files and directories

You also use the mv command to rename directories and files if the destination doesn’t already exist. If the destination exists, then they’re moved using the syntax mv . Here is an example of moving existing files to existing directories:

$ ls -l total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 17:52 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 17:52 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file2 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file3 $ mv file1 dir1/ $ mv file2 dir2/ $ mv file3 dir3/ $ ls -l total 0 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir1 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir2 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 [mydir]$ ls -lR .: total 0 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir1 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir2 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:53 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 ./dir1: total 0 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1 ./dir2: total 0 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file2 ./dir3: total 0 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file3 ./dir4: total 0 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:35 subdir1 ./dir4/subdir1: total 0 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:35 file4

[ Boost your Bash skills. Download the Bash shell scripting cheat sheet. ]

And you can use the mv command to move directories into other directories:

$ ls -1 dir1 dir2 dir3 dir4 $ mv dir1/ dir2/ $ mv dir2/ dir3/ $ ls -1 dir3 dir4

The dir1 and dir2 directories still exist; you’ve just moved them. See what it looks like for yourself:

$ ls -R dir3 dir4 ./dir3: dir2 file3 ./dir3/dir2: dir1 file2 ./dir3/dir2/dir1: file1 ./dir4: subdir1 ./dir4/subdir1: file4

Copy files and directories

The cp command copies both files and directories. This command has many options, but the basic syntax is simple. Run cp to copy from one place (source) to another (destination). Consider the following example:

$ ls -1 dir3 dir4 $ cp dir4/subdir1/file4 . $ ls -1 dir3 dir4 file4 $ ls -R dir4/ dir4/: subdir1 dir4/subdir1: file4

To copy an entire directory with its contents, use the -R option, as seen below:

$ cp -R dir3/ dir4/ $ ls -R dir3 dir4 file4 ./dir3: total 0 dir2 file3 ./dir3/dir2: dir1 file2 ./dir3/dir2/dir1: file1 ./dir4: dir3 subdir1 ./dir4/dir3: dir2 file3 ./dir4/dir3/dir2: file2 ./dir4/dir3/dir2/dir1: file1 ./dir4/subdir1: file4

Great Linux resources

When you copy empty directories into other directories, there’s no need for the -R parameter.

Читайте также:  Linux настройка ipsec сервера

More to explore

For each command I’ve demonstrated, there are many more options I’ve left out for the sake of brevity.

As a sysadmin, you must know how to copy, move, and rename files and directories. These file-management commands are the basis of much of what you do on the system and are the building blocks for effective Linux administration. I hope this article aids you in understanding this topic, helps your Linux certification path, and adds to your general sysadmin knowledge.

Источник

How to move folder on Linux

Moving a folder (also called directory) on Linux is a common task that every user will have to perform frequently. This can be done via any desktop environment that you have installed, or from command line with the mv command.

While this is a pretty basic function, there are some important caveats to keep in mind. In this guide, we’ll go over several examples of moving a folder on Linux. Feel free to follow along on your own system in order to master the mv command and GUI process.

In this tutorial you will learn:

How to move a folder on Linux

Software Requirements and Linux Command Line Conventions

Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to move a folder via GUI

The process for moving a folder on Linux, through the graphical interface, is going to vary a little depending on your distribution and desktop environment that you have installed. But the only real difference you’ll see is that some of the menus look a little different.

Читайте также:  Linux launch as root

Once you know how to move folders on one GUI, you’ll have mastered them all. In the steps below, we’re using the GNOME desktop environment, which is the default for many popular distributions like Ubuntu.

    Right click on the folder you wish to move, and click on “cut.” Alternatively, you can highlight the folder and press Ctrl + X on your keyboard to cut it.

Cut the folder that you wish to move

Paste the folder into its new location

Click the move to option in the right click context menu

Choose the new destination for the folder you are moving

That’s all there is to it. Note that this will move the folder, and all of its contents, including subdirectories. Next, we’ll cover the command line method.

How to move a folder via command line

The mv command is used to move folders (and files, too) on Linux. The most basic form of the command is to simply specify a source and destination location in your command. You can either use absolute paths or relative paths to the directories.

The command above will move /dir1 into /dir2 . Pretty simple, right?

But wait, what if /dir2 doesn’t already exist? In that case, /dir1 would simply get renamed to /dir2 . In other words, we can specify a new name for our moved directory when we use the mv command. To avoid specifying a new name, just move the directory into a location that already exists.

Now that we understand the behavior of the mv command, let’s look at some other things that are handy to know about it.

By default, mv won’t overwrite a directory that already exists, unless the destination directory is empty.

$ mv example1 example2 mv: cannot move 'example1' to 'example2/example1': Directory not empty

The -i (interactive) option will prompt us to ask if we want to overwrite the directory or not. Enter a “yes” or “no” in response, then press enter.

$ mv -i example1 example2 mv: overwrite 'example2/example1'?

You can also use the -v (verbose) option to get details about the move process. Notice that mv actually says it’s renaming the directory. Essentially, that’s the same as “moving” it, since paths are just links to files and directories on the hard drive.

$ mv -v directory1 directory2 renamed 'directory1' -> 'directory2/directory1'

You can also move multiple directories at the same time. The last directory in your command will be the destination directory for the rest. In this example, dir1 and dir2 will both be moved into dir3 .

Closing Thoughts

In this guide, we saw how to move folders on a Linux system through GUI and command line. This is a common task that all users should master. As usual, the command line method offers us a bit more control over the process, but both methods are equally viable. Use whichever one is more convenient for you.

Comments and Discussions

Источник

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