DEV Community

Cover image for Mastering Essential Linux Commands and Understanding File Permissions
Bello Osagie
Bello Osagie

Posted on • Edited on

Mastering Essential Linux Commands and Understanding File Permissions

GitHub Logo

Introduction

In the Linux ecosystem, familiarity with essential commands and an understanding of file permissions are fundamental for efficient system administration and file management. In this blog post, we will explore a set of essential commands, including cd, ls, pwd, less, file, ln, cp, mv, rm, mkdir, type, which, help, and man. Additionally, we will delve into the concept of file permissions and cover related commands such as chmod, sudo, su, chown, chgrp, id, groups, whoami, adduser, useradd, and addgroup. By the end of this article, you will have a comprehensive understanding of these commands and their practical applications.

Basic Linux Commands

Linux provides a wide range of commands that are essential for efficient system administration and file management. Let's explore some of the basic Linux commands:

cd - Change Directory

The cd command allows you to navigate between directories. Use it followed by the directory name to switch to a different directory. For example, to change to the /home/user/documents directory:

cd /home/user/documents
Enter fullscreen mode Exit fullscreen mode

ls - List Files and Directories

The ls command lists the contents of a directory. Use it to view files and directories within the current directory. For example, to list the files and directories in the current directory:

ls
Enter fullscreen mode Exit fullscreen mode

pwd - Print Working Directory

The pwd command displays the current working directory's absolute path. Use it to identify your current location within the file system. For example, to print the current working directory:

pwd
Enter fullscreen mode Exit fullscreen mode

less - View File Content

The less command allows you to view the contents of a file. It provides a pager interface for easy navigation within large files. For example, to view the content of a file named example.txt:

less example.txt
Enter fullscreen mode Exit fullscreen mode

file - Determine File Type

The file command determines the file type of a given file. It examines the file's contents and provides information about its format. For example, to determine the file type of a file named script.sh:

file script.sh
Enter fullscreen mode Exit fullscreen mode

ln - Create Links

The ln command is used to create links between files. It can create hard links and symbolic (soft) links. For example, to create a symbolic link named link pointing to a file named target:

ln -s target link
Enter fullscreen mode Exit fullscreen mode

cp - Copy Files and Directories

The cp command copies files and directories. It allows you to duplicate files and directories with different names or in different locations. For example, to copy a file named source.txt to a new file named destination.txt:

cp source.txt destination.txt
Enter fullscreen mode Exit fullscreen mode

mv - Move/Rename Files and Directories

The mv command moves or renames files and directories. It can be used to change the name of a file or move it to a different location. For example, to move a file named file.txt to a different directory:

mv file.txt /path/to/directory/
Enter fullscreen mode Exit fullscreen mode

rm - Remove Files and Directories

The rm command removes files and directories. Use it with caution, as it permanently deletes files from the file system. For example, to remove a file named unwanted.txt:

rm unwanted.txt
Enter fullscreen mode Exit fullscreen mode

mkdir - Create Directories

The mkdir command creates directories (folders) in the file system. Use it to create new directories. For example, to create a directory named docs:

mkdir docs
Enter fullscreen mode Exit fullscreen mode

type

  • Display Command Type:

The type command displays the type of a command. It indicates whether a command is a shell built-in, an alias, a function, or an executable file. For example, to display the type of the ls command:

type ls
Enter fullscreen mode Exit fullscreen mode

which - Locate Command

The which command locates the executable file associated with a given command. It displays the absolute path to the command's binary file. For example, to locate the executable for the python command:

which python
Enter fullscreen mode Exit fullscreen mode

help - Get Help for Shell Built-in Commands

The help command provides help and documentation for shell built-in commands. It displays information about the usage and options of a specific command. For example, to get help for the cd command:

help cd
Enter fullscreen mode Exit fullscreen mode

man - Manual Pages

The man command displays the manual pages for a given command. It provides detailed information about command usage, options, and examples. For example, to view the manual page for the ls command:

man ls
Enter fullscreen mode Exit fullscreen mode

File Permissions and Related Commands

File permissions play a crucial role in controlling access to files and directories. Let's explore some commands related to file permissions:

chmod - Change File Permissions

The chmod command is used to change the permissions of a file or directory. It allows you to grant or revoke read, write, and execute permissions for users, groups, and others. For example, to add execute permission for the owner of a file named script.sh:

chmod +x script.sh
Enter fullscreen mode Exit fullscreen mode

sudo - Execute a Command with Superuser Privileges

The sudo command allows you to execute a command with superuser (root) privileges. It provides temporary administrative access for authorized users. For example, to install a package using sudo:

sudo apt-get install package_name
Enter fullscreen mode Exit fullscreen mode

su - Switch User

The su command is used to switch to another user account. It enables you to execute commands with the privileges of the specified user. For example, to switch to the root user:

su -
Enter fullscreen mode Exit fullscreen mode

chown - Change File Owner

The chown command changes the owner of a file or directory. It allows you to transfer ownership to another user. For example, to change the owner of a file named file.txt to the user newowner:

chown newowner file.txt
Enter fullscreen mode Exit fullscreen mode

chgrp - Change Group Ownership

The chgrp command changes the group ownership of a file or directory. It allows you to assign a different group to the file. For example, to change the group ownership of a file named file.txt to the group newgroup:

chgrp newgroup file.txt
Enter fullscreen mode Exit fullscreen mode

id - Display User and Group Information

The id command displays information about the current user and associated groups. It provides the user's UID (user ID) and GID (group ID). For example, to display user and group information:

id
Enter fullscreen mode Exit fullscreen mode

groups - Display Group Membership

The groups command shows the groups to which a user belongs. It lists the group names associated with the current user. For example, to display group membership:

groups
Enter fullscreen mode Exit fullscreen mode

whoami - Display Current User

The whoami command displays the username of the current user. It provides a quick way to identify the currently logged-in user. For example, to display the current user:

whoami
Enter fullscreen mode Exit fullscreen mode

adduser - Add a User

The adduser command is used to add a new user to the system. It creates a user account with the specified username and assigns a home directory. For example, to add a user named newuser:

adduser newuser
Enter fullscreen mode Exit fullscreen mode

useradd - Create a User Account

The useradd command creates a new user account. It allows you to specify various options, such as user ID, home directory, and login shell. For example, to create a user account named newuser with a home directory and the /bin/bash login shell:

useradd -m -s /bin/bash newuser
Enter fullscreen mode Exit fullscreen mode

addgroup - Add a Group

The addgroup command adds a new group to the system. It creates a group with the specified group name and assigns a GID (group ID). For example, to add a group named newgroup:

addgroup newgroup
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this blog post, we have explored essential Linux commands for file management and system administration. We covered commands such as cd, ls, pwd, less, file, ln, cp, mv, rm, mkdir, type, which, help, and man. Additionally, we discussed the concept of file permissions and related commands such as chmod, sudo, su, chown, chgrp, id, groups, whoami, adduser, useradd, and addgroup. Mastering these commands and understanding file permissions will give you a solid foundation for effective Linux system management.


References

Here are some references you can explore to learn more about Linux commands and file permissions:

  1. Linux Documentation Project (TLDP): https://tldp.org/

    • The TLDP provides a comprehensive collection of Linux documentation, including guides, how-tos, and tutorials on various topics.
  2. GNU Core Utilities Manual: https://www.gnu.org/software/coreutils/manual/

    • The GNU Core Utilities manual provides detailed documentation on essential Linux commands, including cd, ls, pwd, less, file, ln, cp, mv, rm, mkdir, type, which, help, and man.
  3. Linux man pages: https://man7.org/linux/man-pages/

    • The Linux man pages offer in-depth documentation for Linux commands. You can search for specific commands and explore their options, usage examples, and related information.
  4. Linux File Permission Tutorial: https://www.guru99.com/file-permissions.html

    • This tutorial from Guru99 provides an overview of Linux file permissions, explaining the concepts of read, write, and execute permissions. It covers chmod, chown, and chgrp commands in detail.
  5. Linux File System/Structure Explained: https://linuxhandbook.com/linux-file-system-structure/

    • This article on Linux Handbook explains the Linux file system structure, including directories, files, and their relationships. Understanding the file system structure is essential for effective file management.
  6. Linux User and Group Management: https://www.tecmint.com/manage-users-and-groups-in-linux/

    • Tecmint provides a tutorial on managing users and groups in Linux. It covers commands like adduser, useradd, addgroup, and explains user and group administration concepts.

Remember to explore these resources and refer to the official documentation for your specific Linux distribution to ensure accurate and up-to-date information.


Top comments (0)