Mastering the Linux Command Line: Your Ultimate Cheat Sheet for Seamless Operations

As programmers, we spend most of our time writing code to build software. However, when shifting that code to production, our coding skills may not be sufficient. In such cases, we rely on commands to accomplish various tasks, such as connecting to remote servers using SSH, checking file permissions, and editing/updating files on the server.

In this blog post, we will explore some of the most commonly used commands in Linux-based systems that will make your deployment process a bit easier (although not necessarily a piece of cake 😜!).

Since this article is a bit longer, I have divided it into the following sections.

  1. File commands

  2. Search Commands

  3. System commands

  4. User commands

  5. Permission commands

  6. Process commands

  7. Network and storage commands

  8. Compression commands

  9. SSH and File transfer commands

File commands

As the word implies, this is everything to do with files and directories such as listing directories, printing the working folder, changing the folder, etc.

  • pwd Stands for Print Working Directory. Prints the path of the current working directory starting from the root

  • ls Displays the files and directories of the current working directory.

  • ls Documents Displays the contents of the Documents directory

  • ls / Displays the contents of the root directory

  • ls .. Displays the contents of the parent directory(one level above)

  • ls ../.. Displays the contents of the parent directory(two levels above)

  • ls ~ Displays the contents of the user’s home directory (/home/user)

  • ls -d */ Displays all the folders of the current working directory

  • ls * Displays the contents along with the contents of subdirectories

  • ls -R Displays the files and directories in the current working directory, including their corresponding sub-directories down to the last file.

  • ls -s Displays the content with the size.

  • ls -l Displays the detailed content information, including file and directory permissions, the number of links pointing to the content, the user and group associated with the content, as well as the last modified date and time.

  • ls -lh Similar to the above command, It represents the size of files and directories in a readable format, such as 100B for bytes or 4.0K for kilobytes.

  • ls -a Displays the contents including hidden files

  • ls -l -a or ls -a -l or ls -la or ls -al Displays the contents including hidden information

  • ls -t Displays the contents sorted by last modified date in descending order.

  • ls -tr Displays the contents sorted by last modified date in ascending order

  • ls -S Displays the contents sorted by size in descending order

  • ls -Sr Displays the contents sorted by size in ascending order

  • ls > output.txt Displays contents and directories and writes to output.txt file (Note: You can use any of the above-discussed flags and the ls command. such as -s, -R -t etc.)

  • mkdir test Creates a test directory in the current folder

  • cd test Changes the current working directory to test

  • cd .. Move up to one directory

  • cd / Takes to the root directory.

  • cd Takes to the home directory

  • cd ~ Takes to the home directory

  • clear Clears the terminal

  • touch file1.txt Creates a file1.txt file in the current working directory

  • nano file1.txt Opens the file1.txt file in the nano editor

  • cat file1.txt Shows the contents of file1.txt

  • less file1.txt Shows less content of file1.txt (Hit q to exit)

  • mv file1.txt file2.txt Renames file1.txt to file2.txt

  • cp file1.txt file2.txt Copy the contents of file1.txt to file2.txt

  • cp file1.txt ~/dir2/file2.txt Copy a file to a different path

  • rm file1.txt Deletes a file

  • rmdir dir2 Deletes a directory

  • rm -R dir2 Deletes a directory with files

Search Commands

In this section, you will explore the commands that would help you to search for specific files, directories, or content within files

  • grep "five" file1.txt Search the text five in file1.txt

  • grep "five" blog/posts/file1.txt Search the text five in blog/posts/file1.txt

  • grep -r "five" blog Search in the blog directory and its child contents for text five

  • find -name file1.txt Search the file file1.txt in the current working directory

  • find blog/posts -name 'file1.txt' Finds the file file1.txt in the blog/posts directory

  • find blog/posts -name 'file*' Finds the files prefix with ‘file’ in the blog/posts directory

  • find -size +100M Finds the files which have more than 100 MB in size

System commands

Linux provides a wide range of system commands that allow users to interact with their operating system, gather information, and perform various administrative tasks. These commands play a crucial role in managing and maintaining a Linux system effectively.

  • uname -a Displays Linux system information

  • uname -r Displays kernel release information

  • uptime Displays how long the system has been running and the load average.

  • uptime -p Same as above but displays the output in a pretty format

  • uptime -s Displays how long the system has been up

  • hostname Displays the system hostname

  • hostname -I Display all local IP addresses of the host

  • last reboot Displays the system reboot history

  • date Displays the current date

  • cal Displays the calendar

  • w Displays the users who are online

  • whoami Displays the logged-in user

  • history Shows the audit of the user

  • which curl Shows the curl application path(installation location)

User commands

User management is a crucial aspect of Linux system administration. Linux provides a variety of commands to interact with user accounts, gather user-related information, and manage user privileges. These commands enable system administrators to effectively manage user accounts and ensure proper access control.

In this section, we will delve into some essential user commands that can assist you in user management tasks.

  • cat /etc/passwd Displays the list of users.

  • getent passwd Displays the list of users.

  • The cat /etc/passwd command and getent passwd command displays the list of users in the system. The output is typically presented in the following format

redis:x:112:119::/var/lib/redis:/usr/sbin/nologin

The above line consists of seven fields, which are as follows:

  • redis: User name.

  • x: Encrypted password (the letter "x" indicates that the actual password is stored in the /etc/shadow file).

  • 112: User ID number (UID).

  • 119: User's group ID number (GID).

  • /var/lib/redis: Full name of the user (GECOS).

  • /usr/sbin/nologin: User home directory.

These commands provide important information about users on the system, such as their usernames, user IDs, group IDs, and home directories.

Ok. As you saw the cat /etc/passwd and getent passwd commands will display the output with seven fields.

However, you can also extract the part of the output by giving specific commands.

  • awk -F: '{print $1, $2}' /etc/passwd Displays the first two fields out of 7 fields

  • cut -d: -f1,2,3 /etc/passwd Displays the first 3 fields out of 7 fields

  • sudo useradd test_user1 Creates a new user called test_user1

  • sudo passwd test_user1 Sets the new password for user test_user1

  • sudo useradd -m test_user2 Creates a new user called test_user2 and also creates a test_user2 directory under the home directory.

  • sudo useradd -m -d /opt/test_user3 test_user3 Creates a test_user3 user and also creates a test_user3 directory under opt directory.

  • sudo useradd -u 1600 test_user4 Creates a test_user4 user with 1600 id

  • id -u test_user4 Returns the id of the user

  • sudo useradd -g users login_user1 Creates a new user and sets the login group to users

  • id -gn login_user1 Displays the group name of the user

  • sudo useradd -g users -G user1,user2 login_user2 Creates a new user called login_user2 and sets the primary group as users and secondary groups as user1, and user2.

  • id login_user2 Displays the userid, primary, and secondary group ids

  • sudo groupadd managers Creates a new group called managers

  • sudo chgrp managers file1.txt Changes the group to managers on file1.txt

  • sudo usermod -aG managers login_user2 Adds login_user2 to the managers group

Permission commands

Understanding Permissions in Linux In Linux, permissions play a crucial role in controlling access to files and directories. They define what actions users can perform on specific resources, such as reading, writing, or executing files

For example, run the ls -l command in any of the directories which have some content.

drwxr-xr-x 1 777 shiva 4096 May 25 16:52 blog<br>-rw-r--r-- 1 shiva shiva 158 Jan 17 11:54 dump.rdb<br>drwxr-xr-x 1 shiva shiva 4096 May 25 12:52 shell-scripting

You will see similar output in your terminal. Let’s consider the first 10 characters (drwxr-xr-x) in the first line.

  1. The first letter denotes the content type such as directory or file. d refers to the directory - refers to the file.

  2. The second three letters refer to the user permissions

  3. The third three letters refer to the group permissions

  4. The last three letters refer to the other's permissions

r read, w write, x execute, - no permission

u user, g group, o other people from the outside world, anall

+ adds permissions, - removes permissions, = adds new permissions and overrides existing

  • chmod o+w file1.txt gives write access to the outside people on file1.txt

  • chmod o-w file1.txt removes write access for the outside people on file1.txt

  • chmod g+wr file1.txt gives read-and-write access to the groups

  • chmod u=rwx file1.txt gives read, write, and execute access to the user and overrides the existing permissions

Absolute Numeric Mode

Number

Permission Type

Symbol

0

No permission

---

1

Execute

--x

2

Write

-w-

3

Execute + Write

-wx

4

Read

r--

5

Read + Execute

r-x

6

Read + Write

rw-

7

Read + Write + Execute

rwx

  • chmod 111 file1.txt gives execute access to the user, group, and others

  • chmod 750 file1.txt gives read+write+execute access to users and read+execute access to groups and no permissions to outside people.

  • chmod -R 777 posts give read+write+execute access to users, groups, and others on the directory posts and their contents.

Process commands

Processes are an integral part of any operating system, including Linux. They represent running programs or tasks that consume system resources. Linux provides a range of commands to manage processes efficiently, allowing users to monitor, control, and terminate them as needed.

  • ps displays currently running user processes

  • ps -ef displays currently running system processes

  • ps -ef grep 12693 displays the process information of 12693

  • top shows the processes running(hit q to exit)

  • htop alternative for the top (interactive process viewer)

  • kill 24567 kills the process 24567

Network and storage commands

Linux provides a variety of commands that enable users to interact with the network and storage components of their system. These commands allow you to gather network information, perform network diagnostics, manage storage devices, and retrieve files from the web. Understanding these commands can greatly enhance your ability to work with network and storage resources effectively.

  • ifconfig shows the network information

  • iwconfig shows wireless information

  • ping google.com returns the continuous response from the host

  • blkid shows the storage information

  • df shows the available and unavailable disk space

  • lsusb shows the connected devices list

  • lspci shows PCI information

  • wget https://www.sample-videos.com/img/Sample-jpg-image-50kb.jpg downloads image file

Compression commands

Compression commands are indispensable when it comes to managing large sets of files or reducing file sizes for storage or transfer. Linux provides a wide range of compression commands that allow you to package files into archives, extract their contents, and compress them using various algorithms. Following are some of the example compression commands.

  • tar cf testDir.tar testDir compress the testDir and creates testDir.tar file

  • tar xf testDir.tar extract the contents of testDir.tar

  • tar czf testDir.tar.gz testDir creates gzip compressed tar file

  • tar xzf testDir.tar.gz extract the compressed gzip file

  • tar -tvf testDir.tar displays the contents of the tar file

  • tar -tvf testDir.tar.gz displays the contents of gzip tar file

  • tar -xf nodejs.tar.xs Extracts the contents of nodejs.tar.xs

SSH and File transfer commands

Secure Shell (SSH) and file transfer commands are essential tools for remote access and transferring files between local and remote systems in Linux. SSH provides a secure encrypted connection, while file transfer commands enable efficient and secure file transfer over SSH. Following are some of the example SSH commands.

  • ssh root@domain.com connect to the domain.com server with the root user (prompts for password)

  • scp file1.txt user1@domain.com /tmp copies the file securely to the tmp folder in the domain.com server

  • sudo scp -r user1@domain.com:/var/www /temp copies files recursively from the server to the local machine

Ok. I know the list is too big. But, it is worth going through it. If you have these commands in your arsenal, you can definitely win the command line war.