Moving files and directories in linux

How to rename and move files and directories on Linux

Overview

In this tutorial, you will learn how to use the mv command to move and renames files and directories on Linux.

Files and directories on Linux as very similar, from a filesystem point of view. This means the operations done on one can be also done on the other, with very few exceptions.

As such, you will notice that commands used to perform actions on files are identical with directories.

Rename files on Linux

To rename a file in Linux you use the mv command. The command accepts two or more arguments. For renaming files, only two arguments are needed, which are the source file and the target file.

The mv command will take the source file specified and rename it to the target file.

To rename a file named student1 to student10, for example, you would run the following command.

Provided the file target is the same directory, all file attributes will remain, including permissions.

Moving a file on Linux

To move a file to another location we use the same method as renaming a file, except the file path should be different.

For example, to move a file from /home/student1/lab-work.log to /var/labs/student1/lab-work.log , you would run the following command.

Moving and Renaming files on Linux

A file can be renamed during a move process using the mv command. You simply give the target path a different name. When mv moves the file, it will be given a new name.

For example, to move a file named student1.txt to /var/students and rename it to class1-student1.txt , you would run the following command.

Moving Multiple files on Linux

The mv command accepts multiple source files, which means we can move two or more files at the same time. When executing the mv command, each file listed will be considered a source with the last path being the exception. The last path will be treated as the target.

For example, to move student1.txt and student2.txt to /var/students , you would run the following command.

Moving Directories on Linux

Moving directories work the same as moving files. We specify the source directory and give a target directory.

For example, to move a directory path /tmp/logs to

/data/logs you would run the following command.

Moving Multiple Directories on Linux

As with files, multiple directories can be moved to a new location. We simply specially all of the directories to be moved, and then give a target directory for them to be moved to.

Verbose Output Flag

The mv command will perform its operations silently. No output will be printed to the screen while files or directories are being moved or renamed.

To instruct the mv command to print out a log of actions being taken, you can use the -v flag. This flag enabled verbosity, which is helpful for auditing.

Do Not Overwrite Existing Files

To force the mv command to not overwrite existing files when moving or renaming a file, use the -n flag.

In the example below, if the student2.txt file already exists, then the mv command will not rename the file and it will exit with an error.

Do Not Prompt to Confirm Overwrites

If you want to forcefully move files or directories and overwrite paths that already exist, you can use the -f flag. This is effective for overwriting old, stale files or directories with new ones with the same name.

Источник

Moving files and directories in linux

By now, you’ve learned a little about the structure of the filesystem; and you’ve learned how to create files and directories.

But just because you know how to create files and directories doesn’t mean that you’re stuck with the changes you’ve made. What if you want to rename and/or move files and directories?

Let’s start with the copy command.

Like so many Linux features, you have a variety of options from which to choose when you want to manipulate files and directories. You can also use wildcards when you’re copying, moving, or deleting files and directories.

Basically, the copy command is not much more complex than typing:

so to copy the file sneakers.txt to the directory tigger in your login directory, just type:

Читайте также:  Liber office для windows

cp sneakers.txt tigger

Notice that you also used relative pathnames to copy the file. You can use both relative and absolute pathnames with cp . Our login directory is the parent of the directory tigger ; meaning that tigger is one directory down from ours.

Read the cp man page ( man cp ) for a full list of the options available with cp . But among the options you can use with cp are:

-i — interactive. Prompts you to confirm if the file is going to overwrite a file in your destination. This is a handy option because it can help prevent you from making mistakes.

-r — recursive. Rather than just copying all the files and directories, copies the whole directory tree, subdirectories and all, to another location.

-f — force. Copies without prompting you for confirmation that the file should be overwritten. Unless you’re sure you want to force the copy, you probably don’t want to make friends with this option right now.

-v — verbose. Will show the progress of the files being copied.

Just by using cp alone, you won’t see much when the command is executed. Using an option, such as -i , can make the process a little more useful, because if you want to copy a file to a location that already has a file with the same name, you’ll be asked first if you really want to overwrite — meaning replace — the file that’s already there.

Remember that among your options is -f (force), which can overwrite files without asking you if you’re certain. Make sure, when you use the force option, that you really want to overwrite a file.

Now that we have the file sneakers.txt in the tigger directory, let’s use cp -i to copy the file again to the same location.

[newuser@localhost newuser]$ cp -i sneakers.txt tigger cp: overwrite ‘tigger/sneakers.txt’?

To overwrite the file that’s already there, press Y and then Enter . Don’t want to overwrite the file? Now is the time to press N and Enter .

To move files, use the mv command ( man mv ), which is similar to the cp command, except that with mv the file is physically moved from one place to another, instead of being duplicated, as with cp .

Common options available with mv include:

-i — interactive. Will prompt you if the file you’ve selected will overwrite an existing file in the destination directory. This is a good option, because like the -i option in cp , you’ll be given the chance to make sure you want to replace an existing file.

-f — force. Overrides the interactive mode and moves without prompting. Unless you know what you’re doing, this option doesn’t play nice; be very careful about using it until you become more comfortable with your system.

-v — verbose. Shows a list of the files being moved.

If you want to move a file out of your home directory and into another directory, you would type:

mv sneakers.txt tigger

or, mv sneakers.txt /home/newuser /home/newuser/tigger using absolute pathnames.

Actually, we’ve already covered half of renaming, because when you copy or move files, you can also rename.

To copy the file sneakers.txt from our login directory to our tigger subdirectory, just type:

cp sneakers.txt tigger

To copy and rename that file from sneakers.txt to piglet.txt , type:

cp sneakers.txt tigger/piglet.txt

To move and rename the file, just substitute mv for cp in the above example.

If you cd to tigger and use ls , you’ll see the file piglet.txt .

If you just want to rename the file and keep its location, just mv in your current directory:

mv sneakers.txt piglet.txt

We talked about creating files with the touch command and by using redirection in Chapter 13 . And we created the directory tigger using mkdir .

But we haven’t discussed how to delete files and directories.

Deleting files and directories with the rm command ( man rm ) is a straightforward process.

Let’s take our new file piglet.txt , and delete it from the tigger directory with the rm command:

What happens if we didn’t really want to get rid of it? Too late! Again, that’s where the -i (interactive) option comes in handy, because it gives a second chance to think about whether we really want to toss the file.

[newuser@localhost newuser]$ rm -i piglet.txt rm: remove ‘piglet.txt’?

You can also delete files using the wildcard * , but be careful, because you can easily delete files you didn’t intend to throw away.

To remove a file using a wildcard, you would type:

You can also remove more than one file in one command, as in:

rm piglet.txt sneakers.txt

Options for removing files — and directories — include:

-i — interactive. Prompts you to confirm the deletion. This is good.

-f — force. Overrides interactive mode and removes the file(s) without prompting. This might not be good, unless you know exactly what you’re doing.

-v — verbose. Shows a listing of files as they’re being removed.

-r — recursive. When removing directories, will remove all of the files and the subdirectories of the specified directory. This can also get rid of an empty directory.

To remove directories with rm , you must specify the -r option.

For example, if you want to recursively remove the directory tigger you would type:

And if you want to combine options, such as forcing a recursive deletion, you can type:

Don’t be too «forceful»

The rm is a powerful command, and can delete your entire system! If you’re root and you type the simple command rm -rf / you’re sunk — like a snake eating its tail, the command will recursively remove everything on your system.

A safer alternative to using rm for removing directories is the rmdir command. With this command, you won’t be allowed to use recursive deletions, so a directory which has files in it won’t be deleted.

Read the rmdir man page by typing man rmdir to find out more about the command.

Источник

How to Copy / Move Files and Directories in Linux with “cp” and “mv” commands

Copy Files and Directories

The cp command will copy files and directories or copy multiple sources to a destination directory. The basic syntax of the cp command is:

If you have multiple files/directories to be coped to a destination directory, use the below command syntax.

Common options used with the cp command, include:
-a – archive, never follow symbolic links, preserve links, copy directories recursively
-f – if an existing destination file cannot be opened, remove it and try again
-i – prompt before overwriting an existing file
-r – copy directories recursively

These examples show typical invocations of the cp command with descriptions of what they do.

Example 1

Copying a single file to a destination directory:

Example 2

Copying multiple files to a destination directory:

Example 3

Copying a directory (and it’s contents) recursively:

Moving Files and Directories

The mv command will move or rename files or directories, or can move multiple sources (files and directories) to a destination directory. The basic syntax of the mv command is:

To move multiple files/directories into a destination, use the below syntax.

Common options used with the mv command:
-f – do not prompt before overwriting
-i – prompt before overwrite
-u – move only when the source file is newer than the destination file or when the destination file is missing

If a file or directory is moved to a new name within the same directory, it is effectively renamed. For example, this would rename a file from oldname to newname.

Источник

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.

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.

Источник

Читайте также:  Видеоуроки по установке линукс
Оцените статью
Be careful!