Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
RHCSA - Understand and Use Essential Tools: Create, Delete, Copy, & Move Files & Directories
In this objective we go into detail on creating, deleting, copying and moving files & directories.
Lesson Setup
A seperate folder to practice the objective in can be setup:
Create & change into lesson directory
mkdir -p ~/file-dir-practice
cd ~/file-dir-practice
Ensure to follow the exercises in order as they build on each other.
Creating Files & Directories
Creating Files
To create files you can use a variety of different commands or methods, for example:
-
touch
command: Thetouch
command can be used to create a new empty file, but can also create a file with a specific timestamp using the-t datetime
option, where datetime is in format[CC]YY]MMDDhhmm[.ss]
.Creating a new file using
touch
:touch new-file-touch.txt ls -l new-file-touch.txt
The date and time stamp of the file display exactly when you created the file.
Creating a new file with a specified date & time stamp using
touch
:touch -t 202001010830 new-file-touch-datetime.txt ls -l new-file-touch-datetime.txt stat -c %y new-file-touch-datetime.txt
The date & time stamp of the file display the date & time specified, in this case; 08:30 on 1st Jan 2020.
-
Text Editors (
vi
/vim
): Using text editors such asvim
, you can create new files.Creating a file using
vim
:vim new-file-vim.txt
Once the new file is open in
vim
, you can save and quit by typing:wq
and hitting Enter. Then run the following to check the file exists:ls -l new-file-vim.txt
-
Redirection (
>
): Using redirection is the simplest way to create a a new empty file as you can just run> newfile
. It can also be used to create a file from the output of any command.Creating a file using basic redirection:
> new-file-redirect.txt ls -l new-file-redirect.txt
Creating a file using redirection of output from a command:
df -h > new-file-redirect-command.txt cat new-file-redirect-command.txt
The
cat
command shows that the content of the new file does contain the output of thedf
command.
Creating Directories
To create directories you use the mkdir
command, which you have been using for the lesson setups if you have been following along. You can use the mkdir
command to create a single directory or to create directories and their sub-directories also when using the -p
option. The -v
option displays verbose output detailing the directory creations.
The -p
flag is great for using with scripts, as, unlike running mkdir
without it, it will not complain if the directory already exists.
Creating a single directory:
mkdir -v solo-dir
Creating a directory with a sub-directory that has a sub-directory aswell:
mkdir -v -p sub-dirs-within/sub-dir/another-dir/final-dir
ls -lR sub-dirs-within
Notice how the verbose output to mkdir
shows all the directories getting created, confirmed by running ls
with the -lR
options.
Copying Files & Directories
To copy files and directories you use the cp
command. You can create a copy of a single file, copy multiple files to another directory and copy directories and their contents also. The -v
verbose flag is useful to see the actions that the cp
command performs.
Create a copy of a file:
cp -v new-file-touch.txt new-file-touch.txt.copy
ls -l new-file-touch.txt new-file-touch.txt.copy
Notice how the file copy was successful, but the original timestamp was lost for the new file.
cp -v -p new-file-touch.txt new-file-touch.txt.preserved-copy
ls -l new-file-touch.txt new-file-touch.txt.preserved-copy
The -p
option is used to preserve the permissions, ownership & timestamps.
Copying multiple files to another directory:
cp -v new-file-touch.txt new-file-touch.txt.copy new-file-touch.txt.preserved-copy solo-dir
This copied the files to the solo-dir
folder, however you can copy files to sub-directories also.
cp -v new-file-touch.txt new-file-touch.txt.copy sub-dirs-within/sub-dir/another-dir
ls -lR sub-dirs-within
The ls
command shows the files within the another-dir
sub-directory.
Copying a directory:
new-dir
does not have a trailing /
. This means we want to create a copy of solo-dir
as new-dir
and not copy solo-dir
inside of an existing directory called new-dir
.
cp -v -r solo-dir new-dir
Notice how the output of the command shows that the solo-dir
along with its contents was copied to a directory called new-dir
which was created by the cp
command.
Copying a directory into another directory:
cp -v -r solo-dir new-dir/
Notice how this time the solo-dir
directory was copied inside of the new-dir
folder.
Moving Files & Directories
To move files and directories you use the mv
command. You can use the mv
command to simply rename a file or directory, or you can move files or directories to another location. The -v
verbose flag is useful to see the actions that the mv
command performs.
Rename a file:
mv -v new-file-touch.txt new-file-touch.txt.renamed
Rename a directory:
mv -v solo-dir solo-dir-renamed
Move a file to another directory:
When moving a file to another directory it is best to always include the /
at the end of the directory name, otherwise if the directory does not exist it will just rename the file.
mv -v new-file-vim.txt solo-dir-renamed/
Move multiple files to another directory:
mv -v new-file-redirect.txt new-file-redirect-command.txt solo-dir-renamed/
Move a directory to another directory:
mv -v new-dir sub-dirs-within/
Move multiple directories to another directory:
mv -v sub-dirs-within/new-dir sub-dirs-within/sub-dir ./
Notice how in this example the sub directories are being moved to ./
which represents the current directory. Which can be confirmed with the ls
command:
ls -ld new-dir sub-dir
Deleting Files & Directories
To delete files you use the rm
command and to delete empty directories you use the rmdir
command. However to delete directories that contain files or other directories you will need to use the rm -rf
command, with -r
being recursive and f
meaning force deletion. The -v
verbose flag is useful to see the actions that the rm
and rmdir
commands perform.
Deleting a file:
rm -v new-file-touch.txt.preserved-copy
Deleting multiple files at once:
rm -v new-file-touch*
Notice the use of the *
wildcard here to delete all files starting with new-file-touch
.
Deleting an empty directory:
rmdir -v sub-dirs-within
Deleting a directory that contains data:
rm -v -rf sub-dir
The output shows the deletion of files and directories.
Deleting multiple directories that contain data:
rm -v -rf new-dir solo-dir-renamed
After completing all of the exercises in this guide, the lessons directory will be empty which you can confirm by running ls -l ~/file-dir-practice
.
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.