Linux: Commands and Filesystems

In this article, we will look into what are some of the basic commands used in the Linux system and what are the different types of filesystems that are there in it.

What are some of the basic commands used in Linux?

a. whoami: prints username.

b. pwd: shows our current working directory

c. ls: lists the contents of the current directory

d. cat: Primarily used to read a file, but there are other functions too like concatenation, appending, and overriding that we'll see in much more detail when we reach there. Till then, sit back, relax and enjoy what follows next in this blog post.

Also, if you're on a vagrant VM, then the way to switch from regular user to root user, use the command sudo -i.

Command Structure:

Consider the following command:

[vagrant@local_host ~] $

where,

vagrant: username

local_host: hostname

~: suggests that we're in the home directory

$: regular user

Now, if you're a root user, then everything remains the same except your username changes to root and $ gets replaced by #.

Let's move on.

What are the commands to create a directory and a file?

Command to create a directory: mkdir <directory name>

Command to create a file: touch <file name>

Command structure syntax:

For one command, there could be many options and it is next to impossible to memorize all of them.

syntax: command options arguments

Eg: cp -r Dir01 Dir06

where, cp is command, -r is an option and Dir01 Dir06 is an argument.

There's always help available at your fingertips.

Eg: cp --help( can be used with other commands as well and not just limited to cp command) will show the usage and all the valid options for the command.

What is the command to copy a file?

This can be achieved via a relative path or an absolute path using the cp command.

The relative path is used or works if you're executing the command within the same directory.

The absolute command comes in handy if you wanted to execute commands from anywhere in the filesystem.

Let's understand this furthermore with the following example.

[vagrant@loc_host ~]$ touch file{1..5}

[vagrant@loc_host ~]$ ls

output: file1 file2 file3 file4 file5 DIR1

So, suppose we want to copy the file 'file' to the directory named DIR1, we make use of the command cp file1 DIR1.

Notice that since DIR1 is the sub-directory inside of our home directory, the relative path works to copy 'file1' to DIR1.

But, if DIR1 was a part of say /etc directory, you'd then need to use the cp command with an absolute path shown below:

[vagrant@loc_host ~]$ cp /home/vagrant/file1 /home/etc

where,

home/vagrant/file1: Source path

/home/etc: Destination path

It's a good practice to use an absolute path even if you're located in the same directory to get used to the file system structure

How do we copy a directory in Linux?

It's almost similar to how you did it in the case of a file. The only difference here is the use of the option -r as in recursive. We can copy a directory in Linux only if we use this option. Let's understand using the example below.

[vagrant@loc_host ~]$ cp -r dir1 dir10

Note: This will work provided both are present in the same main directory as you're copying the directory using the relative path.

What will happen if we do not use the -r option while copying a directory?

Let's consider the following command.

[vagrant@loc_host ~]$ cp dir1 dir10

The above command will throw the error: cp: omitting directory 'dir1'

The only solution to this is if you make use of the option -r.

How do we move or rename files?

You can move or rename any file in Linux by using the mv command. mv works similar to the 'cut and paste' that you do on a windows machine.

To move a directory, there's no need of using the recursive option as in the case of the cp command.

[vagrant@loc_host ~]$ mv dir1 dir10

Note: This will work provided both are present in the same main directory as you're moving dir1 to dir10 using the relative path.

We use the mv command to rename a file too.

[vagrant@loc_host ~]$ mv dir1 mydir

Here, dir1 was renamed to mydir

Since you have come this far in this blog post, I must let you in on one awesome trick. Copying or moving multiple files could be achieved by using the '*' symbol.

Eg: [vagrant@loc_host ~]$ mv *.txt mydir

The above command will move all the files with the .txt extension to mydir without having to move them one by one. The same can be done with cp command.

How do we remove a file or a directory?

You can remove a directory by making use of rm command. use the -r option to remove the directory.

command to remove a directory: [vagrant@loc_host ~]$ rm -r mydir

command to remove a file:[vagrant@loc_host ~]$ rm file2

What if I want to create multiple files or directories at once?

You should make use of the curly brace {}.

Eg:

[vagrant@loc_host ~]$ touch file{1..5}

[vagrant@loc_host ~]$ ls

output: file1 file2 file3 file4 file5

The same applies to creating a directory by replacing touch with mkdir.

* has many other use cases as well. Suppose you're in a directory and you want to delete all of its contents. The following command would do the trick.

[vagrant@loc_host ~]$ rm -r *

However, if you unknowingly commit the rm -r * command in a directory that has some important file or directories, there's no rolling back.

The only way to get the files back is the rough hard disk restoration which could be complicated and there's no guarantee that 100% of your files will be restored.

Let's wrap it up for today and get back with another article on Linux tomorrow. I sincerely hope that I have added value to you with this blog post. I yes, well and good. If not, let me know. I'll make the necessary adjustments and improvements in my future article.