Mastering Linux Commands: grep, Filters, I/O Redirection, and Piping
Welcome back to our Linux command series! Today, we will explore some powerful commands that will help you become more efficient in handling text and data in the Linux environment. We'll cover the mighty 'grep' command, various filter commands, I/O redirection techniques, and the concept of piping.
1. The Versatile 'grep' Command
The 'grep' command is a text search tool used to find specific patterns or words within a file or text input. It's a handy tool for extracting valuable information from large datasets.
Example: To find all lines in the passwd file that contain the word "user," you can use:
grep "user" /etc/passwd
If you want to display all lines except those containing the word "user," you can use the '-v' option:
grep -v "user" /etc/passwd
2. Filter Commands
Linux offers several filter commands to manipulate and display file content in various ways. Let's take a look at a few of them:
a. 'less': This command allows you to view file content page-wise or line-wise.
b. 'more': Similar to 'less,' but it displays one page at a time, and you can navigate using the spacebar.
c. 'head': Displays the first few lines of a file (by default, 10 lines).
d. 'tail': Shows the last few lines of a file (by default, 10 lines).
e. 'cut': Used to extract specific columns from a file.
cut -d: -f1,3 /etc/passwd
f. 'sed': A powerful stream editor used to find and replace text within a file.
sed 's/tech/tools/g' devopstools.txt > newfile.txt
3. I/O Redirection
I/O redirection is a handy technique that allows you to control the input and output of commands.
a. Creating a file and redirecting content to it:
echo "This is a sample content." > devopstools.txt
b. Searching for "tech" and replacing it with "tools":
sed 's/tech/tools/g' devopstools.txt > newfile.txt
c. Appending more output to the same file:
echo "This is additional content." >> newfile.txt
d. Redirecting only errors to a file:
ls /invalid_directory 2>> errors.txt
e. Redirecting both output and errors to a file:
ls /home &>> output_and_errors.txt
4. Piping Data Between Programs
Piping is a powerful mechanism that enables you to pass the output of one command as input to another command.
Example: To list all files in the current directory and then count the number of files, you can use piping as follows:
ls | wc -l
Linux Users and Groups
concepts of users and groups are essential for managing who can access what and ensuring the security of the system. Understanding users and groups allows you to control what different people can do on the Linux system and keep everything safe.
Users: Different Types and Their Details
In Linux, a user is an individual who interacts with the system. Users can be categorized into three types:
1. ROOT User:
The root user is the superuser who holds ultimate power over the system. This user has User ID (UID) 0 and Group ID (GID) 0, granting unrestricted access to all files and commands. Being the system administrator, utmost caution is required while using the root account to prevent accidental damage to the system.
User Name: root
User ID (UID): 0
Group ID (GID): 0
Home Directory: /root
Default Shell: /bin/bash
2. Regular Users:
Regular users are typical individuals who use the system for various purposes. Each regular user is assigned a unique UID and GID to maintain segregation between user accounts.
User Names: Abdul, vagrant (examples)
User ID (UID): 1000 to 60000 (typical range)
Group ID (GID): 1000 to 60000 (corresponding GIDs)
Home Directory: /home/username (specific to each user)
Default Shell: /bin/bash
3. Service Users:
Service users are dedicated accounts created for running specific system services or applications. These users typically have limited permissions and are meant for system processes.
User Names: ftp, ssh, apache (examples)
User ID (UID): 1 to 999 (usual range)
Group ID (GID): 1 to 999 (matching GIDs)
Home Directory: /var/ftp, /etc, etc. (varies based on service)
Default Shell: /sbin/nologin
Understanding the Files: /etc/passwd and /etc/group
Now that we know about users, let's explore the two vital files that store user and group information.
The /etc/passwd file contains user account details, such as username, UID, GID, home directory, and default shell. Each line in the file represents a unique user.
The /etc/group file contains group information. Each line represents a single group and includes details such as group name, group password (rarely used), GID, and a list of group members.
Essential User and Group Management Commands
Linux provides several commands to manage users and groups effectively. Let's go through some essential ones:
useradd (RedHat) / adduser (Ubuntu): Creates a new user account on the system.
id: Displays information about a specific user, including their UID and GID.
groupadd: Creates a new group on the system.
usermod -G groupname username: Adds a user to a specific group, allowing them access to group resources.
passwd: Sets or resets a user's password.
userdel -r username: Removes a user account along with their home directory.
groupdel: Removes a group from the system.
last: Shows a list of the last logged-in users on the system.
who: Displays a list of users currently logged into the system.
whoami: Shows the username of the current user.
lsof -u username: Lists files opened by a specific user, aiding in troubleshooting.
In Linux, each file and directory is associated with three types of permissions: read, write, and execute, denoted by letters 'r,' 'w,' and 'x,' respectively. These permissions are assigned to three different user groups: the file owner, the group members, and others (everyone else). By controlling these permissions, you can regulate access to your files and directories.
File Permissions Notation:
Before diving into the commands, let's understand how file permissions are represented. When listing files with the
ls -l command, you'll see an output like this:
-rw-r--r-- 1 user users 1024 Jul 31 2023 myfile.txt
The first character represents the file type ('-' for regular files, 'd' for directories). The next nine characters represent the permissions for the file owner, the group members, and others, respectively.
Commands for Managing File Permissions:
chmod: The chmod command is used to change file permissions. It can be used in two ways: symbolic notation and octal notation.
chmod u=rw,g=r,o=r myfile.txt: This grants read and write permissions to the file owner (user), read permissions to the group members (group), and read permissions to others.
script.sh: This adds execute permissions to the file for all user groups.
chmod 644 myfile.txt: This sets the same permissions as the first example using numeric values. The first digit (6) represents the file owner's permissions (rw-), and the next two digits (44) represent the group and others' permissions (r--).
chown: The chown command allows you to change the file's owner and group. Only the superuser (root) can change ownership.
chown user1 myfile.txt: This changes the file owner to user1.
chown user2:group2 myfile.txt: This changes both the file owner to user2 and the group to group2.
chgrp: The chgrp command allows you to change the group ownership of a file.
chgrp group3 myfile.txt: This changes the group ownership of the file to group3.
umask: The umask command determines the default permissions applied when creating new files and directories. It subtracts the umask value from the default permission (usually 666 for files and 777 for directories).
umask 022: This sets the default permissions to rw-r--r-- for files and rwxr-xr-x for directories.
Linux is a really powerful computer system that can do lots of amazing things. One cool thing it can do is use filter and I/O redirection commands. These commands help people manage and organize information in a smart way. They can also control where the computer gets input from and where it sends output to. Linux also lets us create different users and groups, kind of like clubs, to keep things organized and secure. Each user or group can have special permissions, like rules, for what they can and can't do on the computer. This helps keep important stuff safe and only accessible to the right people. So, Linux gives us lots of control and options to make our computers work just the way we want them to.
Did you find this article valuable?
Support DevOps 0 to 1 by becoming a sponsor. Any amount is appreciated!