File Structure
/bin
: User binaries (Common Linux command need to use in single user mode)/sbin
: System binaries (Commands under this is for system maintenance purpose used by system admins)/etc
: Config files (etc: extended text configuration)/dev
: Device files/proc
: Process info (Contains running process info, once system get reboot then all the related files will get clear and will create new info. because it retrieves information from kernel and memory.)/var
: Variable files (It contains logs)/tmp
: Temporary files (Contains temporary files created by system and user, once system get reboot then all the files will get clear)/usr
: User programs (USR: Unix system resource. Contains binaries, libraries documentation files. Under it we have bin (Ex: less, awk) and sbin (Ex: cron, sshd) which contains binary files for user programs.)/home
: Home directories/boot
: Boot loader files (kernel files, grub files etc)/lib
: System libraries (Contains library files that supports the binaries that are located in /bin and /sbin)/opt
: Optional add-on apps (Contains add on installed application related files)/mnt
: Mount directory (To mount any foreign device temporarily)/media
: Removable device (To mount any removable device)/srv
: Service data (Contains server specific service data)
Basic Symbols in Linux:
Before we get into commands, let’s talk about important special characters. The dot (.), dot-dot (..), forward slash (/), and tilde (~), all have special functionality in the Linux filesystem:
- The dot (.) represents the current directory in the filesystem.
- The dot-dot (..) represents one level above the current directory.
- The forward slash (/) represents the "root" of the filesystem. (Every directory/file in the Linux filesystem is nested under the root / directory.)
- The tilde (~) represents the home directory of the currently logged in user.
The dash (-) navigates back to the previous working directory, similar to how you can navigate to your user home directory with ~. If you need to go back to our deeply nested directory under your user home directory (this was my previous working directory), you will issue this command:
Some More Basic Symbols:
| S:NO | Symbol | Explanation | Examples | | --- | --- | --- | --- | | 1 | / | The forward slash (/) represents the "root" of the filesystem. (Every directory/file in the Linux filesystem is nested under the root / directory.) / Also use for directory separation and path separation | / is a root directory | | /home/user/sample/test.txt | | 2 | ~ | is equal to the current user's home directory. Ex: /home/someone/ | cd ~ | | ls ~ | | 3 | * | A symbol which stands for "everything". Let's say you want to remove all the .jpg files from your Downloads folder which have their name starting with the "E" character, then you can use this symbol to represent all the other letters except E. See the example. | rm ~/Downloads/E*.jpg | | ls /etc/*c | | nano /var/log/nginx/* | | 4 | & | Run a command in the background. It will return the PID of the newly running process to you and won't show you the output. | sudo apt update & | | 5 | && | These symbols written together stand for "and". So, if you want to run 2 commands together, you can use it. | sudo apt update && sudo apt upgrade | | 6 | \ | Allows you to continue writing commands/Bash syntax in new line. | sudo \ | | apt \ | | update | | 7 | .. | In many cases, especially in navigation, the two dots stand for the parent folder. | cd .. | | 8 | . | In navigation or referring to files/folders, the dot stands for the current folder. | ls . | | 9 | # | Everything after this symbol in the same line is considered to be a comment, so it won't be processed by the shell. | cd # This command moves you somewhere. | | 10 | | | This is called "Piping", which is the process of redirecting the output of one command to the input of another command. Very useful and common in Linux/Unix-like systems. | cat /etc/profile | grep bash | | 11 | \> | Take the output of a command and redirect it into a file (will overwrite the whole file). | ls ~ > output.txt | | 12 | < | Read the contents of a file into the input of a command. | grep bash < /etc/profile | | 13 | \>> | Append a text or a command output into the last line of a file. | echo "First Line" > output.txt | | echo "See this is the last line" >> output.txt |
File Type
(-)
: (Regular file) (Ex: text file, image file, binary...)
d
: (Directory)
c
: (Character device file) | (Character and block device files allow users and programs to communicate with hardware peripheral device) server console is a character device file. Talks to devices in a character by character (1 byte at a time)
b
: (Blockdevice file) | (Ex: storage LUN files) Talks to devices 1 block at a time (1 block = 512 bytes to 32KB)
s
: (Local socket file used for communication between processes)
p
: (Named Pipe)
l
: (Symbolic link)
"file
" command is used to identify the file type.
"stat
" command is used to view details (Access, Modify, change time, size, inode, block ...)
"ln
" command is used to make link between files.
Syntax: ln <file> <hard link> with -s option we can create soft link file.
Hard link: It acts like a mirror copy and share the same inode. When you delete hard Link, nothing will happen to the other file. Hard link can't create across the file system. Advantage of the hard link is it consumes 1 files space but if we delete any of the file then we have 1 more backup of the file, because both the files are sharing same inode.
Soft link: It is actual link to the original file, and it have a different inode. Soft link points to the original file so if original file is deleted then the soft link fails. If you delete soft link, then nothing will happen. Soft link can create across the file system because it has different inode number.
dd command: It is used to take backup of any fs or disk and to check the disk. Ex: #dd if=/dev/sdc of=/tmp/sdc.bkp bs=1024 count=5
File Permission
- Create a simple file and run
ls -ltr
to see the details of the files.
Read about ACL and try out the commands
getfacl
andsetfacl
.Task: Create a directory and set specific ACL permissions for different users and groups. Verify the permissions using
getfacl
.Create a script that changes the permissions of multiple files in a directory based on user input.
change_permissions.sh
#!/bin/bash
# Function to change file permissions
change_permissions() {
local directory=$1
local permission=$2
# Check if directory exists
if [ ! -d "$directory" ]; then
echo "Directory does not exist!"
exit 1
fi
# Iterate over all files in the directory
for file in "$directory"/*; do
if [ -f "$file" ]; then
chmod "$permission" "$file"
echo "Changed permissions of $file to $permission"
fi
done
}
# Get user input for directory
read -p "Enter the directory path: " dir_path
# Get user input for file permission
read -p "Enter the file permission (e.g., 755, 644): " file_perm
# Call the function to change permissions
change_permissions "$dir_path" "$file_perm"
echo "All permissions updated."
Write a script that sets ACL permissions for a user on a given file, based on user input.
acl_permissions.sh
!#/bin/bash
echo "Enter the file path"
read file_path
echo "Enter the owner name"
read owner
echo "Enter the permissions (e.g., rwx)"
read permissions
sudo setfacl -m u:$owner:$permissions $file_path
echo "ACL permissions set successfully"
Understanding Sticky Bit, SUID, and SGID:
Read about sticky bit, SUID, and SGID.
Sticky bit: Used on directories to prevent users from deleting files they do not own.
SUID (Set User ID): Allows users to run an executable with the permissions of the executable's owner.
SGID (Set Group ID): Allows users to run an executable with the permissions of the executable's group.
Task: Create examples demonstrating the use of sticky bit, SUID, and SGID, and explain their significance
Sticky bit
SUID
SGID