Running Simple Commands On Linux

image.png

Over the past week, my SCA-Cloud school tasks have gradually gotten tougher. We were told to do various tasks, these included creating users and groups, assigning the users into groups and giving them access based on the group they belong to. I used Linux (Ubuntu 20.04) to perform these tasks. Although I found it quite challenging at first, I thoroughly enjoyed the tasks. So, just to let you in on some of the things I have learnt, I would be writing about some commands in Linux! Let's go!

"Linux is a beast, and there is a great deal you can do with it." -Natasha Postolovski

pwd

The first thing you would definitely like to know when you find yourself on a Linux server is what part of the server you are. You can do this by using the command pwd meaning 'print working directory'. This would show the location or path of the directory you are currently in.

adenicole@nicole:~$ pwd
/home/adenicole                              # Output

Now, let us analyse the adenicole@nicole:~$ pwd

  • adenicole shows the name of the user currently logged in to the Linux machine or server.
  • nicole shows the Linux machine, host, or server name.
  • The tilde symbol ~ represents the home directory of the currently logged-in user i.e. /home/adenicole.
  • The dollar symbol $ shows that you are a normal user logged into the Linux terminal. Commands are usually written after the dollar sign. It is replaced with a hash symbol # when a root/admin/superuser is logged in.
  • pwd is the command to display the current working directory.

cd

The next thing is moving around your terminal. You do this by using the command cd meaning "change directory." Typing just cd moves you to the home directory. To move to a specific directory, paths are added; note that paths are separated with a forward slash / after each directory name.

adenicole@nicole:~$ cd /home/adenicole/bleach
adenicole@nicole:~/bleach$

ls

ls is used to list things in files and directories and sometimes options are passed to get more information about them. To get the list in a specific directory the path is passed as an argument. Note that the outputs of the ls command` are listed in alphabetical order.

adenicole@nicole:~$ ls
bleach/   pictures/    portfolio/   tasks

This home directory currently has 3 folders and a file. adenicole@nicole:~$ ls [option] [files] is how a basic ls command is passed. Multiple directories or files can be passed as arguments with space.

adenicole@nicole:~$ ls [option] [file1] [file2]

We use ls -l i.e passing the option -l to the command ls to show us the long list format of the files and directories.

adenicole@nicole:~$ ls -l
total 0
drwxrwxrwx  1 adenicole nicole 0 Feb  9  2:15 bleach/
-rwxr-x---  1 adenicole nicole 0 Feb  9  2:16 pictures/
-rwxrwxrwx  1 adenicole nicole 0 Feb  9  2:16 portfolio/
-rwxrwxrwx  1 adenicole nicole 0 Feb  9  2:17 tasks
  • The total represents the total disk allocation of the directory.
  • The first d shows us the file is a directory, this may come in the form of a dash - meaning regular file, b(block character file), c(special character file), l(symbolic link) etc.
  • Followed by the 9 alphabets representing permission r-read,w-write,x-execute for the user, group and other.
  • 1 shows the link count to the directory.
  • adenicole represents the file owner.
  • nicole represents the group the file belongs to.
  • 0 shows the file size.
  • Date and time.
  • directory or file name.

Other options passed to the ls command includes:

  • -a which shows hidden files (represented with a dot).
  • --sort which manipulates the output by extension, version, time and size.
  • -R which shows the content of the subdirectories recursively.

mkdir

You use the command mkdir meaning "make directories" to, just as the name implies, make directories or folders. Please note, the command has no space in between; it is mkdir not mk dir. I learnt this because I kept on making the mistake and it kept throwing the mk: command not found error. See how a folder named bleach is created below:

adenicole@nicole:~$ mkdir bleach
adenicole@nicole:~$ ls bleach
bleach/                        # Output

touch

The touch command is used to create files.

adenicole@nicole:~/bleach$ touch ichigo.txt
adenicole@nicole:~/bleach$ ls ichigo.txt
ichigo.txt                        # Output

This command creates a text file named “ichigo” in the bleach directory.

cp

The cp command copies file using this format from left to right as shown below:

adenicole@nicole:~$ cp [file1] [file2]

mv

The mv command is used to move a file/directory from one location to another. The files/directories being moved can be renamed in the process. The file to be renamed is passed in as the first argument.

adenicole@nicole:~$ mv [file1] [file2]

rm

The rm command is used to delete files. Options like -f can be passed to forcefully delete files; this option should be used with care as whole filesystems can be lost this way. The -r option is used when deleting a directory as this allows the rm command to be able to go into lower subdirectories and delete them along with their containing file(s). In the example below, the command would permanently delete the directory bleach and its subdirectories/files.

adenicole@nicole:~$ rm -rf bleach

cat

This command is called the concatenate command it is used to view the content of a file. Line 1 is the command being passed. line 2-4 is the result.

adenicole@nicole:~$ cat tasks
<!DOCTYPE html>
<h1> Hello, world!<h1>
</html>

grep

This command is called the “Global Regular Expression Print.” It is a special tool that is used to search for and print contents of a file or command’s output that matches the pattern provided to grep. Below, the cat command is passed the tasks file as an argument and the output of the command is piped | (chained) to the grep command to find hello.

adenicole@adenicole:~$ cat tasks | grep hello

find

This is one life-saving command. This helps to locate a file in a large directory.

adenicole@nicole:~$ find -name tasks

All files bearing tasks would be shown.

sudo

The sudo, meaning “superuser do,” command is used to run commands with root/admin/superuser level access. For example, when a normal user needs to change the permission of a file they normally would have no access to, `sudo is used.

adenicole@nicole:~$ sudo chown -R ichigo:anime /home/adenicole/bleach

where:

  • ichigo becomes the new file owner.
  • anime becomes the new group owner.
  • /home/adenicole/bleach as the path(directory).

man and help

The commands -man and --help show more about a given command and how to use it.

What command is your favourite? Do comment below. There are so many other Linux commands not mentioned here. Do check on Ryan tutorials, basic Linux commands for beginners, linux-commands-cheat-sheet or 10 things every Linux beginner should know to get more Linux commands.