Linux commands in DevOps

Linux commands in DevOps

Basic Linux commands for DevOps Journey.

What is Linux & Why it is used?

Linux is an open-source and community-developed operating system for computers, servers, mainframes, mobile devices, and embedded devices. It has support on almost every major computer platform including x86, ARM etc, making it one of the most widely supported operating systems.

Linux is a very light operating system compared to Windows operating system, This made a great translation to containers which are even lighter than a full-blown operating system. This means for most DevOps work which requires using containers the Linux knowledge just transfers over.

Why Linux is popular?

Linux is different from the rest of the operating systems in many important aspects. Some of them are as follows

  1. Free – First, and perhaps most importantly, Linux is free. You do not have to spend any amount to download and use it, unlike windows.

  2. Open Source – Linux is open-source software. The code used to create Linux is free and available to the public to view, edit, and—for users with the appropriate skills—contribute to.

  3. Secure – Once you have Linux installed on your system, there is no need to use an antivirus! Linux is a highly secure system. Moreover, there is a global development community constantly looking at ways to enhance its security. Each upgrade makes the OS becomes more secure and robust.

  4. Stability and Performance – Linux provides very high stability i.e. it does not need a reboot after a short period. Your Linux system rarely slows down or freezes.

    You can work without any disturbance on your Linux systems. Linux provides remarkably high performance on various networks and workstations.

    Linux Commands in DevOps In this section:

    we will have a look at the most frequently used Linux commands that are used while working in DevOps.

    1. ls

This command lists all the contents in the current working directory.

syntax:

ls < flag >

ls -a > Using ‘a’ flag, lists all the hidden contents in the specified directory

ls -l \> Using ‘l’ flag, lists all the contents along with its owner settings, permissions & time

stamp (long format)

  1. cat

Concatenate, or cat, is one of the most frequently used Linux commands. It lists, combines, and writes file content to the standard output. To run the cat command, type cat followed by the file name and its extension. For instance:

cat filename.txt.

Here are other ways to use the cat command:

  • cat > filename.txt creates a new file.

  • cat filename1.txt filename2.txt > filename3.txt merges filename1.txt and filename2.txt and stores the output in filename3.txt.

  • tac filename.txt displays content in reverse order.

  1. pwd

Use the pwd command to find the path of your current working directory. Simply entering pwd will return the full current path – a path of all the directories that starts with a forward slash (/). For example, /home/username.

The pwd command uses the following syntax:

pwd [option]

It has two acceptable options:

  • -L or –logical prints environment variable content, including symbolic links.

  • -P or –physical prints the actual path of the current directory.

  1. cd

To navigate through the Linux files and directories, use the cd command. Depending on your current working directory, it requires either the full path or the directory name.

Here are some shortcuts to help you navigate:

  • cd ~[username] goes to another user’s home directory.

  • cd .. moves one directory up.

  • cd- moves to your previous directory.

  1. cp

Use the cp command to copy files or directories and their content. Take a look at the following use cases.

To copy one file from the current directory to another, enter cp followed by the file name and the destination directory. For example:

cp filename.txt /home/username/Documents

  1. mv

The primary use of the mv command is to move and rename files and directories. Additionally, it doesn’t produce an output upon execution.

Simply type mv followed by the filename and the destination directory. For example, you want to move filename.txt to the /home/username/Documents directory:

mv filename.txt /home/username/Documents.

You can also use the mv command to rename a file:

mv old_filename.txt new_filename.txt

  1. mkdir

Use the mkdir command to create one or multiple directories at once and set permissions for each of them. The user executing this command must have the privilege to make a new folder in the parent directory, or they may receive a permission denied error.

Here’s the basic syntax:

mkdir [option] directory_name

For example, you want to create a directory called Music:

mkdir Music

  1. rmdir

To permanently delete an empty directory, use the rmdir command. Remember that the user running this command should have sudo privileges in the parent directory.

For example, you want to remove an empty subdirectory named personal1 and its main folder mydir:

rmdir -p mydir/personal1

  1. rm

The rm command is used to delete files within a directory. Make sure that the user performing this command has write permissions.

Remember the directory’s location as this will remove the file(s) and you can’t undo it.

Here’s the general syntax:

rm filename

To remove multiple files, enter the following command:

rm filename1 filename2 filename3

Here are some acceptable options you can add:

  • -i prompts system confirmation before deleting a file.

  • -f allows the system to remove without a confirmation.

  • -r deletes files and directories recursively.

  1. touch

The touch command allows you to create an empty file or generate and modify a timestamp in the Linux command line.

For example, enter the following command to create an HTML file named Web in the Documents directory:

touch /home/username/Documents/Web.html

  1. df

Use the df command to report the system’s disk space usage, shown in percentage and kilobyte (KB). Here’s the general syntax:

df [options] [file]

For example, enter the following command if you want to see the current directory’s system disk space usage in a human-readable format:

df -h

These are some acceptable options to use:

  • df -m displays information on the file system usage in MBs.

  • df -k displays file system usage in KBs.

  • df -T shows the file system type in a new column.

  1. tar

The tar command archives multiple files into a TAR file – a common Linux format similar to ZIP, with optional compression.

Here’s the basic syntax:

tar [options] [archive_file] [file or directory to be archived]

For instance, you want to create a new TAR archive named newarchive.tar in the /home/user/Documents directory:

tar -cvf newarchive.tar /home/user/Documents

The tar command accepts many options, such as:

  • -x extracts a file.

  • -t lists the content of a file.

  • -u archives and adds to an existing archive file.

  1. chmod

chmod is a common command that modifies a file or directory’s read, write, and execute permissions. In Linux, each file is associated with three user classes – owner, group member, and others.

Here’s the basic syntax:

chmod [option] [permission] [file_name]

For example, the owner is currently the only one with full permissions to change note.txt. To allow group members and others to read, write, and execute the file, change it to the -rwxrwxrwx permission type, whose numeric value is 777:

chmod 777 note.txt

This command supports many options, including:

  • -c or –changes displays information when a change is made.

  • -f or –silent suppresses the error messages.

  • -v or –verbose displays a diagnostic for each processed file.

  1. history

With history, the system will list up to 500 previously executed commands, allowing you to reuse them without re-entering. Keep in mind that only users with sudo privileges can execute this command. How this utility runs also depends on which Linux shell you use.

Liuseradd, userdel commands

nux is a multi-user system, meaning more than one person can use it simultaneously. useradd is used to create a new account, while the passwd command allows you to add a password. Only those with root privileges or sudo can run the useradd command.

When you use the useradd command, it performs some major changes:

  • Edits the /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow files for the newly created accounts.

  • Creates and populates a home directory for the user.

  • Sets file permissions and ownerships to the home directory.

Here’s the basic syntax:

useradd [option] username

To set the password:

passwd the_password_combination

For example, to add a new person named John, enter the following command simultaneously:

useradd John

passwd 123456789

To delete a user account, use the userdel command:

userdel username

  1. nano, vi commands

Linux allows users to edit and manage files via a text editor, such as nano, vi, or jed. nano and vi come with the operating system, while jed has to be installed.

The nano command denotes keywords and can work with most languages. To use it, enter the following command:

nano [filename]

vi uses two operating modes to work – insert and command. insert is used to edit and create a text file. On the other hand, the command performs operations, such as saving, opening, copying, and pasting a file.

To use vi on a file, enter:

vi [filename]

Here are some shortcut keys and tricks you can use to manage the Linux system:

  • Enter the clear command to clean the Terminal screen.

  • Press the Tab button to autofill after entering a command with an argument.

  • Use Ctrl + C to terminate a running command.

  • Press Ctrl + Z to pause a working command.

  • Use Ctrl + S to freeze your Terminal temporarily.

  • Press Ctrl + Q to undo the Terminal freeze.

  • Use Ctrl + A to move to the beginning of the line.

  • Press Ctrl + E to bring you to the end of the line.

  • When executing multiple commands in a single line, use (;) to separate them. Alternatively, use && to only allow the next command to run if the previous one is successful.