Command Linux

Linux Copy File to Current Directory and Rename

Linux users have been using the mv and cp commands for several decades to rename and copy files. There are a few unique options for renaming a file, including simple techniques, variations, and other commands.

Linux Copy File to Current Directory and Rename

Linux Copy a file to Current Directory
An obvious way to do this is to use a command like “cp file1 file1-orig.”

By Using cp Command
The command is named cp from the short name of copy, which means copy. Linux system users can copy folders, directories, and files using the cp command. We can use cp commands along with destination and source only. Here along with the file’s path, the filename is also changed—the syntax for the cp command.

cp <source> <destination>

Or

cp file1.txt file2.txt

where,
cp > copy command
file1.txt > source file “file1.txt”
file2.txt > destination file “file2.txt”

Presence of file2.txt
If file2.txt is present, it is overwritten with the contents of file1. On the contrary, if file2.txt is not present, then it is created in that condition.

Overwriting the File
Using the following command, we can copy file1.txt to file2.txt:

cp -i file1.txt file2.txt

Here, option -i is used to making it interactive and provide a prompt when file2.txt is overridden.

Linux copies an entire directory’s contents to another directory. In the command below, the wildcard * is used to copy one file between two locations.

cp logs1 /* logs2

Where

Thus we have seen that to copy the content from one directory to another. The cp command is used for which the destination directory is mandatory.

In the absence of a destination directory, if we want to copy the content from the current directory, then we first create the destination directory and then copy the content. We use the below command to copy the content by creating the destination directory.

cp -r logs1 logs2

cp > copy command
-r > recursively
logs > source directory
logs2 > destination directory

Linux Copy multiple files to Current Directory

By Using cp Command
All we need is the cp command, plus the sources and destinations to copy multiple files and directories. Its syntax is something like this.

cp <source1> <source2> <source3>…….. <destination>

Or

cp file1.txt file2.txt logs1

Where,

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

By Using tar Command
For copying a large number of files, the tar command is a better option. This strategy can get real old fast.

  • We use the tar command to collect all the files we edit in the files we want to back up.
  • Make backup copies easier by using a for a loop.

Tar is a great way to see the entire contents of an existing directory. In this, we use the following command.

tar cf myfiles.tar *

We use the following command to identify a group of files with a pattern.

tar cf myfiles.tar *.txt

In each case, we see all files with the .txt extension or in the same directory end with the myfiles.tar file.

Easy loops allow us to create backup copies with modified names.

for file in *
> do
>    cp $file $file-orig
> done

To copy the big-name file, we copy the files using the syntax of “-orig”.

cp file-with-a-very-long-name{,-orig}

Linux Rename File to Current Directory

The mv command is used to rename the file in the Linux system. For this, we need the current_name and new_name of the directory along with the mv command. We use the following syntax to rename the file.

mv <current_name> <new_name>

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

Where,

Keep in mind that the new name we are giving to the file may not already exist. If this happens and we use the mv command, then the file gets overridden.

Rename and Copy at the same time

If we want to rename and copy at the same time, then we use the following command.

cp program3.cpp homework6.cpp

Conclusion

There are many options for renaming and copying files in the Linux system. This article saw how we copy files in directories and change their names through different commands. We hope you have understood well from this article how directories are copied and renamed in Linux using wildcards with commands.

Related Articles

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Back to top button