03. Basic Shell Commands in Linux
π Master essential Linux shell commands! This tutorial covers file management, process control, permissions, redirection, and more, empowering you to confidently navigate the Linux environment. π§
What we will learn in this post?
- π Essential Linux Shell Commands
- π File and Directory Management Commands
- π Process Management Commands
- π File Permissions and Ownership
- π Redirection and Piping in Linux
- π Conclusion!
Essential Linux Shell Commands β¨
These commands are the building blocks of navigating and managing files in Linux.
Navigation & Listing πΊοΈ
pwd
(Print Working Directory): Shows your current location.pwd
cd
(Change Directory): Moves you to a different directory.cd /home/user
changes to the/home/user
directory.ls
(List): Shows files and directories in the current location.ls -l
(long listing) shows detailed information.
File Manipulation ποΈ
cp
(Copy): Copies files or directories.cp file1.txt file2.txt
copiesfile1.txt
tofile2.txt
.mv
(Move/Rename): Moves or renames files/directories.mv file1.txt newfile.txt
renamesfile1.txt
.rm
(Remove): Deletes files or directories. Use with caution!rm file.txt
deletesfile.txt
.rm -r directory
recursively deletes a directory (and its contents).mkdir
(Make Directory): Creates a new directory.mkdir new_directory
creates thenew_directory
.
Example: File Management Workflow
graph TD
A["π Create Directory"] --> B{"π Copy File?"};
B --> C["βοΈ Rename File"];
C --> D{"π List Files?"};
D --> E["π Remove File"];
%% Custom Styles
classDef actionStyle fill:#1E90FF,stroke:#00008B,color:#FFFFFF,font-size:14px,stroke-width:3px,rx:15px,shadow:5px;
classDef decisionStyle fill:#FFD700,stroke:#B8860B,color:#000000,font-size:14px,stroke-width:3px,rx:15px,shadow:5px;
classDef dangerStyle fill:#FF6347,stroke:#B22222,color:#FFFFFF,font-size:14px,stroke-width:3px,rx:15px,shadow:5px;
%% Apply Classes
class A actionStyle;
class C actionStyle;
class E dangerStyle;
class B decisionStyle;
class D decisionStyle;
For more in-depth information, explore these resources:
Remember to always be careful when using rm
, especially with the -r
flag, as deleted data is typically unrecoverable. Practice these commands in a safe environment (like a virtual machine) to avoid accidental data loss.
File & Directory Management in Linux π
Creating and Deleting
- Creating Files:
touch my_file.txt
creates an empty file namedmy_file.txt
.touch
is like giving a file a name. - Creating Directories:
mkdir my_directory
creates a new folder namedmy_directory
.mkdir -p dir1/dir2
creates nested directories. - Deleting Files:
rm my_file.txt
deletesmy_file.txt
. Userm -r my_directory
to remove a directory and its contents (be careful!).
Finding and Viewing
find
: Locates files based on criteria.find . -name "*.txt"
finds all.txt
files in the current directory and below.locate
: A faster, but less accurate, way to find files based on name (uses a database).locate my_file.txt
.tree
: Displays the directory structure in a tree-like format.tree .
shows the current directoryβs structure.
Other Useful Commands
cp source destination
: Copies files or directories.cp my_file.txt backup.txt
mv source destination
: Moves or renames files/directories.mv my_file.txt new_file.txt
rm -rf my_directory
: Recursively deletes a directory and its contents (use with extreme caution!).
Example: File Operations
graph TD
A["π Create file: `touch file.txt`"] --> B{"π File exists?"};
B -- "β
Yes" --> C["βοΈ Modify file"];
B -- "β No" --> C;
C --> D["π Delete file: `rm file.txt`"];
%% Custom Styles
classDef createStyle fill:#1E90FF,stroke:#00008B,color:#FFFFFF,font-size:14px,stroke-width:3px,rx:15px,shadow:5px;
classDef decisionStyle fill:#FFD700,stroke:#B8860B,color:#000000,font-size:14px,stroke-width:3px,rx:15px,shadow:5px;
classDef actionStyle fill:#32CD32,stroke:#006400,color:#FFFFFF,font-size:14px,stroke-width:3px,rx:15px,shadow:5px;
classDef dangerStyle fill:#FF6347,stroke:#B22222,color:#FFFFFF,font-size:14px,stroke-width:3px,rx:15px,shadow:5px;
%% Apply Classes
class A createStyle;
class B decisionStyle;
class C actionStyle;
class D dangerStyle;
For more information, check out the Linux Documentation Project. Remember to always back up your data before performing potentially destructive actions like using rm -rf
!
Linux Process Management: Your Handy Toolkit π οΈ
Monitoring Processes π
ps
: Shows running processes.ps aux
displays a detailed list.top
: Displays dynamic, real-time process information, constantly updated. Usetop
to see CPU and memory usage.htop
: An interactive, improved version oftop
with a user-friendly interface.
Managing Processes πΉοΈ
kill
: Terminates processes.kill <PID>
sends a termination signal;kill -9 <PID>
forces termination (use cautiously!).jobs
: Lists background jobs. Useful for managing processes started with&
.
Examples β¨
- List all processes:
ps aux
- Kill process with ID 1234:
kill 1234
- Forcefully kill process with ID 5678:
kill -9 5678
(Use with care!) - List background jobs:
jobs
Further Exploration π
For more in-depth information, explore the man
pages (e.g., man ps
, man kill
). Many online resources also offer comprehensive guides.
Note: Always be careful when using kill -9
, as it can lead to data loss if not used appropriately. Try a gentler kill
first.
File Permissions & Ownership π§βπ»
Every file has an owner, a group, and permissions controlling who can access it (read, write, execute).
Understanding Permissions π
Permissions are represented by three sets of three characters (e.g., rwxr-xr-x
). Each set applies to the owner, group, and others respectively. r
=read, w
=write, x
=execute. -
means no permission.
Example: rwxr-xr-x
- Owner: Read, write, and execute.
- Group: Read and execute.
- Others: Read and execute.
Modifying Permissions with chmod
βοΈ
chmod
changes permissions. You can use octal (e.g., chmod 755 file.txt
) or symbolic (e.g., chmod u+x file.txt
adds execute permission for the user) modes.
u
= user,g
= group,o
= others,a
= all+
= add,-
= remove,=
= set
Modifying Ownership with chown
& chgrp
π€π₯
chown
changes the owner, and chgrp
changes the group. For example:
chown john:users file.txt
changes the owner to βjohnβ and the group to βusersβ.chgrp developers file.txt
changes the group to βdevelopersβ.
For more info: chmod man page, chown man page
Mastering Command-Line Power: Redirection & Pipes β‘οΈ
Input/Output Redirection: Changing the Flow π
Imagine your commands as tiny workers. Redirection lets you control where they get their input from and send their output to.
Redirection Symbols:
>
: Sends output to a new file.ls -l > file_list.txt
createsfile_list.txt
containing thels -l
output.>>
: Appends output to an existing file.date >> file_list.txt
adds the current date to the file.<
: Takes input from a file.wc -w < my_document.txt
counts words inmy_document.txt
.
Pipes: Connecting Commands π
Pipes let you chain commands together, passing a commandβs output as the input to the next. Think of it as an assembly line!
Example: ls -l | grep "txt"
first lists files (ls -l
), then filters for only those containing βtxtβ (grep "txt"
).
Pipe Symbol:
|
: The pipe symbol connects commands.
graph LR
A["π List files: `ls -l`"] --> B{"π Pipe `|`"};
B --> C["π Filter: `grep 'txt'`"];
%% Custom Styles
classDef commandStyle fill:#1E90FF,stroke:#00008B,color:#FFFFFF,font-size:14px,stroke-width:3px,rx:15px,shadow:5px;
classDef pipeStyle fill:#FFD700,stroke:#B8860B,color:#000000,font-size:14px,stroke-width:3px,rx:15px,shadow:5px;
classDef filterStyle fill:#32CD32,stroke:#006400,color:#FFFFFF,font-size:14px,stroke-width:3px,rx:15px,shadow:5px;
%% Apply Classes
class A commandStyle;
class B pipeStyle;
class C filterStyle;
This shows ls -l
βs output feeding into grep "txt"
.
For more information:
- Linux Command Line Tutorial (General Linux commands and redirection/piping)
By mastering redirection and pipes, you can streamline your workflow and boost your command-line efficiency! π
Conclusion
And thatβs a wrap! We hope you enjoyed this post. π Weβd love to hear your thoughts β what did you think? Any questions or suggestions? Let us know in the comments below! π