Linux command line, working with files: touch, mkdir, cp, mv, rm commands
Greetings!

This is the fourth post in a series dedicated to working in the Linux command line. Today we’ll look at commands for working with files in Linux: touch, mkdir, cp, mv, rm.

If you’re just starting to learn Linux, I strongly recommend checking out the previous posts:

The practical examples in this series of posts were performed in a Linux Mint 21 distribution environment.

Join our channel: t.me/r4ven_me and chat: t.me/r4ven_me_chat on Telegram.

List of commands

Well, today our toolkit will be expanded with probably the most popular commands (not counting ls and cat) in Linux:

CommandDescription
touchcreates simple files
mkdircreates directory/-ies
cpcopies files and directories
mvmoves/renames files and directories
rmdeletes files and directories

The touch command — creating simple files

Syntax:

BASH
touch [keys] <file_name>
Click to expand and view more

We’re already a bit familiar with this command from the previous post. Today we’ll take a closer look at it.

This command is usually used to create empty files or change the timestamp of existing ones.

Frequently used keys:

KeyValue
-ato set only the access time (atime) of a file
-mto set only the modification time (mtime) of a file
-cto create a file only if it doesn’t already exist

Examples of using the touch command:

1. Creating a new file:

BASH
touch text_file.txt
Click to expand and view more

We created an empty file. Let’s run ls to check:

2. Updating the access time and modification time of a file:

BASH
ls -l text_file.txt

touch text_file.txt

ls -l text_file.txt
Click to expand and view more

Running this command on an existing file changes its timestamp, as can be seen in the screenshot above.

3. Creating a new file with specific timestamps:

BASH
touch -t 202301011200 time_text_file.txt

ls -l time_text_file.txt
Click to expand and view more

Here we created a new file with a specific timestamp.

For more detailed information about the touch command, you can refer to the official documentation or use the man touch command in the Linux terminal.

The mkdir (make directory) command — creating directories

Syntax:

BASH
mkdir [keys] <directory_name>
Click to expand and view more

Frequently used keys:

KeyValue
pcreates parent directories as needed
voutputs a message for each operation performed
msets file access permissions (like in chmod)

Let’s create a directory for testing, then verify its existence with the ls command and change into it using cd. Then let’s check the current working directory with the pwd command. Let’s run four commands in the terminal one after another:

BASH
mkdir testdir

ls

cd testdir

pwd
Click to expand and view more

If you’re not familiar with the commands mentioned, please read my previous posts on the Linux command line.

Great. Now let’s create another directory inside the current one, adding the -v (verbose) key to the command and look at the command’s output:

BASH
mkdir -v testdir2
Click to expand and view more

We see that the shell outputs a message about the result of the command’s execution.

Now let’s create several nested directories with one command using the -p (parents) key:

BASH
mkdir -vp testdir3/testdir4
Click to expand and view more

As you can see, the command created two directories: testdir3 in the current directory and testdir4 inside testdir3 using the -p key.

If you don’t use the -p key when creating nested directories, the command will return an error and create nothing.

As you may have noticed, by default, if you don’t specify an address for creating a new directory, the mkdir command creates it in the current working directory.

Let’s try to create two directories at once somewhere else, for example let’s create directories testdir5 and testdir6 in our home directory while being in the testdir directory. To do this, we need to specify the path before the name of the directory being created:

BASH
mkdir -v /home/ivan/testdir5 ~/testdir6
Click to expand and view more

As we can see, we passed two new directories as arguments to the mkdir command, specifying the path to our home directory using two different methods. Then, being in the /home/ivan/testdir directory, we output the contents of the parent directory by specifying the relative path .. as an argument to the ls command.

We’ll talk about the -m key another time, when we get into file permissions in Linux.

The cp (copy) command — copying files and directories

Syntax:

BASH
cp [keys] <name_of_file_being_copied> <new_file_name>
Click to expand and view more

Frequently used keys:

KeyValue
-voutputs the actions being performed to standard output
-iinteractive mode (asks before overwriting a file)
-rcopies directories recursively
-pcopies a file while preserving its attributes (owner, group, permissions)

For practice, let’s copy the text file we created text_file.txt into the testdir directory, first returning to the home directory:

BASH
cd

ls

cp text_file.txt testdir

ls -l testdir
Click to expand and view more

Now let’s create and copy the file text_file2.txt into the testdir directory using the -v key:

BASH
touch text_file2.txt

cp -v text_file2.txt testdir
Click to expand and view more

We see that the shell clearly displayed the details of the actions being performed.

You can also pass several files as arguments to the cp command.

Let’s go into the testdir directory, create a new directory testdir2 and copy the files text_file.txt and text_file2.txt located in the current directory into testdir2:

BASH
cd testdir

ls -l

cp -v text_file.txt text_file2.txt testdir2/
Click to expand and view more

Let’s check the contents of the testdir2 directory:

BASH
ls -l testdir2
Click to expand and view more

As we can see, we successfully copied the files.

Now let’s try to copy the testdir2 directory:

BASH
cp testdir2 testdir2copy
Click to expand and view more

As we can see, we got an error. The fact is that directories need to be copied with the cp command using the -r key. The screenshot below shows an example of running the cp -r command after getting the error.

BASH
cp -r testdir2 testdir2copy
Click to expand and view more

This command has one feature you should always remember. By default, when copying files/directories with cp, if you copy files with the name of already existing files, they get overwritten! I.e. the existing file is deleted, and the newly created copy takes its place.

Be very careful when copying any important files.

To avoid accidental overwriting, you need to use the -i key, which will ask you for permission to overwrite a file if files with an identical name already exist in the destination directory.

A simple example. Being in the testdir directory let’s try to copy the file text_file.txt into testdir2:

BASH
cp -vi text_file.txt testdir2
Click to expand and view more

If a file with the name of the copied file exists in the destination directory, the shell will ask us for confirmation to overwrite. The answer to the request should be y or yes to confirm overwriting, and correspondingly n or no to interrupt the copy operation.

As the last example let’s look at the cp command with the -p key, which preserves the original file attributes.

For a clear demonstration, let’s copy the file text_file.txt to the current directory as the root user (entering the admin password after entering the first command), and then run the same command with the -p key:

BASH
sudo cp -v text_file.txt root_file.txt

sudo cp -vp text_file.txt root2_file.txt

ls -l
Click to expand and view more

As we can see, with normal file copying, the owner becomes the user under whose name the copy was performed.

Note that the access permissions of the file also changed, globally set for the root user by default -rw-r--r--.

But when using the -p key, the original attributes were preserved.

An interesting point: if you try, as a regular user, to copy a file owned by root while preserving attributes, the new file’s owner will be the user under whose name the command is running, not the original owner. This is because only root itself can assign root as owner and owner group.

The mv (move) command — moving/renaming files

Syntax (renaming):

BASH
mv [keys] <file_name> <new_file_name>
Click to expand and view more

Syntax (moving):

BASH
mv [keys] <path/file_name> <new_path/file_name>
Click to expand and view more

Frequently used keys:

KeyValue
-voutputs the actions being performed to standard output
-iinteractive mode (asks before overwriting)
-uupdate, moves only if the SOURCE is newer than the destination file

In Linux, the same command is used for renaming and moving files. To rename a file, you need to pass the current file name and the new file name as arguments, while being in the same directory.

To move a file, you need to specify the current file name and its new location. Let’s practice.

Let’s open our file text_file.txt in a text editor and fill it with any content. For example, I’ll add “Hello R4ven.me!!!” to it

BASH
xed text_file.txt
Click to expand and view more

Now, being in the testdir/ directory, let’s rename the file text_file.txt to new_text_file.txt using the -v key, which has the same effect as with the cp command:

BASH
cat text_file.txt

mv -v text_file.txt new_text_file.txt

ls -l

cat new_text_file.txt
Click to expand and view more

To confirm this is the same file, we output its contents with cat.

The -i key of the mv command is also analogous here to the cp command’s key: it triggers interactive mode in case your command would overwrite a file.

There’s also an interesting -u (update) key, which, in case of file overwriting, compares the timestamp of the file you want to move with the file that would be overwritten. If the timestamp of the file being moved is newer, it will overwrite the file in the destination folder. Otherwise, the command does nothing.

For demonstration, let’s take our file new_text_file.txt and copy it into the testdir2 directory:

BASH
cat new_text_file.txt

cp -v new_text_file.txt testdir2
Click to expand and view more

Then let’s open our file new_text_file.txt in the current directory in a text editor and change the content to “Bye R4ven.me!!!” using xed:

BASH
xed new_text_file.txt
Click to expand and view more

Now let’s transfer the file testdir2/new_text_file.txt back into our current directory testdir using the -u keys:

BASH
mv -ivu testdir2/new_text_file.txt .
Click to expand and view more

Note that as the destination path we use the dot . which denotes the relative path of our current directory.

As we can see, our current file new_text_file.txt hasn’t changed. Because the timestamp of the file in testdir2/new_text_file.txt is newer, and with the -u key, no overwriting occurred.

Let’s do the move the other way around: let’s move the recently modified file new_text_file.txt into testdir2 with the same keys:

BASH
mv -ivu new_text_file.txt ./testdir2/

cat testdir2/new_text_file.txt
Click to expand and view more

Now the command with the -u key detected that the destination file was older than the one being moved, and the -i key kicked in, asking us for confirmation to overwrite the destination file. We press y or yes and check the file’s content, for verification.

By the way, you can update a file’s timestamp using the touch command we already know, passing it the name of the target file as an argument:

BASH
ls -l new_text_file.txt

touch new_text_file.txt

ls -l new_text_file.txt
Click to expand and view more

The rm (remove) command — deleting files and directories

Syntax (deleting a file):

BASH
rm [keys] <name_of_file_to_delete>

# or

rm [keys] </path/to/file_to_delete>
Click to expand and view more

Frequently used keys:

KeyValue
-voutputs the actions being performed to standard output
-iinteractive mode (asks before deleting)
-rrecursively deletes directories and their content
-fignores non-existent files and arguments, performs deletion without asking anything

We’ve already learned how to create files, now let’s talk about deleting them.

Please be careful with the rm command, since files deleted with it are permanently removed from the system. This command doesn’t imply any “recycle bin,” like in Windows. It’s possible to recover files deleted with rm, but it’s not that simple and without any guarantees. Always use the -i key for interactive deletion with confirmation.

So, being in the ~/testdir directory let’s delete the existing file text_file2.txt there:

BASH
ls -l

rm text_file2.txt

ls -l
Click to expand and view more

As we can see the file was deleted. Now let’s delete the file testdir2/new_text_file.txt using the -v key:

BASH
rm -v testdir2/new_text_file.txt
Click to expand and view more

We see detailed output.

The -i key here has the same effect as with the cp and mv commands:

BASH
ls -l

rm -vi root2_file.txt
Click to expand and view more

To cancel the deletion press n or no.

Now let’s try to delete the testdir3 directory:

BASH
rm -iv testdir3
Click to expand and view more

We got an error:

The -r key is required for deleting directories. Let’s try with it:

BASH
rm -ivr testdir3
Click to expand and view more

We see that the -i and -v keys work, asking us for confirmation to perform the actions and displaying detailed information about them.

To quickly delete a directory just use -r. Let’s delete testdir2copy:

BASH
rm -r testdir2copy
Click to expand and view more

Now about the -f (force) key.

We have a file root_file.txt owned by root, and write permissions for this file for other users, including us, are absent. Let’s try to delete this file:

BASH
ls -l

rm -v root_file.txt
Click to expand and view more

The command warns us that the file is write-protected and asks for a warning confirmation. Let’s decline deleting this file by pressing n and try again but with the -f key:

BASH
rm -vf root_file.txt
Click to expand and view more

You’ve probably often seen jokes about the rm -rf / command — never run it, especially as the root user.

Given the information provided above, we now understand what it does:

rm — the delete command

-rf — recursively deletes directories with their content, ignoring all warnings and errors

/ — this is the path denoting the root of our system.

I.e. this command deletes the content of our entire system. Most modern distributions add protection against running this command in the form of a warning or an additional key, but it’s still possible to run it, since the system fully belongs to the user, i.e. to you and me :) and that’s cool.

For example, in Linux Mint this command won’t run without a certain key:

Afterword

Today we looked at some of the most popular and most useful commands in the world of the Linux console — commands for working with files. We learned how to create, copy and delete files and directories. We also discussed cases of how deleting the system NOT to do it.

In the next post we’ll talk about a specific type of files in Linux — links. How hard links differ from symbolic links and what advantages and disadvantages each type has.

Useful sources

Previous posts in the series

Copyright Notice

Author: Ivan Cherniy

Link: https://r4ven.me/en/linux/komandnaya-stroka-linux-rabota-s-fajlami-komandy-touch-mkdir-cp-mv-rm/

License: CC BY-NC-SA 4.0

Blog materials may be used with attribution to the author and source, for non-commercial purposes, and under the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut