LFCS Admin Exam preparation guide – Create, delete, copy, and move files and directories

Title

LFCS Admin Exam preparation guide series, main page can be found here.

This post is part of Essential Commands from the domain competency list for the exam. The full list can be found in the link above paragraph or the Linux Foundation page here.

This post is explaining how to perform Create, delete, copy, and move files and directories operations.

First, we have a few methods available to us to create file, touch, and nano we already talk about in the previous post, touch command will create a zero bytes empty file. nano will open the editor and allow us to modify the file before saving and creating on the disk.

touch and nano examples

Another option to create a file is to redirect the command output to the file, for example, we can redirect ls command to the file to create a list of content of the directory. The new file will as well contain the file we are creating if we use the command in the current directory.

command redirection to the file example

To create a directory, we can use the mkdir command, which stands for making a directory. Here we are creating the target directory with the mkdir target command.

mkdir command example

Now we have a few files which we can copy or move to another location. To copy file we can use the cp – copy command, we can use this command to rename the file while copying it to the new location. Below example, we are copying the same file to the new directory and as well as coping it with a new name. After listing the destination directory, we can see that it contains both the files and that the renamed file contains precisely the same data. There is scp command – secure copy – to copy files or directory between different Linux servers.

cp command example

Another command is the mv command which we can use to move the file or directory to a new location, similar we can use this command to rename a file to a different name. In below example, we are moving the firstfile.txt to the new location and secondfile.txt we are moving and renaming to new file movefile.txt

mv command example

Sometimes when we create a file or directory, we will like to delete it, we can do this with the help of rm command. rmdir is a command we can use to remove an empty directory if that directory contains anything inside the command will fail. We can use rm -r where -r stands for recursive this will remove the directory and its content. The rm command removes files or directory instantly; there is no bin or trash where we can retrieve what we deleted back. rm -rf, where f stands for force, is a very dangerous command because it will remove everything in the current working directory without warring.

rm command example

Thank you for reading.