Table of contents
- Part 1: Introduction to Basic Linux Commands
- Setting Up Your Workspace
- Adding Text to a File
- Creating and Viewing Additional Files
- Using less and more Commands
- Current Directory and File Management
- Copying Files and Directories
- Moving and Renaming Files and Directories
- Listing and Counting
- Creating Soft Links
- Creating Hard Links
- Cutting and Sorting Content
- Using tee
- Differences and Editing with vi
- Connecting to Remote Servers and System Monitoring
- Managing Processes
- Memory and System Performance
Part 1: Introduction to Basic Linux Commands
Setting Up Your Workspace
Create a new directory named "devops":
mkdir devops
Navigate into the "devops" directory:
cd devops
Create a new file named "demofile.txt":
touch demofile.txt
List the contents of the directory to ensure "demofile.txt" is created:
ls
Output:
demofile.txt
Display the contents of "demofile.txt" (it should be empty):
cat demofile.txt
Adding Text to a File
Print a message to the terminal:
echo "Hello Dosto"
Write the message "Hello Dosto" to "demofile.txt":
echo "Hello Dosto" > demofile.txt
Verify the content of "demofile.txt":
cat demofile.txt
Output:
Hello Dosto
Creating and Viewing Additional Files
Create another file and add text:
echo "Hello Dosto" > one.txt
Display the contents of "one.txt":
cat one.txt
Output:
Hello Dosto
View the beginning of "one.txt":
head one.txt
View the end of "one.txt":
tail one.txt
Continuously monitor the end of "one.txt":
tail -f one.txt
Using less
and more
Commands
View "one.txt" with
less
:less one.txt
View "one.txt" with
more
:more one.txt
Current Directory and File Management
Display the current directory path:
pwd
Copying Files and Directories
Copy "one.txt" to the "devops" directory:
cp one.txt devops/
Copy "one.txt" within the main directory:
cp devops/one.txt devops/copy/
Copy a directory and its contents recursively:
cp -r copy/ devops/
Moving and Renaming Files and Directories
Move "demofile.txt" to the "copy" directory:
mv demofile.txt copy/
Rename the "Devops" directory to "linuxfordevops":
mv Devops/ linuxfordevops
Listing and Counting
List files in the current directory:
ls
Display the content of "one.txt":
cat one.txt
Output:
Hello Dosto
Word count of "one.txt":
wc one.txt
Creating Soft Links
Create and add text to a new file:
echo "hello this is softlink example." > devops-file.txt
Display the content of "devops-file.txt":
cat devops-file.txt
Display the current directory path and copy it:
pwd
Create a soft link to "devops-file.txt":
ln -s /home/dhruv/Devops/linuxfordevops/devops-file.txt softlink-file1
List files in long format:
ls -ltr
Display the content of the soft link:
cat softlink-file1
Creating Hard Links
Create and add text to another file:
echo "this is harlink example" > devopsfile.txt
List files to verify the creation:
ls
Display the content of "devopsfile.txt":
cat devopsfile.txt
Create a hard link to "devopsfile.txt":
ln /home/dhruv/Devops/linuxfordevops/devopsfile.txt hardlink-file
List files again to see the hard link:
ls
Display the content of the hard link:
cat hardlink-file
Cutting and Sorting Content
Cut and display the first character of "one.txt":
cut -b 1 one.txt
Output:
H
Cut and display the first four characters of "one.txt":
cut -b 1-4 one.txt
Output:
Hell
Using tee
Echo text and use
tee
to display and write to a file:echo "hey" | tee
Output:
hey
Echo text and write to "hey.txt" using
tee
:echo "hey" | tee hey.txt
Output:
hey
Display the content of "hey.txt":
cat hey.txt
Output:
hey
Differences and Editing with vi
Compare two files:
diff one.txt hardlink-file
Edit "one.txt" using
vi
:vi one.txt
Press
i
for insert mode.Press
Esc
to exit insert mode.Type
:wq
to write changes and quit.
Sort the content of "one.txt":
sort one.txt
Connecting to Remote Servers and System Monitoring
Check disk space:
df df -h
Navigate to the parent directory and check disk usage:
cd .. du .
List all files, including hidden ones:
ls -a
Managing Processes
View active processes:
ps top
Find which process is using a file:
fuser filename
Kill a process by ID:
kill -9 process_id
Memory and System Performance
Check free memory:
free free -h
Run a command in the background and view the output later:
nohup free -h ls cat nohup.out
Display the first five lines of "nohup.out":
head -n 5 nohup.out
Check system performance with
vmstat
:vmstat vmstat -a
This guide covers essential Linux commands for beginners, offering practical examples and outputs to help you get started with basic file operations, linking, system monitoring, and process management.