Move files in linux terminal

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.

Читайте также:  Sony vaio pcg 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

Читайте также:  Свернуть все окна линукс минт

Источник

Classic SysAdmin: How to Move Files Using Linux Commands or File Managers

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 altogether. 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:

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 ~/Downloads directory (~/ – 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 ~/Downloads directory, you can move it up one folder (to ~/) like so:

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.

GUI

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.

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

Nautilus screenshot

  1. Open up the Nautilus file manager.
  2. Locate the file you want to move and right-click said file.
  3. From the pop-up menu (Figure 1) select the “Move To” option.
  4. When the Select Destination window opens, navigate to the new location for the file.
  5. 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 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 a 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 then choose the Move To option, or just drag and drop them into a new location.

nautilus

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.

Источник

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