Welcome to the world of Linux! As a new user, navigating the terminal and understanding the basics can seem daunting. Fear not, this article will guide you through the essential commands and concepts to kickstart your Linux journey.
What is Linux?
Linux is an operating system, like Windows or macOS, that runs your computer or devices. It helps manage hardware and software, allowing you to run programs and applications. It's known for being secure, flexible, and often free. Many people use Linux for its reliability and because it gives them more control over their computers.
Step-by-Step Guide to Practicing Linux Commands Hands-On
Step 1: Navigation and File System
The commands to use are:
cd [Change directory]: Use this command to navigate the file system.
cd .. [Move to the parent directory]: Reverses the cd command to move back one directory. For example, if you are currently in /home/user/Documents, using cd .. will take you to /home/user.
Pwd [Print working directory]: Displays the current directory.
ls [List files and directories]: This command lists files and directories in the current directory.
- ls -a [List all files and directories, including hidden ones]: The
ls -a
command shows all the files and folders, even the hidden ones, in the current location. Hidden files and folders are usually system or configuration files that start with a dot (.) and are not shown by default.
- ls -a [List all files and directories, including hidden ones]: The
Steps:
1a) Open the terminal
1b) To ensure you're in your home directory, type cd
and press enter
1c) To display your current working directory, type pwd
and press enter
1d) To list files and directories, type ls
and press enter
1e) To list all files and directories (including hidden ones), type ls -a
and press enter
1f) To navigate to a specific directory (e.g., Desktop) type cd Desktop
and press enter to navigate to that directory.
Step 2: File Management
The commands to use are:
mkdir [Create a new directory]: is a command used in the terminal to create a new directory (folder).
rmdir [Remove an empty directory]: use this command to remove an empty directory. But, if the directory contains any files or subdirectories, it will not be removed.
rm [Remove a file]: is a command to remove (delete) files
- rm -r [Remove a directory and its contents]: This is to recursively remove a directory and all of its contents, including subdirectories and files. This means that it deletes everything within the specified directory.
cp [Copy a file]: This is to copy files or directories from one location to another.
- cp -r [Copy a directory and its contents]: where -r stands for "recursive" to include all files and subdirectories.
mv [Move or rename a file]: The mv command effectively replaces the original file or directory with the new location or name.
touch [Create a new empty file]: The touch command in Linux is used primarily to create an empty file or to update the access and modification timestamps of an existing file without modifying its contents
Steps:
2a) To create a new directory, type mkdir <name-of-the-first directory>
and press enter
2b) To create a new empty file type touch <name-of-the-first file>
and press enter
2c) Type ls
to confirm if the new file/directory is listed
2d) Type cd ..
to go to the parent directory
2e) To create multiple new directories, type mkdir <name-of-the-second directory> <name-of-the-third directory>
and press enter
2f) To create multiple new empty files, type touch <name-of-the-second file> <name-of-the-third file> <name-of-the-fourth file>
and press enter
2g) Type ls
to confirm if the new directories and files are listed
2h) Type pwd
to locate your current working directory
2i) To copy a file, type cp <name-of-the-file-to-copy> <name-of-the-new-file>
and press enter
2j) Type ls
to confirm if the copied file is listed
2k) To rename a file and how to add spaces in file names, type mv <name-of-the-existing-file> "<name-of-the-new-file>"
and press enter
2l) Type ls
to confirm if the renamed file is listed
2m) To move a file, type mv <name-of-the-file-to-move> <destination: name-of-the-directory-or-sub-directory>
and press enter. (create a directory first if needed because you can only move a file or files into a directory or a sub-directory)
2n) Type cd <destination: name-of-the-directory-or-sub-directory>
to enter the directory(folder)
2o) Type ls
to confirm if the moved file is listed
2p) Type cd ..
to go to the parent directory
2q) To remove a file, type rm <name-of-the-file-to-remove>
(we are removing the file we copied from)
2r) To remove the empty directory, type rmdir <name-of-the-directory-to-remove>
(we are removing the third directory)
2s) Type ls
to confirm if the file and directory are delisted
Step 3: Working with Text
The commands to use are:
echo [Print text to the screen]: The command prints text to the terminal screen. For example, typing echo ‘Hello, World!’ will display Hello, World! on the screen. Also, we can use the echo command to write text to a file.
cat [Display file contents]: The
cat
command in Linux shows the content of a file on the screen. For example, typingcat
<filename> will display what's inside the filename.vim [Text editor]: Vim is designed for efficient text editing and is an enhanced version of the original vi editor. It offers features like, syntax highlighting, search and replace, and advanced editing commands, making it suitable for simple and complex text editing tasks.
- In Vim, you can:
Type
i
to enter insert mode and edit the file.Type
:wq
to save and exit.Type
:q!
to exit without saving.
head [Display the first few lines of a file]: This is useful for quickly viewing the start of a file without opening the entire file. It displays the first 10 lines by default, but you can adjust this number using options. (head -n <figure> filename)
tail [Display the last few lines of a file]: This is valuable for quickly accessing the end of files, particularly when dealing with logs or when the need to see the most recent data.
more [View a file page by page]: This command in Unix-like operating systems is used to view the contents of a file one screen at a time. It’s useful for reading through long files where you don't want to see all the content.
less [View a file page by page (with more features)]: This allows you to view the contents of a file or output from commands one screen at a time. It offers more features and flexibility than the ‘more’ command.
grep [Search for text within files]: This is for searching specific patterns within files or other input. It stands for "Global Regular Expression Print," and it allows users to filter and find lines of text that match a given pattern
Steps:
3a) To create a file with text, type echo "Welcome to the world of Linux!" > <name-of-the-new-file>
and press enter
3b) Type ls
to confirm the new file
3c) To display the new file contents, type cat <the-new-file name>
3d) To put a string in a file (to write text to an existing file), type echo "I am a cloud enthusiast." >> <name-of-the-existing-file>
3e) To display the existing file contents, type cat <the-existing-file name>
3f) To print text to the screen, type echo "Linux is fun"
3g) Type pwd
to locate your current working directory
3h) Type cd Desktop
to move to the desktop directory
3i) To create a text in a file, type vim <the-file name>
and press enter
- Type
i
to enter insert mode and start typing your text
- To Save the file: Press <Esc> to switch to normal mode, then type
:wq
and press <Enter>.
3j) Type cat
to view your typed text in the file
3k) To display the first few lines, type head <the-name-of-the-text file>
3l) To display the last few lines, type tail <the-name-of-the-text file>
3m) To view the <vim text file> page by page, type more <the-name-of-the-text file>
3n) To view the <vim text file> page by page (with more features), type less <the-name-of-the-text file>
Up/Down Navigation: You can scroll up or down through the content by pressing the
Up
andDown
arrow keys.Page Up/Page Down: Use the
Page Up
andPage Down
keys to move one screen at a time.Spacebar: Pressing the spacebar moves one screen forward.
b: Pressing
b
moves one screen backward.
3o) To search for a specific word, phrase, or string in a file, type grep "the pattern" <the-name-of-the-file>
in this case, we will use the <vim text file>
Step 4: System Information
The commands to use are:
uname -a [Display system information (kernel name, version, etc.)]: It provides detailed information about the system's kernel and operating environment
uptime [Show system uptime and load average]: This command provides information on how long the system has been running since its last boot.
whoami [Display current username]: To display the username of the currently logged-in user. This command is useful for confirming the identity, of the user, running the session, especially when working with multiple user accounts or executing commands that require specific user privileges.
history [Display command history]: This command displays the list of previously executed commands in the terminal session. Each command is listed with a unique number, allowing users a quick reference or re-run previous commands.
i !<number> [Re-run Commands]: where <number> is the command’s number in the list. For example, If command 45 in your history is 'ls -l', you can re-run it with: !45
history | grep "search_term" [Search History]: You can use grep to search through your
Steps:
4a) To Display system information, type uname -a
and press enter
4b) To Display system uptime and load average, type uptime
and press enter
4c.) To display your username, type whoami
and press enter
4d.) To display your command history, type history
and press enter
Step 5: Package Management (optional)
The commands to use are:
sudo apt update [To update the local package index]: This command is typically the first step before using commands like ‘sudo apt install’ and ‘sudo apt upgrade’ to ensure you get the latest version of the software and the most recent updates to upgrade your installed packages.
sudo apt install [Install a new package]: It’s a powerful and essential command for managing software on Linux systems. This command simplifies the processing of installing software by handling dependencies and automating the download and installation steps.
Steps:
5a) To update package lists, type sudo apt update
and press enter
5b) Enter your password (please note your password will not show)
5c) To install a package type sudo apt install <package>
and press enter
5d) if you want to install vim text editor) type sudo apt install vim
5e) You will be prompted to continue. Type "y" and press Enter.
Step 6: User and Permission Management (optional)
The commands to use are:
sudo useradd [Create a new user account]: The sudo useradd command is a powerful tool for creating new user accounts. It allows administrators to set up user accounts with various configurations, ensuring proper access and functionality according to system policies.
sudo passwd [Change a user's password]: This command is useful when creating new user accounts or when a user forgets their password and needs it reset by an administrator.
sudo userdel [Delete user’s account]: The sudo userdel command is an important tool for system administrators to manage user accounts. It allows for the secure removal of user accounts and optionally their associated directories and files. This command is useful for maintaining the system's user base and ensuring that old or unused accounts are properly removed.
sudo su [Switch to the superuser account]: Provides superuser privileges, allowing the execution of commands that require administrative rights.
Steps:
6a) To create a new user account, type sudo useradd <newuser>
and press enter
6b) To Set a password for the new user type sudo passwd <newuser>
and press enter
6c) Enter the 'new user' password
6d) Retype the 'new user' password (After confirming the password, you will see a message indicating that the password has been successfully updated}.
6e) To verify that the new user has been added, type grep 'newuser' /etc/passwd
or cat /etc/passwd
to view all users on the system
6f) To remove a user account, type sudo userdel <username>
(It will prompt for the sudo user's password)
To delete the user's home directory and mail spool, use the -r flag:
sudo userdel -r <username>
6g) To verify that the new user has been remove, type grep 'newuser' /etc/passwd
or cat /etc/passwd
to view all users on the system
6h) To Switch to the superuser account type sudo su
and press enter
- Security Note: Use this command with caution, as it provides full control over the system, which can lead to unintentional changes or damage if used improperly. Always ensure you have a clear understanding of the commands you are executing in a superuser session.
Mastering these essential commands and concepts will provide a solid foundation for your Linux journey. Practice and experimentation are key to becoming proficient in Linux. Remember, the Linux community is vast and supportive, so don't hesitate to ask for help when needed. Happy Linux-ing!