- Essential Linux Commands
- 1. Basic Commands
- 1.1 Navigation and File Operations
- i. 'pwd' Command - Print Working Directory
- ii. 'ls' Command - List directory contents
- iii. 'cd' Command - Change directory
- iv. 'cp' Command - Copy files or directories
- v. 'mv' Command - Move/rename files or directories
- vi. 'rm' Command - Remove files or directories
- vii. 'mkdir' Command - Create a new directory
- viii. 'touch' Command - Create an empty file or update the timestamp
- 1.2 Viewing and Editing Files
- i. 'cat' Command - Concatenate and display file content
- ii. 'more' Command - View file content interactively
- iii. 'less' Command - View file content interactively with more options
- iv. 'head' Command - View the first few lines of a file
- v. 'tail' Command - View the last few lines of a file
- vi. 'Nano' & 'vi' - Simple text editors
- 1.1 Navigation and File Operations
- 2. Intermediate Commands
- 3. System Administration
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.
Navigation and file operations in Linux involve a set of essential commands that allow users to interact with the filesystem.
The pwd
command displays the current directory's full path.
Syntax: pwd
The ls
command lists files and directories in the current directory.
Syntax: ls
For example, ls -a
displays all files, including hidden ones.
Syntax: ls -a
The cd
command allows users to switch directories. If no directory is specified, it defaults to the user's home directory.
Syntax: cd [directory]
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/
To confirm the file was copied, use the cd
command to navigate to the destination directory and list the contents with ls
.
The mv
command moves or renames files and directories. If the destination exists, it will be overwritten.
Syntax: mv [options] source destination
Use the ls
command to verify the mv
command operation.
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
To remove multiple files:
rm file1.txt file2.txt file3.txt
To remove a directory:
rm -r directoryname
To force remove a file or directory without prompting for confirmation:
rm -f filename.txt
To force remove a directory and its contents without prompting for confirmation:
rm -rf directoryname
The mkdir
command creates new directories. Multiple directories can be created simultaneously.
Syntax: mkdir directory.name
Use the ls
command to verify the mkdir
command operation.
The touch
command creates an empty file or updates the timestamp of an existing file.
Syntax: touch file.name
Use the ls
command to verify the touch
command operation.
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.
The cat
command reads, combines, and displays file contents.
Syntax: cat filename.txt
The more
command displays the contents of a file one screen at a time. It’s useful for large files.
Syntax: more filename.txt
Using more
with a 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 themore
viewer.
The less
command allows both forward and backward navigation through a file with advanced features.
Syntax: less filename.txt
Key Navigation for less
command:
Space
orf
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.
The head
command shows the beginning of a file. By default, it displays the first 10 lines.
Syntax: head filename.txt
To view a specific number of lines, use -n
followed by the number of lines:
Syntax: head -n 20 filename.txt
The tail
command shows the end of a file. By default, it displays the last 10 lines.
Syntax: tail filename.txt
To view a specific number of lines, use -n
followed by the number of lines:
Syntax: tail -n 20 filename.txt
Both nano
and vi
are text editors for editing files directly from the terminal.
To open a file with nano
, use:
Syntax: nano filename.txt
To open a file with vi
, use:
Syntax: vi filename.txt
File permissions and ownership in Linux are crucial for controlling access and security.
The chmod
command changes the permissions of a file or directory.
Syntax: chmod [options] mode file
The chown
command changes the owner and group of a file or directory.
Syntax: chown [owner][:group] file
The chgrp
command changes the group ownership of a file or directory.
Syntax: chgrp group file
Finding and searching for files efficiently can be achieved using commands like find
, grep
, and locate
.
The find
command searches for files in a directory hierarchy.
Syntax: find [path] [expression]
The grep
command searches for patterns within files.
Syntax: grep [options] pattern file
The locate
command quickly finds files by name using a database.
Syntax: locate filename
The sudo
command allows users to execute commands with superuser or another user’s privileges.
To run a command as the root user, prepend sudo
:
Syntax: sudo command
To run a command as a specific user:
Syntax: sudo -u username command