This content originally appeared on DEV Community and was authored by Harsh Mishra
Linux is a robust and versatile operating system that offers a powerful command-line interface (CLI) for managing various tasks. For beginners, understanding basic Linux commands is crucial to efficiently navigating and operating within the Linux environment. This guide provides an introduction to essential Linux commands, categorized by their functionality, complete with descriptions, syntax, and multiple examples to demonstrate different ways of using each command.
Introduction to Linux Commands
Linux commands are text-based instructions that allow you to interact with your operating system. Whether you're managing files, checking system status, or installing software, these commands are your gateway to effective Linux usage.
Categories of Linux Commands
1. File and Directory Management Commands
File and directory management are core tasks in Linux. These commands help you navigate the file system, manage files and directories, and organize your system effectively.
a. ls
Command
Description: The
ls
command is used to list the contents of a directory. It displays files and directories within the specified path, allowing you to see what’s inside a directory at a glance.Syntax:
ls [options] [directory]
- Examples:
-
Basic listing of files and directories:
ls
Lists all files and directories in the current directory.
-
Long format listing with details:
ls -l
Provides a detailed listing, including file permissions, number of links, owner, group, size, and the last modified date.
-
Listing hidden files:
ls -a
Includes hidden files (those starting with a dot
.
). -
Combining options to list all files with detailed information:
ls -la /home/user
Lists all files, including hidden ones, in the
/home/user
directory with detailed information. -
Sorting files by modification time:
ls -lt
Lists files sorted by modification time, with the most recently modified files appearing first.
-
Listing files in a specific directory:
ls /var/log
Lists the contents of the
/var/log
directory.
b. cd
Command
Description: The
cd
(change directory) command is used to navigate between directories in the file system.Syntax:
cd [directory]
- Examples:
-
Change to a specific directory:
cd /home/user/Documents
Moves to the
/home/user/Documents
directory. -
Move up one directory level:
cd ..
Moves to the parent directory.
-
Return to the home directory:
cd ~
Takes you back to your home directory.
-
Change to the root directory:
cd /
Switches to the root directory of the file system.
-
Navigate to a directory with spaces in its name:
cd 'My Projects'
Changes to a directory named "My Projects" by enclosing it in quotes.
c. pwd
Command
Description: The
pwd
(print working directory) command displays the full path of the current working directory.Syntax:
pwd
- Examples:
-
Display the current directory:
pwd
Outputs the full path of the current working directory.
d. mkdir
Command
Description: The
mkdir
command is used to create new directories.Syntax:
mkdir [options] directory_name
- Examples:
-
Create a single directory:
mkdir new_folder
Creates a directory named
new_folder
. -
Create multiple directories:
mkdir dir1 dir2 dir3
Creates
dir1
,dir2
, anddir3
in the current location. -
Create a directory with parent directories:
mkdir -p /home/user/Projects/2024/January
Creates
2024
andJanuary
directories under/home/user/Projects/
if they don't exist. -
Set directory permissions at creation:
mkdir -m 755 new_folder
Creates
new_folder
with permissions755
(read, write, execute for owner, read and execute for group and others).
e. rmdir
Command
Description: The
rmdir
command is used to remove empty directories.Syntax:
rmdir [options] directory_name
- Examples:
-
Remove a single empty directory:
rmdir empty_folder
Deletes the
empty_folder
directory if it is empty. -
Remove multiple empty directories:
rmdir dir1 dir2 dir3
Removes
dir1
,dir2
, anddir3
if they are all empty. -
Remove a directory and its parent directories:
rmdir -p /home/user/Projects/2024/January
Removes the
January
directory and its parent directories2024
andProjects
if they become empty after the deletion.
f. cp
Command
Description: The
cp
command is used to copy files and directories from one location to another.Syntax:
cp [options] source destination
- Examples:
-
Copy a file to another location:
cp file.txt /home/user/Documents
Copies
file.txt
to the/home/user/Documents
directory. -
Copy a file with a new name:
cp file.txt newfile.txt
Creates a copy of
file.txt
with the namenewfile.txt
in the current directory. -
Copy multiple files to a directory:
cp file1.txt file2.txt /home/user/Documents
Copies
file1.txt
andfile2.txt
to the/home/user/Documents
directory. -
Copy a directory and its contents:
cp -r /home/user/old_directory /home/user/new_directory
Recursively copies
old_directory
and all its contents tonew_directory
. -
Preserve attributes while copying:
cp -p file.txt /backup/
Copies
file.txt
to/backup/
, preserving the original file attributes.
g. mv
Command
Description: The
mv
command is used to move or rename files and directories.Syntax:
mv [options] source destination
- Examples:
-
Move a file to a new directory:
mv file.txt /home/user/Documents
Moves
file.txt
to/home/user/Documents
. -
Rename a file:
mv oldname.txt newname.txt
Renames
oldname.txt
tonewname.txt
. -
Move and rename a file:
mv file.txt /home/user/Documents/newfile.txt
Moves
file.txt
to/home/user/Documents
and renames it tonewfile.txt
. -
Move multiple files to a directory:
mv file1.txt file2.txt /home/user/Documents
Moves
file1.txt
andfile2.txt
to/home/user/Documents
.
h. rm
Command
Description: The
rm
command is used to remove files and directories.Syntax:
rm [options] file_or_directory
- Examples:
-
Remove a single file:
rm file.txt
Deletes
file.txt
. -
Remove multiple files:
rm file1.txt file2.txt
Deletes
file1.txt
andfile2.txt
. -
Remove a directory and its contents:
rm -r directory_name
Recursively deletes
directory_name
and all its contents. -
Forcefully remove a file:
rm -f protected_file.txt
Removes
protected_file.txt
without prompting for confirmation. -
Remove all files in a directory:
rm -rf /path/to/directory/*
Deletes all files within the specified directory, including subdirectories.
i. touch
Command
Description: The
touch
command is used to create empty files or update the timestamp of existing files.Syntax:
touch [options] file_name
- Examples:
-
Create a new empty file:
touch newfile.txt
Creates an empty file named
newfile.txt
. -
Update the timestamp of an existing file:
touch existingfile.txt
Updates the last modified timestamp of
existingfile.txt
. -
Create multiple files at once:
touch file1.txt file2.txt
Creates two empty files,
file1.txt
andfile2.txt
.
j. cat
Command
Description: The
cat
command is used to concatenate and display the content of files. It is commonly used to view the contents of text files.Syntax:
cat [options] file_name
- Examples:
-
Display the contents of a file:
cat file.txt
Outputs the content of
file.txt
to the terminal. -
Concatenate multiple files:
cat file1.txt file2.txt > combined.txt
Combines
file1.txt
andfile2.txt
into a new file namedcombined.txt
. -
Display content with line numbers:
cat -n file.txt
Displays the contents of
file.txt
with line numbers. -
Append a file to another file:
cat file2.txt >> file1.txt
Appends the contents of
file2.txt
tofile1.txt
.
k. less
Command
Description: The
less
command allows you to view the content of files one screen at a time. It is useful for reading long files.Syntax:
less [options] file_name
- Examples:
-
View the contents of a file:
less file.txt
Opens
file.txt
inless
, allowing you to scroll through the content. -
Search for a pattern within a file:
less file.txt
Inside
less
, type/pattern
to search for a specific pattern withinfile.txt
. -
Navigate within a file:
-
Move down one line: Press
j
or the down arrow key. -
Move up one line: Press
k
or the up arrow key. - Move down one page: Press the spacebar.
-
Move up one page: Press
b
.
-
Move down one line: Press
-
Quit less:
q
Press
q
to exitless
and return to the terminal.
2. File Permissions and Ownership Commands
Managing file permissions and ownership is crucial in Linux for controlling access to files and directories. These commands allow you to modify who can read, write, or execute files and change file ownership to maintain security and organization in your system.
a. chmod
Command
Description: The
chmod
command is used to change the permissions of files and directories. Permissions control who can read, write, or execute a file.Syntax:
chmod [options] mode file_name
- Examples:
-
Grant read, write, and execute permissions to the owner:
chmod u+rwx file.txt
Grants the owner (user) read, write, and execute permissions on
file.txt
. -
Remove write permission from the group:
chmod g-w file.txt
Removes write permission for the group on
file.txt
. -
Set read-only permission for others:
chmod o=r file.txt
Sets read-only permission for others on
file.txt
. -
Set permissions using numeric mode (e.g., 755):
chmod 755 script.sh
Sets the permissions to
rwxr-xr-x
, meaning the owner has full permissions, while the group and others have read and execute permissions. -
Apply the same permissions to multiple files:
chmod 644 *.txt
Sets the permissions to
rw-r--r--
for all.txt
files in the directory. -
Recursively change permissions for all files and directories:
chmod -R 700 /home/user/private
Recursively applies
rwx------
permissions to all files and directories within/home/user/private
.
b. chown
Command
Description: The
chown
command changes the ownership of a file or directory. It assigns a new owner and/or group to the specified file or directory.Syntax:
chown [options] owner[:group] file_name
- Examples:
-
Change the owner of a file:
chown newuser file.txt
Changes the ownership of
file.txt
tonewuser
. -
Change the owner and group of a file:
chown newuser:newgroup file.txt
Changes the ownership of
file.txt
tonewuser
and the group tonewgroup
. -
Change the owner of a directory and its contents:
chown -R newuser /home/user/documents
Recursively changes the owner of
/home/user/documents
and all its contents tonewuser
. -
Change the group ownership only:
chown :newgroup file.txt
Changes the group ownership of
file.txt
tonewgroup
without altering the file’s owner.
c. chgrp
Command
Description: The
chgrp
command changes the group ownership of a file or directory. This is useful when multiple users in the same group need access to specific files.Syntax:
chgrp [options] group_name file_name
- Examples:
-
Change the group ownership of a file:
chgrp developers file.txt
Changes the group ownership of
file.txt
to thedevelopers
group. -
Change the group ownership of a directory and its contents:
chgrp -R staff /home/user/projects
Recursively changes the group ownership of
/home/user/projects
and all its contents to thestaff
group. -
Change the group ownership of multiple files:
chgrp admins file1.txt file2.txt
Changes the group ownership of
file1.txt
andfile2.txt
to theadmins
group.
3. Text Processing Commands
Text processing commands are vital for managing and analyzing text files in Linux. They allow users to search for patterns, sort data, and manipulate text effectively.
a. grep
Command
Description: The
grep
command searches for lines in files that match a specified pattern. It's commonly used for finding specific text within files.Syntax:
grep [options] pattern [file_name]
- Examples:
-
Search for a pattern in a file:
grep "error" file.txt
Searches for the word "error" in
file.txt
and prints lines containing it. -
Search for a pattern in multiple files:
grep "TODO" *.txt
Searches for the word "TODO" in all
.txt
files in the current directory. -
Perform a case-insensitive search:
grep -i "success" file.txt
Searches for the word "success" in
file.txt
, ignoring case.
b. sort
Command
Description: The
sort
command arranges lines in text files either alphabetically or numerically. It is useful for organizing data in a meaningful order.Syntax:
sort [options] [file_name]
- Examples:
-
Sort the contents of a file alphabetically:
sort file.txt
Sorts the lines in
file.txt
in alphabetical order. -
Sort the contents of a file in reverse order:
sort -r file.txt
Sorts the lines in
file.txt
in reverse alphabetical order. -
Sort a file numerically:
sort -n numbers.txt
Sorts the lines in
numbers.txt
numerically.
4. Other Useful Commands
In addition to file management and user administration, there are several other useful commands in Linux that enhance your productivity and help manage your command-line environment. These include commands for viewing command history, clearing the terminal screen, displaying text, and accessing manual pages.
a. history
Command
Description: The
history
command displays a list of previously executed commands in the terminal. It helps you recall and reuse past commands without retyping them.Syntax:
history [options]
- Examples:
-
Show the command history:
history
Displays a list of previously executed commands with their command numbers.
-
Show the last 10 commands:
history 10
Displays the most recent 10 commands.
-
Search for a specific command in history:
history | grep "command_name"
Searches for
command_name
in the history list. -
Repeat a specific command from history:
!n
Replace
n
with the command number from the history list to re-execute that command.
b. clear
Command
Description: The
clear
command clears the terminal screen of all previous commands and outputs, providing a clean slate for new commands.Syntax:
clear
- Examples:
-
Clear the terminal screen:
clear
Removes all text from the terminal window.
-
Clear the terminal screen and scrollback buffer:
clear && printf '\e[3J'
Clears both the terminal screen and the scrollback buffer.
c. echo
Command
Description: The
echo
command displays a line of text or the value of variables. It’s useful for printing messages to the terminal or debugging scripts.Syntax:
echo [options] [string]
- Examples:
-
Display a simple message:
echo "Hello, World!"
Prints
Hello, World!
to the terminal. -
Display the value of a variable:
NAME="John" echo "Hello, $NAME!"
Prints
Hello, John!
, assumingNAME
is set toJohn
. -
Print text with special characters:
echo -e "Line 1\nLine 2"
Uses
-e
to enable interpretation of backslash escapes, printingLine 1
andLine 2
on separate lines. -
Display environment variables:
echo $PATH
Prints the current
PATH
environment variable.
d. man
Command
Description: The
man
command displays the manual pages for other commands, providing detailed documentation on their usage, options, and syntax.Syntax:
man [options] command
- Examples:
-
Show the manual page for a command:
man ls
Displays the manual page for the
ls
command. -
Search for a keyword in manual pages:
man -k keyword
Searches for
keyword
in the manual page descriptions. -
View a specific section of a manual page:
man 5 passwd
Displays the manual page for the
passwd
file in section 5 (which typically covers file formats).
These fundamental Linux commands are essential tools for navigating, managing, and configuring your system. Mastering them provides a strong foundation for more advanced tasks and enhances your efficiency as you work with Linux. As you continue to explore and practice, these commands will become second nature, empowering you to handle various tasks with confidence and ease.
This content originally appeared on DEV Community and was authored by Harsh Mishra
Harsh Mishra | Sciencx (2024-09-04T21:01:52+00:00) Linux Commands for Beginners. Retrieved from https://www.scien.cx/2024/09/04/linux-commands-for-beginners/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.