Basic and must-know commands on Linux

Table of contents

In Linux, navigation and directory commands are essential for interacting with the file system. Here are some commonly used commands:
Navigation Commands:
cd (Change Directory):
Usage:
cd [directory]
Example:
cd Documents
Moves you to the specified directory. Use
cd
without arguments to return to your home directory (cd
orcd ~
).
pwd (Print Working Directory):
Usage:
pwd
Example: Shows the current directory path you are in.
ls (List):
Usage:
ls [options] [directory]
Example:
ls -l
Lists files and directories in the current directory. Common options include
-l
(long format, showing details like permissions and sizes),-a
(show hidden files), and-h
(human-readable sizes).
Directory Commands:
mkdir (Make Directory):
Usage:
mkdir [directory name]
Example:
mkdir Documents
Creates a new directory with the specified name.
rmdir (Remove Directory):
Usage:
rmdir [directory name]
Example:
rmdir Documents
Removes the specified empty directory.
cp (Copy):
Usage:
cp [options] source destination
Example:
cp file1.txt /path/to/destination
Copies files and directories. Use
-r
option to copy directories recursively.
mv (Move):
Usage:
mv [options] source destination
Example:
mv file1.txt /path/to/destination
Moves files and directories. Can also be used to rename files/directories within the same parent directory.
rm (Remove):
Usage:
rm [options] file/directory
Example:
rm file.txt
Deletes files and directories. Use with caution, as deleted files are not typically recoverable from the command line.
chmod (Change Mode):
Usage:
chmod [options] mode file/directory
Example:
chmod +x
script.sh
Changes the permissions (read, write, execute) of files and directories.
chown (Change Owner):
Usage:
chown [options] owner:group file/directory
Example:
chown user1:group1 file.txt
Changes the owner and group of files and directories.
Some tips!
1. ls -la command
ls -la
is a command in Linux used to list all files and directories in a detailed format, including hidden ones.
Here’s what each part of the command does:
ls
: This is the command to list directory contents.-l
: This option (lowercase "L") specifies that the output should be in long format. Long format typically includes information such as permissions, number of links, owner, group, size, and timestamp.-a
: This option stands for "all" and includes all files and directories, including hidden ones (those whose name starts with a dot.
).
So, ls -la
together lists all files and directories in the current directory (.
) in a detailed (long) format, showing all files, including hidden ones. It's a common command used to get a comprehensive view of everything within a directory, including files and directories that might not be visible with a simple ls
command.
ls-r | cp-r |chmod -R | where -r stands for recursive
ls -r
:- This option is used to reverse the order of the sort to get a reverse listing of files. By default,
ls
lists files and directories in alphabetical order. Using-r
reverses this order lists them in reverse alphabetical order.
- This option is used to reverse the order of the sort to get a reverse listing of files. By default,
Example:
ls -r
cp -r
:- In the
cp
command,-r
stands for. It is used to copy directories and their contents recursively, including all subdirectories and files within them.
- In the
Example:
cp -r directory1 directory2
chmod -R
:- In the
chmod
command,-R
(uppercase "R") stands for recursive as well. It is used to change file permissions recursively for all files and directories within a directory.
- In the
Example:
chmod -R 755 directory
If you encounter -r
in a command, it's crucial to check the specific command context to understand its exact function, as it can vary significantly between commands.
-v
typically stands for "verbose "
In Linux commands, -v
typically stands for "verbose". Its exact function can vary depending on the command being used. Here are a few common commands where -v
is used:
cp -v
:- In the
cp
(copy) command,-v
stands for verbose mode. When you usecp -v
, it displays detailed information about each file being copied, including the source file and destination file paths.
- In the
Example:
cp -v file1.txt /path/to/destination/
mv -v
:- Similarly, in the
mv
(move) command,-v
also enables verbose mode. It displays information about each file or directory being moved, including the source and destination paths.
- Similarly, in the
Example:
mv -v file1.txt /path/to/destination/
rm -v
:- In the
rm
(remove) command,-v
again enables verbose mode. It displays the names of files and directories as they are being removed.
- In the
Example:
rm -v file1.txt
chmod -v
:- In the
chmod
(change mode) command,-v
can be used to display a message for each file processed, indicating the changes made to file permissions.
- In the
Example:
chmod -v +x script.sh
The -v
option is useful when you want to see more detailed information about the operation being performed by a command. It provides feedback and confirmation of each action taken by the command, making it easier to track and verify what changes or operations are being executed.
-
f typically stands for "force"
In Linux commands, the -f
an option often stands for "force". Its exact behavior can vary depending on the specific command being used. Here are a few common commands that -f
is used:
rm -f
:- In the
rm
(remove) command,-f
stands for force. It suppresses prompts to confirm the removal of files. It's commonly used in scripts or when you want to delete files without being prompted for confirmation.
- In the
Example:
rm -f file1.txt
cp -f
:- In the
cp
(copy) command,-f
stands for force as well. It causes thecp
command to overwrite existing destination files without prompting for confirmation.
- In the
Example:
cp -f file1.txt /path/to/destination/
mv -f
:- Similarly, in the
mv
(move) command,-f
forces the move operation without prompting for confirmation, even if it would overwrite existing files.
- Similarly, in the
Example:
mv -f file1.txt /path/to/destination/
chmod -f
:- In the
chmod
(change mode) command,-f
doesn't force anything related to permissions directly. Instead, it might suppress error messages that would normally be displayed if the command encounters problems.
- In the
Example:
chmod -f +x script.sh
The -f
option is useful when you want to carry out an operation forcefully, without being prompted for confirmation or when you want to suppress certain types of errors or warnings. However, use it with caution, especially with commands like rm -f
, as it can delete files irreversibly without a chance to recover them.
File maintenance commands in Linux are essential for managing files and directories effectively. Here's an explanation of each command you mentioned:
touch:
Usage:
touch [options] file
Description: The
touch
command is used to create empty files or update the access and modification timestamps of existing files. If the file doesn't exist,touch
creates an empty file with the specified name. If it does exist,touch
updates the timestamps to the current time.
Example:
touch file.txt
find:
Usage:
find [path...] [expression]
Description: The
find
command is used to search for files in a directory hierarchy based on various criteria such as file name, permissions, type, size, and more. It is powerful and versatile, allowing complex searches and actions on the found files.
Example:
find . -name "*.txt"
umask:
Usage:
umask [mode]
Description: The
umask
command sets the default permissions for new files and directories created by the current shell session. It works by subtracting the specified mode from the default permission settings.
Example:
umask 022
chmod:
Usage:
chmod [options] mode file/directory
Description: The
chmod
command is used to change the permissions (read, write, execute) of files and directories. Permissions can be specified using symbolic or octal notation.
Example:
chmod +x script.sh
chown:
Usage:
chown [options] owner:group file/directory
Description: The
chown
command changes the owner and/or group of a file or directory to the specified owner and/or group.
Example:
chown user1:group1 file.txt
chgrp:
Usage:
chgrp [options] group file/directory
Description: The
chgrp
command changes the group ownership of a file or directory to the specified group.
Example:
chgrp group1 file.txt
cp:
Usage:
cp [options] source destination
Description: The
cp
command copies files and directories. It can copy single or multiple files, as well as entire directories and their contents. Use the-r
option for recursive copying of directories.
Example:
cp file1.txt /path/to/destination/
mv:
Usage:
mv [options] source destination
Description: The
mv
command moves (or renames) files and directories from one location to another. It can also be used to rename files and directories within the same parent directory.
Example:
mv file1.txt /path/to/destination/
wc:
Usage:
wc [options] file
Description: The
wc
(word count) command is used to count the number of lines, words, and bytes in a file. It's useful for obtaining basic statistics about the content of text files.
Example:
wc -l file.txt
These commands are fundamental for file and directory maintenance tasks in Linux, enabling users to manage permissions, ownership, content, and locations of files efficiently from the command line.
Subscribe to my newsletter
Read articles from Namdev Pratap directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Namdev Pratap
Namdev Pratap
Come along with me on a public learning journey into AWS Cloud and DevOps, designed specifically for those without a technical background. I'll be documenting each step in straightforward, easy-to-understand language to help others make a smooth transition into DevOps. Together, we'll delve into continuous integration, deployment, and automation, breaking down complex concepts into manageable, actionable insights.