Linux Commands for Beginners
Overview
Linux is a powerful operating system that provides a command-line interface for users to interact with their system. Knowing basic Linux commands is essential for beginners to manage and execute tasks efficiently. This cheat sheet provides a brief definition of the basic commands in Linux and an example for each of the commands.
Linux Commands for Beginners Cheat Sheet
As mentioned in the Overview, we will discuss the most useful Linux commands for beginners here.
Print Working Directory (pwd)
The pwd command stands for "print working directory." When executed, it displays the full path of the current working directory, which represents the directory you are currently located in within the file system.
This command is helpful when you need to know your exact location in the file system or when referencing paths for other commands.
Make Directory (mkdir)
The mkdir command is used to create a new directory or multiple directories within the file system. It allows you to organize your files by creating new directories as needed.
In this example, the mkdir command is used to create a directory named "my_directory" in the current working directory. After executing the command, a new directory with the specified name will be created.
Additionally, you can specify a full path to create a directory in a specific location:
Here, the mkdir command will create a directory named "new_directory" at the specified path "/path/to/".
List (ls)
The ls command is used to list the files and directories within a given directory. It provides a convenient way to view the contents of a directory and obtain information about files and directories, such as names, sizes, permissions, and timestamps.
When executed without any arguments, the ls command lists the files and directories in the current working directory.
You can also provide a specific directory path as an argument to list its contents:
By specifying a directory path, the ls command will list the files and directories within that particular directory.
There are various options available with the ls command to customize the output. Some commonly used options include:
Options | Objectives |
---|---|
-l (long format) | Provides detailed information about files and directories, including permissions, owner, size, and timestamps. |
-a (all) | Displays all files, including hidden files that start with a dot (.). |
-h (human-readable) | Prints file sizes in a human-readable format, such as "K" for kilobytes or "M" for megabytes. |
Change Directory (cd)
The cd command is used to change the current working directory in the Linux command-line interface. It allows you to navigate through the file system and switch to different directories.
In this example, the cd command is used to change the current working directory to a directory named "my_directory" within the current directory. After executing the command, you will be in the specified directory.
You can also provide an absolute or relative path to change to a specific directory:
By providing the absolute path to the desired directory, such as "/path/to/directory", the cd command will switch the current working directory to that location.
Remove a File (rm)
The rm command is used to delete or remove files in Linux. It allows you to remove unwanted or unnecessary files from the file system.
In this example, the rm command is used to delete a file named "file.txt" in the current working directory. After executing the command, the specified file will be permanently removed.
If you want to remove a directory and its contents recursively, you can use the "-r" or "-rf" option:
or,
These options will remove the directory and its contents, including all files and subdirectories.
Copy a File (cp)
The cp command in Linux is used to copy files and directories. It allows you to duplicate files, preserving the original while creating an identical copy in a specified location.
In this example, the cp command is used to copy a file named "file.txt" to a directory named "new_location." After executing the command, a new file with the same content as "file.txt" will be created in the "new_location" directory.
Move and Rename a File (mv)
The mv command in Linux is used to move or rename files and directories. It allows you to change the location of a file or directory within the file system or rename it while preserving its content.
In this example, the mv command is used to move a file named "file.txt" to a directory named "new_location." After executing the command, the file "file.txt" will no longer exist in its original location and will be moved to the specified directory.
Create an Empty File (touch)
The touch command in Linux is used to create an empty file without any content. It is commonly used to generate a new file quickly or update the timestamp of an existing file.
In this example, the touch command is used to create a new empty file named "file.txt" in the current working directory. If the file already exists, the touch command updates its timestamp without modifying the file's content.
Change Permissions (chmod)
The chmod command in Linux is used to change the permissions of files and directories. It allows you to modify the read, write, and execute permissions for the owner, group, and others.
In this example, "u+rwx" represents the desired permission settings, for the owner's read, write, and execute permissions. "file.txt" is the target file for which the permissions are being modified.
Escalate Privileges (sudo)
The sudo command allows users to run commands with elevated privileges. Let's say you're a regular user and want to install a software package on your Linux system that requires administrative privileges. Instead of logging in as the root user, you can use sudo to execute the installation command with elevated permissions.
For example, to install a package named "example-package" using the apt package manager, you would typically run the command.
After entering your password and authenticating, sudo checks if you have the necessary permissions and then runs the apt install command with the privileges of the root user. This allows you to install the package successfully.
Shut Down (poweroff)
The poweroff command is used to shut down a Linux system. It initiates the process of powering off the computer in an orderly manner. This command is typically executed with root or administrative privileges using sudo.
After entering your password and authenticating with sudo, the system will begin the shutdown process. It will perform necessary tasks to close applications, save data, and safely turn off the system.
Read the Manual (man)
To access the man manual pages (man pages) in Linux, you can use the man command followed by the name of the command or topic you want to learn more about. The man command displays comprehensive documentation and information about various commands, system calls, libraries, and configuration files.
To use the man command, simply type the following in the terminal.
Replace <command> with the name of the command or topic you want to read the manual for.
Conclusion
-
This cheat sheet provides an overview of essential Linux commands for beginners.
-
It covers commands such as pwd for printing the working directory, mkdir for creating directories, ls for listing files and directories, cd for changing directories, rm for removing files, cp for copying files, mv for moving and renaming files, touch for creating empty files.
-
Also discussed on chmod for changing permissions, sudo for executing commands with elevated privileges, poweroff for shutting down the system, and using a man for accessing comprehensive command documentation.