Skip to content

Latest commit

 

History

History
300 lines (191 loc) · 10.7 KB

README.md

File metadata and controls

300 lines (191 loc) · 10.7 KB

Essential Linux Commands

Table of Contents

Objective

This documentation manual aims to offer a step-by-step guide on basic Linux commands for users to master a skillset for effective and productive use of the Linux environment. It covers file management, navigation, text editing, and system administration tasks, introducing basic to intermediate commands with explanations, syntax examples, and use cases to enhance your competence in system management using the Linux command line.

1. Basic Commands

1.1 Navigation and File Operations

Navigation and file operations in Linux involve a set of essential commands that allow users to interact with the filesystem.

i. 'pwd' Command - Print Working Directory

The pwd command displays the current directory's full path.

Syntax: pwd

pwd command

ii. 'ls' Command - List directory contents

The ls command lists files and directories in the current directory.

Syntax: ls

ls command

For example, ls -a displays all files, including hidden ones.

Syntax: ls -a

ls -a command

iii. 'cd' Command - Change directory

The cd command allows users to switch directories. If no directory is specified, it defaults to the user's home directory.

Syntax: cd [directory]

cd command

iv. 'cp' Command - Copy files or directories

The cp command copies files or directories to a specified location, useful for creating backups or duplicating data.

Syntax: cp [option] source destination

Example: cp filename.txt /path/to/destination/

cp command

To confirm the file was copied, use the cd command to navigate to the destination directory and list the contents with ls.

cp confirmation

v. 'mv' Command - Move/rename files or directories

The mv command moves or renames files and directories. If the destination exists, it will be overwritten.

Syntax: mv [options] source destination

mv command

Use the ls command to verify the mv command operation.

mv confirmation

vi. 'rm' Command - Remove files or directories

The rm command deletes files or directories. Multiple files can be removed simultaneously.

Syntax: rm [options] file(s)

To remove a single file: rm filename.txt

rm command

To remove multiple files: rm file1.txt file2.txt file3.txt

rm multiple files command

To remove a directory: rm -r directoryname

rm directory command

To force remove a file or directory without prompting for confirmation: rm -f filename.txt

rm files/director command

To force remove a directory and its contents without prompting for confirmation: rm -rf directoryname

rm directory with content command

vii. 'mkdir' Command - Create a new directory

The mkdir command creates new directories. Multiple directories can be created simultaneously.

Syntax: mkdir directory.name

mkdir command

Use the ls command to verify the mkdir command operation.

mkdir confirmation

viii. 'touch' Command - Create an empty file or update the timestamp

The touch command creates an empty file or updates the timestamp of an existing file.

Syntax: touch file.name

touch command

Use the ls command to verify the touch command operation.

touch confirmation

1.2 Viewing and Editing Files

Linux offers various commands and text editors to view and edit files. Commands like cat, more, less, head, and tail are useful for viewing file contents, while nano and vi are popular text editors for making changes.

i. 'cat' Command - Concatenate and display file content

The cat command reads, combines, and displays file contents.

Syntax: cat filename.txt

cat command

ii. 'more' Command - View file content interactively

The more command displays the contents of a file one screen at a time. It’s useful for large files.

Syntax: more filename.txt

more command

Using more with a large file:

more command with large file

Key Navigation for more command:

  • Press Space to move to the next page.
  • Press Enter to move to the next line.
  • Press b to move back one page.
  • Press q to quit the more viewer.

iii. 'less' Command - View file content interactively with more options

The less command allows both forward and backward navigation through a file with advanced features.

Syntax: less filename.txt

less command less command view

Key Navigation for less command:

  • Space or f to move forward one page.
  • b to move backward one page.
  • Enter to move forward one line.
  • y to move backward one line.
  • n to repeat the last search forward.
  • N to repeat the last search backward.
  • q to quit the viewer.

iv. 'head' Command - View the first few lines of a file

The head command shows the beginning of a file. By default, it displays the first 10 lines.

Syntax: head filename.txt

head command

To view a specific number of lines, use -n followed by the number of lines:

Syntax: head -n 20 filename.txt

head command with line count

v. 'tail' Command - View the last few lines of a file

The tail command shows the end of a file. By default, it displays the last 10 lines.

Syntax: tail filename.txt

tail command

To view a specific number of lines, use -n followed by the number of lines:

Syntax: tail -n 20 filename.txt

tail command with line count

vi. 'Nano' & 'vi' - Simple text editors

Both nano and vi are text editors for editing files directly from the terminal.

Nano:

To open a file with nano, use:

Syntax: nano filename.txt

nano command

vi:

To open a file with vi, use:

Syntax: vi filename.txt

vi command

2. Intermediate Commands

2.1 File Permissions and Ownership

File permissions and ownership in Linux are crucial for controlling access and security.

i. 'chmod' Command - Change file modes or Access Control Lists

The chmod command changes the permissions of a file or directory.

Syntax: chmod [options] mode file

chmod command

ii. 'chown' Command - Change file owner and group

The chown command changes the owner and group of a file or directory.

Syntax: chown [owner][:group] file

chown command

iii. 'chgrp' Command - Change group ownership

The chgrp command changes the group ownership of a file or directory.

Syntax: chgrp group file

chgrp command

2.2 Searching and Finding Files

Finding and searching for files efficiently can be achieved using commands like find, grep, and locate.

i. 'find' Command - Search for files in a directory hierarchy

The find command searches for files in a directory hierarchy.

Syntax: find [path] [expression]

find command

ii. 'grep' Command - Search text using patterns

The grep command searches for patterns within files.

Syntax: grep [options] pattern file

grep command

iii. 'locate' Command - Find files by name

The locate command quickly finds files by name using a database.

Syntax: locate filename

locate command

3. System Administration

3.1 'sudo' Command - Execute a command as another user

The sudo command allows users to execute commands with superuser or another user’s privileges.

i. Running a Command as Root

To run a command as the root user, prepend sudo:

Syntax: sudo command

sudo command

ii. Running a Command as a Specific User

To run a command as a specific user:

Syntax: sudo -u username command

sudo specific user command