In these examples we will cover the Red Hat Certified System Administrator (RHCSA) objective “Create, delete, copy, and move files and directories”.
Create Files and Directories
Let’s start with file creation
There are a number of ways to create files within Linux. My favourite way to create a quick test file is with the ‘touch’ command. You simply specify ‘touch’ followed by the name of the file that you want to create.
[[email protected] ~]# touch testing [[email protected] ~]# ls -la testing -rw-rw-r--. 1 root root 0 May 14 00:44 testing
This will create an empty file (we can see above the file size is listed as 0, just before the time stamp) called ‘testing’ in the current working directory. My current session is within the root user’s home directory, /root/, so the file has therefore been created at /root/testing in this example. You can print out the current working directory with the ‘pwd’ command.
The touch command can also be used on an existing file or directory, the act of ‘touching’ it will update the last modified time stamp.
We can also define the specific location where the file should be created:
[[email protected] ~]# touch /root/somewhere/file
Another example is using the ‘echo’ command. By default, the ‘echo’ command will print out the text that we feed it, as shown below:
[[email protected] ~]# echo "hello" hello
However we can also use the ‘>’ redirection, as covered in another post, to redirect the output to a new file that we specify:
[[email protected] ~]# echo "hello" > hello [[email protected] ~]# cat hello hello
We have confirmed that our new ‘hello’ file contains the text ‘hello’.
Other tools will create new files too, for instance “vim testing” will open the vim text editor and allow you to edit a file called ‘testing’. The file will not actually be created until you save the file from within the vim editor though.
Directories (also known as folders in Windows) can also be created with a number of different techniques. The most simple way is with the ‘mkdir’ command, simply specify the name of the directory to create afterwards.
[[email protected] ~]# mkdir directory
This will create a new directory (or folder) called ‘directory’ in the current working directory. We can now enter it and use it to store content in.
[[email protected] ~]# cd directory/ [[email protected] directory]# pwd /root/directory
We can also use the ‘mkdir’ command with the ‘-p’ flag to create parent directories if they do not already exist. In the example below, we can create the sub directory structure /root/directory/one/two/three in one step:
[[email protected] directory]# mkdir -p one/two/three [[email protected] directory]# ls -lR .: total 0 drwxr-xr-x. 3 root root 16 May 14 00:56 one ./one: total 0 drwxr-xr-x. 3 root root 18 May 14 00:56 two ./one/two: total 0 drwxr-xr-x. 2 root root 6 May 14 00:56 three ./one/two/three: total 0
The ‘-R’ flag from ‘ls’ will list all directories within recursively.
Delete Files and Directories
Now that we know how to create files and directories, how do we delete them?
The ‘rm’ command, short for remove, is used to delete files or directories. In this example, we’ll delete the ‘testing’ file that we created in /root/testing earlier.
[[email protected] ~]# rm testing rm: remove regular empty file ‘testing’? y
When using the ‘rm’ command, by default we’re prompted to confirm removal of the file by pressing the ‘y’ key for yes, followed by enter.
If you’re absolutely sure that you want to remove the file in one command without being prompted to confirm, you can use the ‘-f’ command to force the action. For obvious reasons, you’ll want to be careful with this option.
This would be more useful when deleting many files at once. In the example below, we create two new empty files called ‘testing2’ and ‘testing3’. We then delete everything named ‘testing*’, which includes these two files. The ‘*’ acts as a wildcard, and includes anything starting with the string that precedes it.
[[email protected] ~]# touch testing2 [[email protected] ~]# touch testing3 [[email protected] ~]# rm testing* rm: remove regular empty file ‘testing2’? y rm: remove regular empty file ‘testing3’? y
As you can see, we were prompted to remove each individual file, which could get tedious if you need to remove thousands of files within a directory.
Deleting directories is a little different. The ‘rm’ command is still used, however look what happens if we try to delete a directory in the same manner as a file:
[[email protected] ~]# rm directory/ rm: cannot remove ‘directory/’: Is a directory
By default, to remove a directory we need to use the ‘-d’ flag, which is used to remove an empty directory.
[[email protected] ~]# rm -d directory/ rm: cannot remove ‘directory/’: Directory not empty
Note that I said empty! To demonstrate this, let’s create a fresh empty directory called ’empty’ and try again:
[[email protected] ~]# mkdir empty [[email protected] ~]# rm -vd empty/ rm: remove directory ‘empty/’? y removed directory: ‘empty/’
In the example above I’ve also used the ‘-v’ flag for verbose, this just outputs some extra information. In this case, the final line confirming that the directory has been removed is given due to the increased verbosity.
Now back to our /root/directory, we created content within here in the demonstration above, sub directories with ‘mkdir’ to be specific. We can either go into the directory and remove the files within first, or we can use the ‘-r’ flag
[[email protected] ~]# rm -r directory/ rm: descend into directory ‘directory/’? y rm: descend into directory ‘directory/one’? y rm: descend into directory ‘directory/one/two’? y rm: remove directory ‘directory/one/two/three’? y rm: remove directory ‘directory/one/two’? y rm: remove directory ‘directory/one’? y rm: remove directory ‘directory/’? y
This is another good example where the ‘-f’ flag to force the remove may be useful, as I had to manually confirm with ‘y’ multiple times here. The ‘-r’ flag is recursively deleting all content within /root/directory in the above example. Once the contents are removed, it finishes up clearing out everything within, it deletes the directory specified itself.
You may have heard many stories about the classic ‘rm -rf’ command, and for good reason, it’s extremely powerful and cause a lot of damage. For example if you’re deleting a directory recursively and accidentally put in a space somewhere and specify ‘/’ you’ll start deleting the entire file system. Be careful and don’t start using ‘-f’ until you’ve gotten some practice in.
Fun fact: By default when you run the ‘rm’ command, you’re actually running it with the ‘-i’ flag, which is what causes us to be prompted for every file or directory to remove. This works through a Bash alias – in short there is an alias for ‘rm’ that actually runs ‘rm -i’. To list the different aliases for all commands, simply type ‘alias’ into the Bash shell.
[[email protected] ~]# alias rm alias rm='rm -i'
Copy Files and Directories
We can copy files and directories with the ‘cp’ command.
The ‘cp’ command works by first specifying the file that you want to copy, followed by the destination where you want to copy it to.
Let’s start off by creating an example file to copy.
[[email protected] ~]# echo "original" > original [[email protected] ~]# cat original original
We’ve confirmed our /root/original file contains the text ‘original’, now let’s copy it!
[[email protected] ~]# cp original cheapcopy [[email protected] ~]# cat cheapcopy original
This copies the source file ‘original’ to the destination ‘cheapcopy’, in this example within the same working directory of /root/ as no full path was provided. We could instead specify the path we want to create the copied file however:
[[email protected] ~]# cp /root/cheapcopy /tmp/morecopy [[email protected] ~]# cat /tmp/morecopy original
Unlike when we create a hard link, the raw data is copied into a new inode, the copied files can be modified independently of the original file. Hard links differ in this way, as the hard links all point to the same inode, editing one file will show the same modifications if a different hard link is accessed.
Like the delete command, we’ll be using the same ‘cp’ command as before with the files, except now to work with directories we’ll need an extra flag.
If we just try to copy a directory in the same way we did with files, this is what happens.
[[email protected] ~]# cp originaldir/ copydir/ cp: omitting directory ‘originaldir/’
For this to work, we need the ‘-r’ flag, which specifies recursive.
[[email protected] ~]# cp -r originaldir/ copydir
By default this will also copy all of the files within the source directory to the destination, for example:
[[email protected] ~]# echo "something" > originaldir/file [[email protected] ~]# cp -r originaldir/ newcopy [[email protected] ~]# cat newcopy/file something
In the above example we first create /root/originaldir/file containing the text “something”. We then copy the /root/originaldir to /root/newcopy, then confirm that the /root/newcopy/file contains the string “something” as per the original.
Move Files and Directories
Moving files and directories is similar to copying, except rather than copying the content we’re moving the original.
Moving files is done with the ‘mv’ command, similar to ‘cp’ above we first specify the source file, followed by the destination where we wish to move the file.
[[email protected] ~]# echo "movethis" > movethis [[email protected] ~]# mv movethis alright [[email protected] ~]# cat alright movethis
In this example, we first create the file ‘movethis’ containing the string ‘movethis’. Next we use the ‘mv’ command to move ‘movethis’ to ‘alright’, and view the contents of our newly moved file ‘alright’ to confirm they are as expected.
This example is essentially just renaming the file, we’re moving it from within the current working directory to the same directory but with a different name. This is also generally how you rename files within Linux.
We can of course work with full paths as well, in this example we move our newly moved file to the /tmp directory.
[[email protected] ~]# mv /root/alright /tmp/movedagain [[email protected] ~]# cat /tmp/movedagain movethis
By default you will be prompted if you attempt to move a file to one that already exists, you’ll be given the option to overwrite the destination file, or you can also specify the ‘-f’ flag to force the overwrite without being prompted.
Moving directories with the ‘mv’ command is quite straightforward, it’s the same as moving files, no additional flags are needed.
[[email protected] ~]# mkdir newdir [[email protected]rhel8 ~]# mv newdir moveddir
This will also move anything within the directory:
[[email protected] ~]# echo "hello" > moveddir/hello [[email protected] ~]# mv moveddir/ movedagain [[email protected] ~]# cat movedagain/hello hello
During the RHCSA exam you will have access to built in documentation, you can find information on these commands and their flags by running the following commands to view the manual pages:
man cp man rm man mv
Basically just use ‘man’ followed by the command name to see the manual page for it.
That’s it, you should now have a basic understanding of how to create, delete, copy and move files and directories in RHEL 8 for the RHCSA exam. Make sure you practice these examples yourself to get a full understanding on how the commands work.
This post is part of our Red Hat Certified System Administrator (RHCSA) exam study guide series for Red Hat Enterprise Linux (RHEL) 8. For more RHCSA related posts and information, see our full RHCSA 8 study guide.