How to move directory 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

Читайте также:  Assassins creed brotherhood 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.

Источник

How to Move Directories in Linux

Managing files and directories is one of the most basic tasks when working in a Linux system. You can move directories in Linux using the GUI and system commands.

In this tutorial, we will cover the two methods you can use to move directories in Linux.

How to move directories in Linux

  • A system running a Linux distribution
  • An account with sudo privileges
  • Access to the terminal window/command line

How to Use mv Command to Move Directories in Linux

The mv command is a Linux system utility that allows you to move files and directories from one place to another. It uses the following command syntax:

mv [options] [source] [destination]

For instance, to move the Example directory to the Downloads directory use:

There will be no output if the command has been successful.

Moving multiple directories works by changing the syntax to include multiple sources:

mv [source 1] [source 2] … [source n] [destination]

For example, to move the Example1, Example2, and Example3 directories to the Downloads directory use:

mv Example1 Example2 Example3 Downloads

Note: Learn how to create new directories in Linux using the terminal in our guide to the Linux mkdir command.

The mv command is also used to rename files and directories with the following syntax:

mv [old filename] [new filename]

For example, renaming the Example file into Test:

Note: If the destination directory does not exist, the mv command will automatically rename the source directory to the destination name.

mv Command Options

The mv command uses the following options:

  • —backup : Creates a backup of the destination file. Allows you to pass additional arguments to change how the command behaves.
  • -b : Works like —backup but does not accept arguments.
  • -f : Automatically overwrite destination files.
  • -i : Enables interactive mode, with a prompt asking for input before overwriting files.
  • -n : Disables overwriting destination files.
  • —strip-trailing-slashes : Removes trailing slashes («/») from source file or directory names.
  • -S : Provide a new suffix for the backup files.
  • -t : Move all provided sources into a destination directory.
  • -T : Treat the provided destination as a file instead of a directory.
  • -u : Update destination files.
  • -v : Show command output in verbose format.
  • -Z : Set the destination file security context to default.

If the destination file already exists, using the -b option allows you to create a backup of the existing file:

In this example, the mv command creates a backup of the existing test.txt file in the Example directory before overwriting. The backup file has the same name as the original, with a tilde («~») added to the end. Use the ls command to verify the backup file:

Using the ls command to verify the backup destination file

Change the default suffix for backup files (tilde) by combining the —backup and -S options. The new suffix is passed as an argument after the -S option and before specifying the source and destination files or directories:

mv --backup -S 01 test.txt Example

In this example, the new backup filename suffix is 01 :

Using the -S option to change the backup file name suffix

Using the -i option prompts the user to confirm overwriting existing files. Typing Y or N and pressing Enter confirms or cancels overwriting, respectively:

Читайте также:  Simply linux live cd

The interactive mode causes a prompt to confirm overwriting files

The -f option automatically overwrites any existing files in the destination directory, while the -n option disables overwriting and skips existing files. Using the -u option only overwrites the file if the source file is newer than the destination file of the same name.

Have the mv command output show the step-by-step process of moving files and directories by using the -v option:

mv -v test01.txt test02.txt test03.txt Example

Enabling verbose command output with the -v option

Note: Be careful not to move a directory or file to /dev/null also known as «the black hole». We outline this and other dangerous terminal commands in our article 14 Dangerous Linux Terminal Commands.

How to Move Directories Using GUI in Linux

There are two methods of moving directories in Linux using the GUI.

Option 1: Copy and Paste

1. Select the directory you want to move and press Ctrl+X. Alternatively, right-click the directory and select Cut from the drop-down menu.

Right-click the directory and select Cut from the drop-down menu

2. Navigate to the destination and press Ctrl+V or right-click the empty space and select Paste from the drop-down menu to move the directory.

Select Paste from the drop-down menu to move the directory

Option 2: Move To. Option

1. Another method is to right-click the directory and select Move to… from the drop-down menu.

Right-click the directory and select Move to. from the drop-down menu

2. In the Select Move Destination window, navigate to the destination and click Select to move the directory.

navigate to destination and click Select to move directory

Note: If you are using Ubuntu, check out our tutorial on how to install a GUI on Ubuntu.

After reading this article, you should know how to move directories in Linux using the GUI and the built-in mv command.

Learn more about using Linux commands in our comprehensive overview of Linux Commands.

For more information about Linux directories, make sure to read our article how to rename a directory in Linux.

Источник

How to move a directory in Linux

In Linux we can move the directories to a specific folder and protect them by changing their access permission in order to secure the files and data present in those directories. To move the directory from one path to another is much simpler and can be done easily.

The one thing most people are confused about is the difference between move and copy command; copy command is used to make the clone of files/directories to some other paths while keeping the original file/directory at its original path, while the move command is used to move the file/directory from its original path and place it to some other path.

In this write-up, we have explained the methods in detail through which we can move the directories from one path to another in Linux.

Читайте также:  All linux programming application

How to move directory in Linux

There are two methods to move directories from one destination to another in Linux is either by command line method or graphical user interface, both are discussed in detail.

Method 1: Command line method

We have two directories with the name, mydirectory1, and mydirectory2, we can list both of them using the ls command:

The general syntax of moving the directory from one path to another is:

Use the mv command to move the directory from source to some destination and we can use some options along with this command. The options which can be used with the mv command are:

Options Description
-f It will overwrite the functions forcibly while moving the directory from source to destination
-i It will enable the interactive mode
-v It will show you the progress of the command execution
-u It will update the destination files
-z It will set the security context of the file to the default settings
-n It will disable the overwriting in the destination files

To understand this, we will move the directory mydirectory2 from /home/hammad (current working directory) to /home/hammad/Documents using the command:

We have used the “-f” option in the above command which is used to forcibly move the directories, and list down the contents, using the ls command:

Only “mydirectory1” is present which means the “mydirectory2” has successfully been moved into to Documents directory, to verify it, use the command:

Similarly, we can move multiple directories using the command:

$ mv -vfi / home / hammad / Documents / mydirectory1 / home / hammad / Documents / mydirectory2 / home / hammad

In the above-executed command, we have moved the multiple directories; mydirectory1 and mydirectory2, from /home/hammad/Documents to /home/hammad using the flags “-vfi” where “v” is used to display the progress of executed command, “f” is used to forcibly moved the directory if required, and “i” is used to enabling the interactive mode.

Method 2: Graphical User interface

For the GUI method, go to the destination folder, right-click on the directory you want to move, and choose the move option:

Choose the “Destination folder” in our case, it is Documents, and then clicks on the “Select” button:

The directory has successfully been moved, open the “Documents” directory to verify the mobility of the mydirectory2 from /home/hammad to /home/hammad/Documents:

Conclusion

The directory can be moved either by using the command line method or GUI method; both are convenient and understandable, it’s up to the reader’s choice. In this write-up, we have explained both the methods in detail, the command line method is recommended because with this method you can use other options using the flags.

About the author

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.

Источник

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