How to Copy Move Remove Files and Directories on FreeBSD

This article examines how to use the copy move remove and rename commands on a FreeBSD system. The contents of this article can be run in almost all versions of FreeBSD. In writing this article, the FreeBSD 13.2 system was used.


List of contents:
1. How to Copy Files and Folders
2. How to Move Files and Folders
3. How to Delete Files and Folders


1. How to Copy Files on FreeBSD

On UNIX the command to copy files is written with the command "cp" which means copy. The cp command is used to copy directories and files using the command line. With this command, you can transfer multiple files or folders, retain attribute information, and back them up. The resulting cp command file is separate from the original. So, we can say that the cp command is useful for FreeBSD.

Maybe people who are new to FreeBSD still don't know how to use this command and are looking for answers regarding copying multiple files using cp in FreeBSD. That's why we wrote this article to briefly explain how to copy multiple files using the cp command in FreeBSD systems.

You can see the basic script for the cp command below.

cp [options] [source file] [target file]
cp [options] [source file] [target directory]

a. How to Copy Files From One Place to Another

root@ns1:~ # cp xmrig.json /usr/local/etc/xmrig
root@ns1:/usr/local/etc #
cp /root/xmrig.json /usr/local/etc/xmrig

The two commands above will copy the xmrig.json file in the /root folder to the /usr/lical/etc/xmrig folder.

root@ns1:/usr/local/etc # cp /usr/local/etc/xmrig/CMakeLists.txt /usr/local/etc/cpuminer/cpuminer.txt
root@ns1:~ # cp -R /usr/local/etc/xmrig/xmrig.json /usr/local/etc/cpuminer


The first command will copy the CMakeLists.txt file to the /usr/local/etc/cpuminer folder and rename the CMakeLists.txt file to the cpuminer.txt file. Meanwhile, the second command will copy the xmrig.json file in the /usr/local/etc/xmrig folder to the /usr/local/etc/cpuminer folder recursively. This means that the file and all its attributes (permissions and ownership) will all be copied. The second command line is highly recommended and if you want to copy a file, use the "-R" option so that all file attributes are copied.

b. How to Copy Folders

So that you can quickly understand this command, we will create a new folder and its contents. We will place the new folder in the /usr/local/etc folder and the /root folder. Follow these steps to create a folder and its contents.

b.1. Create a training folder in /root

root@ns1:~ # mkdir latihan
root@ns1:~ #
cd latihan
root@ns1:~/latihan #
mkdir freebsd
root@ns1:~/latihan #
mkdir dnsserver
root@ns1:~/latihan #
mkdir webserver
root@ns1:~/latihan #
touch named.conf
root@ns1:~/latihan #
touch httpd.txt
root@ns1:~/latihan #
touch unbound.conf
root@ns1:~/latihan #
touch bind.txt

The script above will create folders /root/exercise, /root/exercise//freebsd, /root/exercise/dnsserver, /root/exercise/webserver and also create files in the /root/exercise folder with the names named.conf, httpd. txt, unbound.conf and bind.txt. If you are not sure, test with the "ls" command.

root@ns1:~ # ls /root/latihan
bind.txt freebsd named.conf webserver
dnsserver httpd.txt unbound.conf

b.2. Create an example folder in /usr/local/etc

root@ns1:~ # mkdir /usr/local/etc/example

After the folder is created, now we practice how to copy/copy the folder.

root@ns1:~ # cp /root/latihan/* /usr/local/etc/example
cp: /root/latihan/dnsserver is a directory (not copied).
cp: /root/latihan/freebsd is a directory (not copied).
cp: /root/latihan/webserver is a directory (not copied).

The script above only copies files, while the folder in /root/exercise is not copied. Use the "ls" command to view the contents of the /usr/local/etc/example folder.

root@ns1:~ # ls /usr/local/etc/example
bind.txt httpd.txt named.conf unbound.conf

It has been explained that the /usr/local/etc/example folder only contains copied files from /root/exercise. We continue with the other method, but first we delete the files in the /usr/local/etc/example folder.

root@ns1:~ # rm -f /usr/local/etc/example/*

After the /usr/local/etc/example folder is empty, we continue by copying the folder by adding the "-R" option.

root@ns1:~ # cp -R /root/latihan /usr/local/etc/example

The script above will copy the training folder to the /usr/local/etc/example folder. Now let's look at the contents of the /usr/local/etc/example folder.

root@ns1:~ # ls /usr/local/etc/example
latihan

The contents of the /usr/local/etc/example folder are only practice folders. We continue again with another copy script. But first, we delete the training folder in the /usr/local/etc/example folder.

root@ns1:~ # rm -fr /usr/local/etc/example/*

Now try using the copy command below and see the difference with the copy command above.

root@ns1:~ # cp -R /root/latihan/* /usr/local/etc/example

The script above will copy the files and folders in /root/exercise to /usr/local/etc/example. Let's look at the contents of the /usr/local/etc/example folder.

root@ns1:~ # ls /usr/local/etc/example
bind.txt dnsserver freebsd httpd.txt named.conf unbound.conf webserver

At this point, do you understand how to use the copy command in FreeBSD. If we understand, continue with the command to move the "mv" file


2. How to Move Files on FreeBSD

The method for moving files or move is almost the same as copying files, the difference is that in the copy command there is a "-R" option while in the move command there is no "-R" option. You can see the basic script for the move command below.

mv [-f | -i | -n] [-hv] source target
mv [-f | -i | -n] [-v] source ... directory

By using the copy script above, now we will practice how to move files on FreeBSD.

a. Moving Files Between Folders

root@ns1:~ # mv /root/latihan/unbound.conf /usr/local/etc/example

The script above simply moves the unbound.conf file to the /usr/local/etc/example folder.

b. Moving Files by Renaming

root@ns1:~ # mv /root/latihan/named.conf /usr/local/etc/example/cpuminer.txt

The script above will move the named.conf file to the /usr/local/etc/example folder, and rename the file to cpuminer.txt.

c. Move all contents of files in the folder

root@ns1:~ # mv /root/latihan/* /usr/local/etc/example

The script above will move the entire file contents from the /training root folder to the /usr/local/etc folder. With the script above, the contents of the folders and files in /root/exercise will be moved. So in general, if you use this script the /root/exercise folder will be empty.

d. Moving a Folder to Another Folder

root@ns1:~ # mv /root/latihan /usr/local/etc/example

The script above will move the training folder into the /usr/local/etc folder. When using this script the /root/exercise folder will move to /usr/local/etc. So in the /root folder there is no longer a training folder because it has moved.


3. How to Delete Files and Folders

rm (short for remove) is a Unix command implemented on FreeBSD. The rm command is used to delete files from the file system. Typically, on most file systems, deleting a file requires write permission on the parent directory (and execute permission, to get to the directory in the first place). Apart from deleting files, the rm command can also be used to delete directories/folders. Follow the steps below to delete files and directories.

a. Deleting Files

root@ns1:~ # rm /root/latihan/unbound.conf

The script above will delete the unbound.conf file, which is in the /root/exercise folder.

b. Deleting Files With Confirmation

root@ns1:~ # rm -i /root/latihan/unbound.conf
remove /root/latihan/unbound.conf?
y


c. Deleting Directories

root@ns1:~ # rm -rf /root/latihan

The script above will delete the training folder in the /root folder.

d. Deleting Directories With Confirmation

root@ns1:~ # rm -I -r -f /root/latihan
recursively remove /root/latihan?
y

e. Deleting Directory Contents

root@ns1:~ # rm -rf /root/latihan/*

The script above will delete all contents in the /root/exercise folder.

f. Deleting Files With File Letter Prefixes

root@ns1:~ # rm /root/latihan/b*

The script above will delete all files in the /root/exercise folder that start with the letter "b". With the script above, all files starting with the letter "b" in the /root/exercise folder will be deleted.


g. Deleting Files With Certain Extensions

root@ns1:~ # rm /root/latihan/*.txt

The script above will delete all files in the /root/exercise folder that have the extension "txt", such as bind.txt, httpd.txt, book.txt. So everything with the extension "txt" will be deleted.

The commands "cp", "mv" and "rm" are basic and important commands in the FreeBSD system. A system administrator is required to master these commands. Because you can be sure that these commands are commonly used on FreeBSD systems both when installing, configuring and maintaining the system.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post