Familiarization of basic Linux Commands- ls, mkdir, rmdir , rm, cat, cp, mv , chmod
ls - list files
The ls
command in Linux is one of the most frequently used commands. It is used to list the contents of a directory. It shows files, directories, and symbolic links. Here’s a detailed explanation of the command, including its syntax, common options, and examples.
Syntax
ls [OPTION]... [FILE]...
ls
lists the contents of the current directory.Default Behavior
- Without any options,
ls
lists the contents of the current directory in a simple format. - It does not show hidden files (files starting with a dot
.
) by default.
Common Options
Option | Description |
---|---|
-l | Displays contents in a detailed (long) format, including file permissions, size, and more. |
-a | Lists all files, including hidden files (starting with . ). |
-h | Shows file sizes in a human-readable format (e.g., 1K , 5M , 3G ). |
-R | Lists contents of directories recursively. |
-t | Sorts files by modification time (newest first). |
-r | Reverses the order of the listing. |
-S | Sorts files by size (largest first). |
--color | Adds colors to distinguish file types (e.g., directories in blue, files in white). |
-d | Lists directories themselves, not their contents. |
-i | Displays the inode number of each file. |
--help | Displays a help message with usage information. |
Explanation:
File Permissions (
-rw-r--r--
):- The first character: File type (
-
for files,d
for directories,l
for symbolic links). - Next nine characters: Permissions for the owner, group, and others (
r
for read,w
for write,x
for execute).
- The first character: File type (
Number of Links (
1
): Number of hard links to the file or directory.Owner (
user
): The username of the file's owner.Group (
group
): The group that owns the file.Size (
1234
): File size in bytes.Last Modified (
Nov 30 14:00
): Date and time of the last modification.Name (
file.txt
): Name of the file or directory.
- If you try to create a nested directory (e.g., parent/child) without -p, it will fail if parent does not exist.
- Using -p, mkdir will create all necessary parent directories.
2. -v
(Verbose Mode)
- Prints a confirmation message for every directory created.
3. -m
(Set Permissions)
- Specifies permissions for the new directory at creation time.
- Permissions are provided in octal format (e.g.,
755
for read/write/execute for the owner, and read/execute for group and others).
Examples
1. Create a Single Directory
existing_dir
already exists, no error is thrown.sudo
to create directories in restricted locations:8.Combine Options:
- Use
mkdir -pv
to create nested directories with confirmation messages.
9.Check Directory Creation:
- Use
ls
to verify the new directory:
- If the
-m
option is not used, the directory permissions are determined by the user’sumask
.
Summary
The mkdir
command is a simple yet powerful tool for directory management. By using options like -p
for nested directories or -m
for permissions, it provides flexibility to suit various needs. Practicing with this command helps you manage directories efficiently in the Linux file system.
rmdir - Removing Directories
rmdir
: A standard Linux command used to remove empty directories.rmdir
command is used to remove empty directories. If a directory contains files or sub directories, rmdir
will fail.-p:Removes the specified directory and any parent directories if they are empty.
rm -r
Command
If you need to remove non-empty directories or directories containing files, you use the rm
command with the -r
(recursive) option.
-r
: Recursively removes directories and their contents.-f
: Forces deletion without prompting for confirmation.1.Remove an Empty Directory
rmdir emptydirSafety Tips for Recursive Deletion
- Be Cautious: Using
rm -rf
can irreversibly delete important files. - Double-Check Path: Always verify the directory path before running the command.
- Dry Run: Use
ls
ortree
to preview the contents before deletion
Summary
- Use
rmdir
for safely removing empty directories. - Use
rm -r
for removing directories and their contents. - Always exercise caution with recursive deletion commands like
rm -rf
.
rm - remove files and directories
Syntaxrm [OPTION]... FILE...
rm
command.Basic Functionality
- The
rm
command removes the specified files or directories. - By default,
rm
does not remove directories; you need to use specific options to remove directories or their contents. - Deleted files and directories cannot be recovered directly through the command.
Common Options
Option | Description |
---|---|
-f | Force deletion without prompting or showing error messages for non-existent files. |
-i | Prompts for confirmation before each file or directory is removed. |
-I | Prompts once before removing more than three files or recursively deleting. |
-r or -R | Removes directories and their contents recursively. |
-d | Removes empty directories. |
--preserve-root | Prevents recursive deletion of the root directory / (default behavior). |
--help | Displays help information about the command. |
2. Remove Multiple Files
rm file1.txt file2.txt
Summary
The rm
command is a versatile tool for file and directory management. With options like -r
for recursive deletion, -f
for forced deletion, and -i
for interactive mode, it offers flexibility for various scenarios. However, its power comes with responsibility — always double-check paths and options before executing potentially destructive commands.
cat command
cat
command.Common Use Cases
- Displaying the contents of a file.
- Creating a new file.
- Appending content to a file.
- Combining multiple files into one.
Common Options
Option | Description |
---|---|
-n | Numbers all lines in the output. |
-b | Numbers only non-empty lines in the output. |
-s | Suppresses repeated empty lines in the output. |
-T | Displays tab characters as ^I . |
-v | Shows non-printable characters (except for tabs and line endings). |
-A | Combines -vET to show all non-printable characters, end-of-lines, and tabs. |
-e | Equivalent to -vE ; shows non-printable characters and end-of-line markers. |
-E | Displays $ at the end of each line. |
6.Create a File
You can use cat
to create a file and input text directly from the terminal.Type the content, and press CTRL+D
to save and exit:
cat > newfile.txt
Tips and Best Practices
Avoid Overwriting by Mistake
- Be cautious when using
>
as it overwrites the target file. - Use
>>
to append instead of overwriting.
- Be cautious when using
Verify Commands
- Double-check file paths and options to prevent data loss, especially when overwriting or appending.
Combine with Other Commands
- Pipe the output of
cat
to other utilities likegrep
,less
, orwc
Summary
The cat
command is an indispensable utility in Linux for managing and viewing files. Its simplicity and flexibility make it a favorite for both basic and advanced text operations. With options like -n
, -b
, and -s
, it provides detailed control over file output and manipulation. Always use it carefully, especially when overwriting or appending data.
cp - copy command
cp
command in Linux is used to copy files and directories from one location to another. It is one of the fundamental file management commands and supports a variety of options to handle permissions, symbolic links, and directory structures effectively.cp
command.Common Use Cases
- Copying a single file to a new location.
- Copying multiple files to a directory.
- Copying directories, including their contents, recursively.
Common Options
Option | Description |
---|---|
-a | Archive mode: Preserves file attributes (e.g., ownership, permissions, timestamps). Equivalent to -dR --preserve=all . |
-f | Force overwriting existing files without prompting. |
-i | Interactive mode: Prompts before overwriting files. |
-n | No clobber: Prevents overwriting existing files. |
-r or -R | Recursive: Copies directories and their contents recursively. |
-v | Verbose: Displays detailed information about the copying process. |
-u | Updates files only if the source file is newer than the destination file or if the destination file does not exist. |
--preserve | Specifies which attributes to preserve (e.g., mode, ownership, timestamps). |
--no-preserve | Specifies attributes not to preserve. |
--backup | Creates a backup of files that are about to be overwritten. |
--help | Displays help information about the command. |
Tips and Best Practices
Use
-i
for Safe Copying- Always use
-i
when working with critical files to avoid accidental overwrites.
- Always use
Preserve Attributes with
-a
- When creating backups or migrating files, use
-a
to maintain file metadata.
- When creating backups or migrating files, use
Verify Files with
ls
- After copying, list the contents of the destination directory to confirm the operation:
- Use
-u
to Save Time
- When copying large directories,
-u
ensures only updated files are copied, saving time and resources.
Summary
The cp
command is a versatile and powerful tool for copying files and directories in Linux. With options like -r
for recursive copying, -a
for preserving attributes, and -i
for interactive mode, it provides flexibility and control over the copying process. Always double-check source and destination paths to prevent accidental overwrites or data loss.
mv - move command
mv
command in Linux is used to move or rename files and directories. It is a versatile command for organizing and renaming files and directories in the filesystem.Common Options
Option | Description |
---|---|
-i | Interactive: Prompts for confirmation before overwriting existing files. |
-f | Force: Moves files without prompting, even if overwriting. |
-n | No-clobber: Prevents overwriting existing files. |
-u | Updates: Only moves files if the source is newer than the destination or missing. |
-v | Verbose: Displays the details of the move process. |
Usage
1. Move a File
mv file1.txt /home/user/documents/
Differences Between mv
and cp
Aspect | mv | cp |
---|---|---|
Operation | Moves the file (removes it from the source). | Copies the file (source remains intact). |
Permissions | Requires write permissions at both locations. | Requires read permissions at source and write at destination. |
chmod- change mode
chmod
command in Linux is used to change the permissions of files or directories. Permissions control who can read, write, or execute a file or directory.- MODE: Specifies the new permissions, either symbolically (letters) or numerically (octal).
- FILE/DIRECTORY: The target file or directory to which permissions are applied.
File Permissions in Linux
Each file or directory has three types of permissions for three categories of users:
Permission Types
Permission | Symbol | Description |
---|---|---|
Read | r | Allows reading the file or listing the directory. |
Write | w | Allows modifying the file or creating/deleting files in a directory. |
Execute | x | Allows executing a file or accessing a directory. |
User Categories
User | Symbol | Description |
---|---|---|
Owner | u | The file's creator or owner. |
Group | g | Users in the file's group. |
Others | o | All other users. |
All | a | Represents u , g , and o combined. |
-rw-r--r--
represents the permissions:r
: Readw
: Writex
: Execute
- The breakdown is:
- Owner:
rw-
(read, write) - Group:
r--
(read) - Others:
r--
(read)
- Owner:
Setting Permissions
1. Using Symbolic Mode
Symbolic mode modifies permissions by adding (+
), removing (-
), or setting explicitly (=
) for specific categories.
2. Using Numeric (Octal) Mode
Numeric mode uses a three-digit octal number to set permissions. Each digit represents the permissions for owner, group, and others:
Permission | Octal Value |
---|---|
--- | 0 |
--x | 1 |
-w- | 2 |
-wx | 3 |
r-- | 4 |
r-x | 5 |
rw- | 6 |
rwx | 7 |
Options
Option | Description |
---|---|
-R | Recursive: Changes permissions for files and subdirectories. |
--verbose | Displays a message for each file processed. |
--help | Displays help for the command. |
Best Practices
Avoid Over-Permissive Settings
Avoid using777
unless necessary, as it grants all users unrestricted access.Use Recursive Option Cautiously
When using-R
, double-check the target directory to prevent unintended permission changes.Combine Symbolic and Numeric Modes
Use symbolic mode for flexibility and numeric mode for precision.
Comments
Post a Comment