This README file provides a brief introduction to some essential Linux commands for beginners.
- Navigating the File System
- File Management
- Directory Management
- Permissions
- Viewing File Contents
- System Information
-
To print the current working directory:
pwd -
To change the current working directory:
cd [directory] -
To list the contents of a directory:
ls [options] [directory]
-
To create a new file:
touch [file] -
To copy a file:
cp [source] [destination] -
To move a file:
mv [source] [destination] -
To remove a file:
rm [file]
-
To create a new directory:
mkdir [directory] -
To remove an empty directory:
rmdir [directory] -
To remove a non-empty directory:
rm -r [directory]
-
To change file or directory permissions:
chmod [permissions] [file or directory] -
To change file or directory ownership:
chown [owner] [file or directory]
-
To display the contents of a file:
cat [file] -
To display the first few lines of a file:
head [file] -
To display the last few lines of a file:
tail [file]
-
To display system information:
uname -a -
To display disk usage:
df -h -
To display memory usage:
free -h
This section provides an in-depth explanation of the chmod and chown commands, including examples.
| Operator | Description |
|---|---|
u |
User |
g |
Group |
o |
Others |
a |
All (User, Group, and Others) |
| Permission | Description |
|---|---|
r |
Read |
w |
Write |
x |
Execute |
| Action | Description |
|---|---|
+ |
Add permission |
- |
Remove permission |
= |
Set permission |
To add execute permission for the user:
chmod u+x [file or directory]
To remove write permission for the group and others:
chmod go-w [file or directory]
To set read and execute permissions for the user, and read permission for the group and others:
chmod u=rw,g=r,o=r [file or directory]
| Octal Value | Permission |
|---|---|
0 |
No permission |
1 |
Execute |
2 |
Write |
3 |
Write and Execute |
4 |
Read |
5 |
Read and Execute |
6 |
Read and Write |
7 |
Read, Write, Execute |
| Octal Value | Permission | Description | Example |
|---|---|---|---|
0 |
No permission | No permissions are granted | chmod 000 file.txt |
1 |
Execute | Execute permission only | chmod 111 script.sh |
2 |
Write | Write permission only | chmod 222 file.txt |
3 |
Write and Execute | Write and execute permissions | chmod 333 script.sh |
4 |
Read | Read permission only | chmod 444 file.txt |
5 |
Read and Execute | Read and execute permissions | chmod 555 script.sh |
6 |
Read and Write | Read and write permissions | chmod 666 file.txt |
7 |
Read, Write, Execute | All permissions (read, write, and execute) | chmod 777 script.sh |
To set permissions using octal values, use the chmod command followed by the octal value for user, group, and others:
chmod [user][group][others] [file or directory]
Example:
To set read, write, and execute permissions for the user, read and execute permissions for the group, and read permissions for others:
chmod 754 script.sh
You can combine octal permissions to create custom permission sets for your files and directories. Each digit in the octal value represents the permissions for the user, group, and others, respectively.
- To set read and write permissions for the user, and read permissions for the group and others:
chmod 644 file.txt
- To set read, write, and execute permissions for the user, and read permissions for the group and others:
chmod 744 script.sh
- To set read and execute permissions for the user, group, and others:
chmod 555 script.sh
Remember to carefully choose permissions based on the security requirements
To set read, write, and execute permissions for the user, read and write permissions for the group, and read permission for others:
chmod 764 [file or directory]
chown [owner]:[group] [file or directory]
To change the owner of a file or directory to newuser and the group to newgroup:
chown newuser:newgroup [file or directory]
To change only the owner to newuser:
chown newuser [file or directory]
To change only the group to newgroup:
chown :newgroup [file or directory]
- Set read-only permission for a
.pemfile:
chmod 400 private_key.pem
This restricts the .pem file to be readable only by the owner, ensuring the private key remains secure.
- Set read, write, and execute permissions for the owner, and read and execute permissions for the group and others on a
script.shfile:
chmod 755 script.sh
This allows the owner to read, write, and execute the script, while others can only read and execute it.
- Set read, write, and execute permissions for the owner, group, and others on a
script.shfile:
chmod 777 script.sh
This allows everyone to read, write, and execute the script, which can be a security risk and is generally not recommended.
- The
chmodandchowncommands can be used recursively with the-Roption to change permissions or ownership for a directory and its contents:
chmod -R [permissions] [directory]
chown -R [owner]:[group] [directory]
- You can use the
umaskcommand to set default file permissions for newly created files:
umask [octal value]
- You can use the
idcommand to display information about the current user and group:
id
For more information and examples, consult the man pages for each command:
man chmod
man chown