Linux - Create, Delete, Copy, and Move Files and Directories

The Linux file system is a popular choice for high-performance computing environments due to its efficiency. Its design provides fast and reliable data access while ensuring data integrity.

In this blog, we will see how to create, delete, copy, and move files and directories in Linux.

Filesystem Tree

Linux organizes files and directories in what it calls the filesystem tree. Why is it called this way? Because just like a tree we’d see in nature, this also has a root, branches, and leaves. Except, Linux’s filesystem tree is inverted. The root is at the top, and its branches and leaves “grow” downwards.

The root directory is /. This is the top-level directory, there can be no other directories above it. Under / there are a few subdirectories like bin, etc, home, usr, and so on. These subdirectories may also contain other subdirectories themselves. To access a file or directory on our command line, we must specify its file path or directory path. This path can be written in two different ways, absolute path or relative path.

What is an Absolute Path in Linux?

The easiest to understand the absolute path is by using an example. Below is an example of an absolute path.

/home/alex/Documents/Invoice.pdf

Absolute paths always start out with the root directory /. Then we specify the subdirectories we want to descend into, in this case, first home, then alex, then Documents. We can see the subdirectory names are separated by a /. And we finally get to the file we want to access, Invoice.pdf.

An absolute path can end with the name of a file, but also with the name of a directory. If we’d want to delete the Documents directory, we’d specify a path like

/home/alex/Documents

What is a Relative Path in Linux?

A relative path is a file or directory path relative to the current directory. It specifies the location of a file or directory relative to the current directory, rather than specifying the location starting from the root directory.

To understand a relative path better, let’s look at what the current directory is. Note that the current directory is also known as a working directory.

When we’re working at the command line, we’re always “inside” a directory. For example, if we log in as the user “alex” on some server, our starting current directory might be /home/alex. Every user starts in its home directory when they log in. john might have it at /home/john, and root (the super user/administrator) has it at /root.

To see our current (working) directory we can type:

pwd

‌Note that pwd stands for 'Print Working Directory.'

Changing Current (Working) Directory

To change our current directory, we use the cd command (change directory).

cd /var/log

The command above would change our current directory to /var/log. We used an absolute path here. But we can also change the directory this way:

cd ..

The command above will take us one directory UP. If we were in /home/alex, this would take us into /home, which becomes the new current directory. Note that . always refers to the parent directory of our current directory. This was an example of using a very simple relative path. Let’s dive deeper.

Let’s imagine our current directory is /home/alex. With relative paths, we can refer to other places in one of these three main ways:

  • Locations “under” our current directory, e.g., Documents/Invoice.pdf. Since we’re in /home/alex, typing a path like Documents/Invoice.pdf is like typing /home/alex/Documents/Invoice.pdf. Our relative path “gets added” to our current directory and we get to our PDF file.
  • Locations in our current directory. Typing Invoice.pdf will access the file at /home/alex/Invoice.pdf
  • Locations above our current directory. Typing ../Invoice.pdf points to the file at /home/Invoice.pdf. Since we used ../ we basically said, “Go one directory up”. We can use .. multiple times. ../../Invoice.pdf points to the file at /Invoice.pdf. The first .. “moved” the relative path at /home, and the next .. moved it at /.

Extra tips:

If you’re in /var/log currently and you move to / with:

cd /

You can return to your previous working directory with:

cd -

It will take you back to /var/log.

If you’re in /var/log and you want to return to your home directory /home/alex or /home/john or whatever it may be, use:

cd

The command above without any options or paths after it will always take you back to the home directory.

Ok! Now let’s move on to the easy stuff.

Creating Files and Directories

To create a new file inside the current directory, use this command:

touch Invoice.pdf

To create a new file at another location, use this command:

touch /home/jane/Invoice.pdf

or, if we’re in /home/alex, we can use the relative path to create the file in /home/jane:

touch ../jane/Invoice.pdf

All the commands we’ll discuss accept both absolute and relative paths, so we won’t mention these alternatives for each one. Just know that after the command, you can use any kind of path you want.

To create a new directory:

mkdir Invoices

Note that mkdir stands for 'make directory.'

Copy Files and Directories

To copy a file, we use the cp command, followed by the path to the file we want to copy (source), then the path to the destination where we want to copy it - cp source destination.

To copy Invoice.pdf to the Invoices directory:

cp Invoice.pdf Invoices/

Note that cp stands for 'copy'

Notice how we terminated the path to the Invoices directory with a /, to make it Invoices/. cp Invoice.pdf Invoices, without the / would have worked too. But it’s good practice to end your directories with a /. This way, you’ll form a healthy habit and get a visual indicator that tells you when Invoices (without /) might be a file, and Invoices/ might be a directory.

To copy Invoice.pdf to the Invoices directory, but also choose a new name for it:

cp Invoice.pdf Invoices/InvoiceCopy.pdf

To copy the Invoices directory to the Documents directory:

cp -r Invoices/ Documents/

The -r is a command-line option (also called command line flag) that tells cp to copy recursively. It directs the copying of the directory itself, and everything else it contains - files, other subdirectories it may have, and so on.

Just like we did with the file, we can also copy and rename:

cp -r Invoices Documents/BackupOfInvoices/

So instead of the copy ending up at /home/alex/Invoices, it will now end up at /home/alex/BackupOfInvoices.

The name you choose for your cloned directory must not exist at your destination. For example, if we already have a directory at /home/alex/BackupOfInvoices, this would just move Invoices there and it would end up at Documents/BackupOfInvoices/Invoices/.

Move Files and Directories

To move a file to the Invoices directory we use:

mv Invoice.pdf Invoices/

Note that mv stands for 'move'. To rename a file:

mv Invoice.pdf OldInvoice.pdf

To move the Invoices directory to the Documents directory:

mv Invoices/ Documents/

Note that mv does not need the -r recursive option.

To rename a directory:

mv Invoices/ OldInvoices/

Deleting Files and Directories

To delete a file:

rm Invoice.pdf

Note that rm stands for 'remove.'

To delete a directory:

rm -r Invoices/

Once again, the -r option was used to do this recursively, delete the directory, then all of its subdirectories and files. When you copy or delete directories, remember to always add the -r option.

Tip: If you want to quickly refer to your home directory without typing /home/alex or /home/john or whatever it might be, you can use the ~ sign. For example, let’s say you’re in /etc and you want to copy the file called fstab.

You can run this command:

cp fstab ~

This is the same as running the command:

cp fstab /home/alex

Wherever you put ~, it’s the same as typing the full path to your home directory, /home/alex.

If you’d want to copy it to /home/alex/Documents, you’d use:

cp fstab ~/Documents

Listing Files and Directories

To list files and directories in your current (working) directory:

ls

On Linux, files and directories can have a name that begins with a . Example: the “.ssh” directory. These won’t be displayed by a simple ls command. They are, in a way, hidden.

To list all files and directories, even the ones beginning with a .

ls -a

-a comes from the word all.

Of course, to list files and directories from a different location, we just type the directory path at the end of ls.

ls /var/log/

or

ls -l /var/log/

To list files and directories in a different format, called a “long listing format”, add the -l flag as shown in the command below:

ls -l

This shows us more details for each entry, like the permissions for a file or directory, what user/group owns each entry, and when it was last modified.

We can combine the -a and -l command-line options like this:

ls -l -a

or like this

ls -la

or

ls -al

This will display entries in long listing format and also show us “pseudo-hidden” files and directories that have a name beginning with a .

The last form is preferred as it’s faster to write it.

There’s also a command-line option, -h that shows sizes in “human-readable format”: bytes, kilobytes, megabytes, and so on. This has to be combined with the -l option. If we want to use three options: -l, -h, and -a.

We can write:

ls -lha

l, h, a can be typed in any order.

Enroll in our Linux Basics Course to learn more Linux concepts.

Learning Linux Basics Course & Labs | KodeKloud

Conclusion

Knowing how to create, delete, copy, and move files and directories in Linux is essential for effective and efficient use of the operating system. These tasks are fundamental to managing files and directories on your computer and can save you time and effort.


More on Linux: