How to move folders 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.

Читайте также:  Gre туннель astra 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.

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.

Читайте также:  Linux удобный файловый менеджер

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.

Источник

Terminal Basics #8: Move Files and Directories (Cut-Paste Operation)

In the eighth chapter of the Terminal Basics series, learn about moving files and directories using the mv command in Linux.

Cut, copy and paste are part of everyday computing life. In the previous chapter, you learned about copying files and folders (directories) in the terminal. In this part of the Terminal Basics series, you’ll learn about the cut-paste operation (moving) in the Linux terminal.

Moving or cut-paste?

Alright! Cut-paste is not the correct technical term here. It is called moving files (and folders). Since you are new to the command line, you may find the term ‘moving’ confusing. When you copy a file to another location using the cp command, the source file remains in the same location. When you move a file to another location using the mv command, the source file no longer remains in the origin location. This is the same cut-paste operation (Ctrl+X and Ctrl+V) you do in a graphical file explorer.

Basically, moving files in the command line can be thought same as cut-paste in a graphical environment.

Moving files

Linux has a dedicated mv command (short for move) for moving files and directories to other locations. And using the mv command is quite simple:

mv source_file destination_directory

The role of path comes to play here as well. You can use either the absolute or relative path. Whichever suits your need. Let’s see this with an example. You should practice along with it by replicating the example scenarios on your system. This is the directory structure in the example:

[email protected]:~/moving_files$ tree . ├── dir1 │ ├── file_2 │ └── file_3 ├── dir2 │ └── passwd ├── dir3 ├── file_1 ├── file_2 ├── file_3 ├── file_4 ├── passwd └── services 3 directories, 9 files 

Example of moving files in Linux using the mv command

Moving multiple files

mv file1 file2 fileN destination_directory
mv file_2 file_3 file_4 dir3

Example of moving multiple files in Linux

Moving files with caution

If the destination already has files with the same name, the destination files will be replaced immediately. At times, you won’t want that. Like the cp command, the mv command also has an interactive mode with option -i . And the purpose is the same. Ask for confirmation before replacing the files at the destination.

[email protected]:~/moving_files$ mv -i file_3 dir1 mv: overwrite 'dir1/file_3'?

Example of moving interactively in Linux

You can press N to deny replacement and Y or Enter to replace the destination file.

Move but only update

The mv command comes with some special options. One of them is the update option -u . With this, the destination file will only be replaced if the file being moved is newer than it.

mv -u file_name destination_directory
[email protected]:~/moving_files$ ls -l file_2 file_3 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_2 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:06 file_3 

In the destination directory dir1, file_2 was last modified at 10:37 and file_3 was modified at 10:39.

[email protected]:~/moving_files$ ls -l dir1 total 0 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:37 file_2 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_3

In other words, in the destination directory, the file_2 is older and file_3 is newer than the ones being moved. It also means that file_3 won’t me moved while as file_2 will be updated. You can verify it with the timestamps of the files in the destination directory after running the mv command.

[email protected]:~/moving_files$ mv -u file_2 file_3 dir1 [email protected]:~/moving_files$ ls -l dir1 total 0 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_2 -rw-rw-r-- 1 abhishek abhishek 0 Apr 4 10:39 file_3 [email protected]:~/moving_files$ date Tue Apr 4 10:41:16 AM IST 2023 [email protected]:~/moving_files$ 

Using move command with update option

As you can see, the move command was executed at 10:41 and only the timestamp of file_2 has been changed.

Читайте также:  Linux показать все переменные окружения

You can also use the backup option -b . If the destination file is being replaced, it will automatically create a backup with the filename~ pattern.

Troubleshoot: Target is not a directory

If you are moving multiple files, the last argument must be a directory. Otherwise, you’ll encounter this error:

target is not a directory

Handling target is not a directory error in Linux

Here, I create a file which is named dir . The name sounds like a directory, but it is a file. And when I try to move multiple files to it, the obvious error is there: But what if you move a single file to another file? In that case, the target file is replaced by the source file’s content while the source file is renamed as the target file. More on this in later sections.

Moving directories

So far, you have seen everything about moving files. How about moving directories? The cp and rm commands used recusrive option -r to copy and delete folders respectively. However, there is no such requirement for the mv command. You can use the mv command as it is for moving directories.

Moving folders in Linux command line

Here’s an example where I move the dir2 directory to dir3 . And as you can see, dir2 along with its content is moved to dir3 . You can move multiple directories the same way.

Rename files and directories

mv filename new_name_in_same_or_new_location

Rename files with mv command

Let’s say you want to rename a file in the same location. Here’s an example where I rename file_1 to file_one in the same directory. You can also move and rename the files. You just have to provide the directory path and the file name of the destination. Here, I rename services file to my_services while moving it to dir3 .

[email protected]:~/moving_files$ ls dir dir1 dir3 file_2 file_3 file_one passwd services [email protected]:~/moving_files$ mv services dir3/my_services [email protected]:~/moving_files$ ls dir3 dir2 my_services 

You cannot rename multiple files directly with mv command. You have to combine it with other commands like find etc.

Test your knowledge

Time to practice what you just learned. Create a new folder to practice the exercise. In here, create a directory structure like this:

. ├── dir1 ├── dir2 │ ├── dir21 │ ├── dir22 │ └── dir23 └── dir3 

Copy the file /etc/passwd to the current directory. Now rename it secrets . Make three new files named file_1 , file_2 and file_3 . Move all the files to dir22 . Now move the dir22 directory to dir3 . Delete all contents of dir2 now. In the penultimate chapter of the Terminal Basics series, you’ll learn about editing files in the terminal. Stay tuned.

DuckDuckGo hidden features

Hidden Features! 25 Fun Things You Can Do With DuckDuckGo Search Engine

What is TTY in Linux

What is TTY in Linux?

s-tui is used for CPU utilization monitoring

Beautifully Monitor CPU Utilization in Linux Terminal With Stress Terminal UI

Access Grub in VM

How to Access the GRUB Menu in Virtual Machine

FOSS Weekly 23.28

FOSS Weekly #23.28: China’s Linux OS, Linux Exit Codes, Btrfs Origins and More

cp command in Linux

Using cp Command in Linux

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

Источник

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