Table of contents
- To view what's written in a file.
- To change the access permissions of files.
- To check which commands you have run till now.
- To remove a directory/ Folder.
- To create a fruits.txt file and to view the content.
- Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
- To create another file Colors.txt and to view the content.
- Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
- To find the difference between fruits.txt and Colors.txt files.
- Some Advance Command In Linux
- Some WildCard Example
- Some Tips and Tricks In the command line
Task: What is the Linux command to
To view what's written in a file.
To change the access permissions of files.
To check which commands you have run till now.
To remove a directory/ Folder.
To create a fruits.txt file and to view the content.
Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
To Show only top three fruits from the file.
To Show only bottom three fruits from the file.
To create another file Colors.txt and to view the content.
Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
To find the difference between fruits.txt and Colors.txt file.
To view what's written in a file.
To change the access permissions of files.
To check which commands you have run till now.
To remove a directory/ Folder.
To create a fruits.txt file and to view the content.
Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
To Show only top three fruits from the file.
head -n 3 file_name.txt
To Show only bottom three fruits from the file.
tail -n 3 file_name.txt
To create another file Colors.txt and to view the content.
Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
To find the difference between fruits.txt and Colors.txt files.
Some Advance Command In Linux
rsync
: Efficiently synchronize and transfer files between systems. Example: Synchronize a local directory with a remote directory:rsync -avz /path/to/local/dir user@remote:/path/to/remote/dir
ssh
: Securely connect to remote systems and execute commands. Example: Connect to a remote server:ssh user@remote
tar
: Archive and compress files and directories. Example: Create a compressed tarball of a directory:tar -czvf archive.tar.gz /path/to/directory
grep
: Search for patterns in files and directories. Example: Search for a specific string in a file:grep "pattern" file.txt
awk
: Process and manipulate structured text data. Example: Print the second column of a CSV file:awk -F ',' '{print $2}' file.csv
sed
: Stream editor for manipulating text. Example: Replace a specific string in a file:sed 's/old_string/new_string/g' file.txt
find
: Search for files and directories based on various criteria. Example: Find all files modified within the last 24 hours:find /path/to/search -type f -mtime -1
top
: Monitor system processes and resource usage. Example: Display real-time system resource usage:top
htop
: Interactive process viewer with additional features compared to top. Example: Monitor system processes and resource usage with htop:htop
netstat
: Display network connections, routing tables, and network interface statistics. Example: Show active network connections:netstat -tuln
iptables
: Configure and manage firewall rules. Example: Allow incoming SSH connections:iptables -A INPUT -p tcp --dport 22 -j ACCEPT
lsof
: List open files and the processes that use them. Example: List processes using a specific file:lsof /path/to/file
strace
: Trace system calls and signals of a running program. Example: Trace system calls of a command:strace -f -e trace=network command
journalctl
: View and manage system logs using the systemd journal. Example: View the latest system logs:journalctl -xe
dd
: Convert and copy files with low-level I/O operations. Example: Create a bootable USB from an ISO image:dd if=/path/to/image.iso of=/dev/sdX bs=4M status=progress
wget
: Download files from the internet. Example: Download a file from a URL:wget https://example.com/file.tar.gz
curl
: Transfer data from or to a server using various protocols. Example: Send an HTTP POST request with data:curl -X POST -d "param1=value1¶m2=value2" https://example.com/api
useradd
andusermod
: Create and modify user accounts. Example: Create a new user:useradd username
chown
andchmod
: Change ownership and permissions of files and directories. Example: Change ownership of a file:chown username file.txt
cron
: Schedule and automate recurring tasks. Example: Edit the crontab file to schedule a task:crontab -e
Additionally, here are some systemctl
and service-related commands:
Start a service:
sudo systemctl start service-name
Stop a service:
sudo systemctl stop service-name
Restart a service:
sudo systemctl restart service-name
Enable a service to start at boot:
sudo systemctl enable service-name
Disable a service from starting at boot:
sudo systemctl disable service-name
Check the status of a service:
sudo systemctl status service-name
Some WildCard Example
*
(asterisk): Matches any number of characters (including none). Example: List all files with the.txt
extension in the current directory:ls *.txt
?
(question mark): Matches a single character. Example: List all files with a three-letter extension in the current directory:ls ???
[ ]
(square brackets): Matches any single character specified within the brackets. Example: List all files with a digit as the second character:ls [0-9]?*
[!]
(exclamation mark within square brackets): Matches any single character not specified within the brackets. Example: List all files without a.txt
extension in the current directory:ls *[!.txt]
{ }
(curly braces): Matches any of the comma-separated patterns enclosed within the braces. Example: Copy multiple files with different extensions to a directory:cp file.{txt,doc,pdf} destination/
~
(tilde): Represents the user's home directory. Example: Navigate to the home directory:cd ~
.
(dot): Represents the current directory. Example: List all hidden files in the current directory:ls -a .*
..
(double dot): Represents the parent directory. Example: Navigate to the parent directory:cd ..
[^ ]
(caret within square brackets): Matches any single character not specified within the brackets. Example: List all files that don't start with a digit:ls [^0-9]*
**
(double asterisk): Matches any number of directories and subdirectories recursively. Example: Search for files with a specific name in all subdirectories:find /path/to/search -name "filename" -type f
Some Tips and Tricks In the command line
Command History:
Use the arrow keys to navigate through previously executed commands.
Pressing
Ctrl + R
allows you to search and execute commands from your command history.
Command Completion:
Pressing the
Tab
key autocompletes commands, file and directory names, and command options.Pressing
Tab
twice displays possible completions if there are multiple matches.
Command Output Manipulation:
Use
|
(pipe) to redirect the output of one command as input to another command.Example:
ls -l | grep "file"
- Lists files and directories and filters for lines containing "file".
Command Options:
Most commands have various options that modify their behavior.
Use the
-h
or--help
option to display the command's manual or help page.Example:
ls --help
- Displays the help page for thels
command.
Command Shortcuts:
Use the
Ctrl + C
shortcut to terminate a running command.Use the
Ctrl + D
shortcut to exit the current shell session.Use the
Ctrl + Z
shortcut to suspend a running command and bring it to the background.
Command Output Paging:
Use the
less
command to view long command output one page at a time.Example:
command | less
- Pipes the output of a command intoless
for paging.
Command Execution as Root:
Prefix a command with
sudo
to execute it with root (superuser) privileges.Example:
sudo apt-get update
- Updates package information with root privileges.
Command Execution in the Background:
Append
&
at the end of a command to run it in the background.Example:
command &
- Executes the command in the background, allowing you to continue working in the same terminal.
Command Execution Timing:
Use the
time
command to measure the execution time of a command.Example:
time command
- Displays the execution time of the specified command.
Command Output Redirect:
Use
>
to redirect command output to a file, overwriting its contents.Use
>>
to redirect command output to a file, appending it to the existing contents.Example:
command > output.txt
- Redirects the output of the command to the "output.txt" file.