Simple Linux Commands

Simple Linux Commands



Getting into Linux, it is primordial to learn the basics of the Linux shell, a program that takes commands from the keyboard and gives them to the operating system to perform. This blog serves to demonstrate some simple Linux commands as a learning tool.

We use a program known as the terminal(terminal emulator) which allows us to interact with the shell.
The 4 commands that will be demonstrated are: ls, cd, pwd and mkdir.

When opening the Linux terminal,
the last character of the shell prompt should be "$" sign, however if you have the "#" sign instead, this can be quite dangerous since we are operating as a superuser; someone with admin privileges and we can delete or overwrite any file on the system.

ls - List
This command will list all the files and directories of a specified directory in alphabetical order by default.

There are multiple flags that can change the behavior of a command and some common flags are:

--help : when used as an argument, it displays all options and what can be run for that command, in this case, the command is ls


/ : Lists contents of the root directory

    If we want to list the files in tmp:


.. : Lists contents of parent directory(One level above)

    Let's say we are in a different directory
    "ls .." now yields a different result because in folder1 there were 3 folders


* : Lists contents of the directories and sub directories (Lists subdirectories)



~ : Lists contents in the home directory of the user

The above arguments are universal in Linux, they can be used on other commands.

These are other arguments that allows the user to refine the data in the list 

-a : Lists hidden files (They start with .)
-S : Lists the directories and their size
-l  : List the files and gives detailed information of directories such as permissions
-t : Sorts by date last modified
-r : Sorts the file in reverse order
-lh : Displays the file size in a readable format using prefixes
-R : Display files recursively 
-F : Displays files ending with '/' (Folders)
-d : Displays only the directory itself, not the content
-i : Displays the inode (the data structure that keeps track of all the files and directories within the Linux filesystem) number of the files and directories
-n : Displays the the UID and GID of files and directories
--v : Displays the version of ls(can sometimes be used on other commands as well)

An example of these arguments is
-R displays the files inside the folders within the specified directory


One of the key aspects of the Linux command line, is that the flags can be combined together for a particular need

One example would be
ls -tr
which would sort by oldest date modified

A second example would be
ls ../.. 
which lists contents 2 levels above

A third example is
-d */ : Displays only directories inside of that directory


Adding colors to output
ls -- color
by default this is already on

Please note that flags and directories are case-sensitive.

cd - Change Directory
This command is used to navigate through directories, by default cd will change to the home directory. Some common parameters are:
<name of the directory> : goes to that directory name if it is found in the current directory
~ : Also goes to the home directory
/ :  goes to the root directory
- : goes to previous directory
.. : goes to parent directory(one level above)

By combining arguments, cd also allows you to go to custom directories
cd <folder>/<subfolder>

Here's a demonstration of some arguments


pwd - Print Working Directory
This command is used print the full name of the current directory which can be useful in navigating directories.

When this command is executed, it will have 2 exit statuses:
0 : Success
Non-zero : Failure

pwd does take some arguments;
-L : prints the $PWD environment variable contents, including symbolic links(shortcut to directories, similar to windows shortcuts) and the full path of the target directory
-P : prints the full path of the target directory without any symbolic links

pwd takes the -L argument by default


mkdir - Make Directory
This command allows the user to create directories in a directory and can take 2 arguments
mkdir [options...] [directories]

A simple application of the mkdir command:


What this has effectively done is create folder1 inside Documents, and folder2 inside folder1



mkdir can take the following arguments:

-v : verbose, displays a message for every directory created



-p : creates a parent directory or directories for a file.
        Let's say we want folder3 to contain folder4 and folder4 to contain folder5
        Let's first delete all the directories in folder2
        We will then execute this command; creating multiple parent folders.


    folder5 is now in the directory desired.

-m : mode, this is used for setting up permissions, by default rwx(read, write, execute) permissions are given to the current user only.

To give rwx to all users, the user 777 is added onto the mode

The permissions(encased in the rectangle) are different.

For further documentation on these 4 commands,
$ <command name> --help and $ man <command name>
can provide more information about the command.


These were 4 basic, yet simple Linux commands that are necessary in operating the Linux command line.

Comments

Popular posts from this blog

Version Control Basics

Installing Ubuntu on Windows 11 with python support on VirtualBox