Move and rename in linux

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.

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 see graphics driver

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 Copy, Move and Rename Files and Directories in Linux System

Till now we have seen how to explore the Linux System, the meaning and use of wildcards, and create and delete files and directories in Linux System. Now let us look at how we can copy, move, and rename these files and directories. Let’s start with how can we copy files and directories 1. cp command The cp command stands for copy is used to copy files and directories in Linux System. The syntax for cp command. copy syntax We can simply use the cp command along with the source and destination. copy file single In the above example, we used the command cp file1.txt file2.txt where cp represents the copy command
file1.txt represents the source file «file1.txt»
file2.txt represents the destination file «file2.txt» So what if file2.txt did not exist? If file2.txt exists, it is overwritten with the contents of file1. If file2.txt does not exist, it is created. Okay but can we do something to check when the file is being overridden?

  • copy file1.txt to file2.txt and prompt if file2.txt is being overridden
Читайте также:  Linux ping через определенный интерфейс

copy single file prompt

In the above example, we use the command cp -i file1.txt file2.txt to copy file1.txt to file2.txt and we used to option -i to make it interactive and provide us a prompt if file2.txt is being overridden.

Okay so now we know how to copy files but what if we need to copy all the contents from directory1 to directory2?

copy content directory

In the above example, we used wildcard * and created the command cp logs1/* logs2 where

cp represents the copy command
logs1 represents the source directory
logs1/* represents all the contents of the directory logs1
logs2 represents the destination directory

Unlike the case of when we copy a file we need destination directory to exist when we use cp command.

So what if we want that
If the folder does not exist then the folder should be created and then contents should be copied.

copy directory contents

In the above example, we used the option -r and created a command cp -r logs1 logs2 where

cp represents the copy command
-r represents recursively
logs1 represents the source directory
logs2 represents the destination directory

Now we know how we can copy a single file/directory but what about multiple files and directories.

The cp command syntax for multiple files and directories

copy multiple syntax

We can simply use the cp command along with all the sources and the destination.

copy multiple to directory

In the above example, we used the commands cp file1.txt file2.txt logs1 where

cp represents the copy command
file1.txt represents the source file file1.txt
file2.txt represents the source file file2.txt
logs1 represents the destination directory logs1

These are the ways we can copy files/directories. Now let’s have a look at how to move files/directories.

2. mv command

The mv command stands for move is used to move files and directories in Linux System.

The syntax for mv command.

move syntax

We can simply use the mv command along with the source and destination.

move files dir

In the above example, we used the command mv file2.txt logs1 where

mv represents the move command
file2.txt represents the source file file2.txt
logs1 represents the destination directory logs1

To check whether the file has been moved to the destination directory we used the command ls . logs1 where

ls represents list command
.(Dot) represents the current working directory
logs1 represents the destination directory logs1

What if we want to move one directory to another directory?

move directory

In the above example, we used the command mv logs1 logs2 where

mv represents the move command
logs1 represents the source directory logs1
logs2 represents the destination directory logs2

Читайте также:  Linux delete postgresql user

To check whether the file has been moved to the destination directory we used the command ls . logs2 where

ls represents list command
.(Dot) represents the current working directory
logs2 represents the destination directory logs2

So, what happens if the destination logs2 does not exist?

If the directory does not exist then a new directory is created and then the contents are moved to that directory.

Similar to what we saw with cp command the mv command also overwrites the files if it already exists.

But what if we want to move only those files that are not already present in the destination directory and skip files that are already present?

move updated files

In the above example, we first checked the content of the directories logs1 and logs2 and found that both logs1 and logs2 contain the file1 and file2.

After that, we used the command mv -u logs1/* logs2 where

mv represents the move command
-u represents update
logs1/* represents the content inside the logs1 directory
logs2 represents the destination directory logs2

What if we want to move all the .txt files to a new folder?

move text files

In the above example, we first listed all the contents of the current directory and then used the command mv *.txt text_files where

mv represents the move command
*.txt represents all the .txt files
text_files represents the destination directory text_files

To check whether all the files of type «.txt» have been moved or not we used the ls . text_files command where

. represents the current working directory
text_files represents the destination directory text_files

Now we know how to easily move tons of files within a second just by using our wildcards.

These are the ways we can move files/directories. Now let’s have a look at how to rename a file/directory.

3. mv command for renaming

Hold on, didn’t we just used the mv command to move the files and directories?

Yes the mv command is used for both moving as well as renaming a file/directory.

The syntax for mv command for renaming

rename syntax

To rename a file we just used mv command along with current_name and new_name

rename file name

In the above example, we used the command mv original new where

mv represents move command
original represents the current_name original
new represents new_name new

Note:- Please be cautious that the new name that we are giving to the file is not the name of already existing file because if the file with new_name already exists then the file will be overridden when we use mv command.

Let’s have a look at how we can rename a directory

rename folder

In the above example, we used the command mv original_folder/ new_folder where

mv represents move command
original_folder/ represents the current_name original_folder
new_folder represents new_name new_folder

Okay, so that’s all the commands we need to know for Copy, Move, and Rename Files and Directories in Linux System.

I hope you understood the basics of file manipulation in Linux. Please, let me know if there are any questions.

Источник

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