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

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

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

The easiest to understand is the absolute path.

/home/alex/Documents/Invoice.pdf

is an example of such a path.

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

Current / Working Directory

To understand a relative path we first have to explore what the current directory means. This is also called the 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

pwd = Print Working Directory

Changing Current (Working) Directory

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

cd /var/log

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

cd ..

This will take us one directory UP.

If we were in /home/alex, this would take us into /home, which becomes the new current directory.

.. always refers to the parent directory of our current directory.

And 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 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, 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

cd 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, type

touch Invoice.pdf

This will create it inside the current directory. To create it at another location:

touch /home/jane/Invoice.pdf

or, if we’re in /home/alex, we can use the relative path to create 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

mkdir = 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/

cp = 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. That means, the directory itself, but also descend into the directory and copy 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’d 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/

mv = 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

rm = 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.

Extra 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 type

cp fstab ~

This is the same as typing

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”:

ls -l

This shows us more details for each entry, like the permissions for a file or directory, what user/group owns each entry, 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 which 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 like

-l

-h

-a

We can write:

ls -lha

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

To learn more about Linux, checkout our hands-on Linux course here

To get free hands-on experience on System Administration and DevOps tasks, please check out our KodeKloud Engineer program here