Linux-Assignment-4

  • In this repo I'm documenting 25 Linux Questions as a part of Keenable internship. Let's start the adventure with terminal..

Ques 1: Create a directory named "MyFiles" in your home directory. Navigate into this directory and list its contents.

cd /home
sudo mkdir MyFiles
cd MyFiles
 ls

Output

Screenshot from 2024-02-05 12-08-10

Ques 2: Copy a file named "document.txt" from your home directory to the "MyFiles" directory. Move the file to a subdirectory named "Documents" within "MyFiles."

cd /home
sudo touch document.txt
sudo cp document.txt MyFiles/
cd MyFiles/
sudo mkdir -p Document
sudo mv MyFiles/document.txt MyFiles/Document

output

Screenshot from 2024-02-05 12-30-15

Ques 3 : Create an empty file named "notes.txt" in the "MyFiles" directory. Afterward, delete the file.

cd /home/MyFiles/
sudo touch notes.txt
sudo rm -rf notes.txt

Output

Screenshot from 2024-02-05 12-35-44

Ques 4 : Create a hard link named "hardlink.txt" for the file "document.txt" within the "Documents" subdirectory. Also, create a symbolic link named "symlink.txt" in the same location.

sudo cd /home/MyFiles/documents
sudo ln document.txt hardlink.txt
sudo ln -s document.txt symlink.txt

Output

image

Ques 5 : In the "MyFiles" directory, use a single command to list all files that start with the letter "a" and have a ".txt" extension.

sudo ls /home/MyFiles/a*.txt

Output

image

Ques 6: Rename all files in the "Documents" subdirectory of "MyFiles" with a ".bak" extension. Ensure the original file names are preserved.

cd /home/MyFiles
sudo vim rename.sh
#!/bin/bash

rename_extension=".bak"

for file in *; do
    if [ -f "$file" ]; then
        rename_file="$file$rename_extension"
        cp "$file" "$rename_file"
        echo "Renamed File $file as $rename_file"
    fi
done
cd Document
touch a.txt b c.txt d.txt
sudo bash ../rename.sh

Output

Screenshot from 2024-02-09 18-39-58

Ques 7: Use a wildcard character to copy all files from the "Documents" subdirectory of "MyFiles" to another directory named "Backup."

cd /home
cd MyFiles
sudo mkdir backup
cd
sudo cp /home/MyFiles/Document/* /home/MyFiles/backup/

Output

Screenshot from 2024-02-09 18-46-37

Ques 8 :Execute the ls command to list files in the current directory. Save the output to a file named "file_list.txt." Then, use a pipe to filter the output through grep to display only files with a ".txt" extension.

sudo su
cd /home/MyFiles/
touch file_list.txt
cd Document
chmod +rwx a.txt b
cd ..
chmod +rwx file_list.txt
cd Document
ls > ../file_list.txt
grep '\.txt$' file_list.txt

Output

Screenshot from 2024-02-02 11-03-35

Ques 9: Create a new text file named "my_notes.txt" using the touch command. Open the file in the Vim editor, add some text, and save the changes.

cd /home/MyFiles/
sudo touch my_notes.txt
sudo vim my_notes.txt

Output

Screenshot from 2024-02-10 17-39-52

image

Ques 10 :Run the date command and store the output in a variable named "current_date." Display the value of the variable and append it to the "my_notes.txt" file.

cd /home/MyFiles/
sudo su
current_date=$(date)
echo "current date : $current_date"
echo "$current_date" >> my_notes.txt

Output

image

image

Ques 11 : Edit the Bash startup script (e.g., .bashrc) to set an environment variable named "CUSTOM_PATH" to a specific directory path. Ensure the variable is available in new shell sessions.

vim .bashrc
export CUSTOM_PATH="/home/MyFiles/Document"
source ~/.bashrc
echo $CUSTOM_PATH

Output

Screenshot from 2024-02-02 14-08-23

Screenshot from 2024-02-02 14-09-58

Ques 12 : Use the echo command to add a new line of text to the "my_notes.txt" file without overwriting existing content. Verify that the new text is appended.

cd /home/MyFiles
sudo su
echo "I am writing text in new line" >> my_notes.txt
cat my_notes.txt

Output

image

Ques 13 : List all files in the "/etc" directory, filter the output to include only those containing the word "conf," and save the result to a file named "conf_files.txt."

ls /etc | grep 'conf' > conf_files.txt
vim conf_files.txt

Ques 14 : Open the "my_notes.txt" file in Vim. Use Vim's search and replace functionality to replace all occurrences of the word "important" with "critical." Save the changes.

cd /home/MyFiles
sudo vim my_notes.txt

**After opening file write below command after press shift + colon

%s/important/critical/g

Output

Screenshot from 2024-02-03 10-39-06

Ques 15 : Create a new user account named "john_doe." Set the user's home directory to "/home/john_doe" and assign the user to the "users" group.

sudo useradd -m -d /home/john_doe -g users john_doe

Output

image

Ques 16 : Add the user "john_doe" to the sudoers file, allowing them superuser privileges. Confirm that "john_doe" can execute commands with sudo.

sudo visudo

Inside visudo file write below content and save it and exit

john_doe ALL=(ALL:ALL) ALL

Write any desire path

sudo ls /Desktop
sudo su

Output

image

Ques 17 :Modify the user account "john_doe" to change the default shell to "/bin/bash" and set the account's expiration date to one month from today.

sudo chsh -s /bin/bash john_doe
sudo chage -E $(date -d "+1 month" +"%Y-%m-%d") john_doe
sudo chage -l john_doe

Output

image

Ques 18 :Create a new group named "development_team." Add "john_doe" to this group and verify the group's existence.

sudo groupadd development_team
sudo usermod -aG development_team john_doe
getent group development_team

Output

image

Ques 19 : Remove "john_doe" from the "users" group and add them to the "development_team" group. Confirm the changes.

sudo gpasswd -d john_doe users
sudo usermod -aG development_team john_doe
groups john_doe

Output

image

Ques 20 :Delete the user account "john_doe" and ensure that their home directory is also removed.

sudo userdel -r john_doe
groups jhon_doe

Output

image

Ques 21 : Delete the group "development_team" and ensure that all users previously belonging to the group are appropriately handled.

getent group development_team
sudo usermod -g users jhon_doe
sudo groupdel development_team
getent group development_team

Output

Screenshot from 2024-02-03 11-56-05

Ques 22 : Implement a password policy that requires users to change their passwords every 90 days. Apply this policy to all existing and new user accounts.

sudo chage -M 90 -m 0 -W 7 -I 30 -E -1 $(cut -d: -f1 /etc/passwd)
sudo vim /etc/login.defs

Inside this file add max pass day as 90 and min pass day as 0

PASS_MAX_DAYS   90
PASS_MIN_DAYS   0

Output

Screenshot from 2024-02-03 12-12-05

Screenshot from 2024-02-03 12-12-58

Ques 23 : Manually lock the user account "john_doe." Attempt to log in as "john_doe" to confirm that the account is locked. Then, unlock the account.

sudo passwd -l john_doe
su - john_doe
sudo passwd -u john_doe

Output

Screenshot from 2024-02-03 12-21-10

Ques 24 : Use the id command to display detailed information about the "john_doe" user, including user ID, group ID, and supplementary groups.

id john_doe
id -u john_doe
id -g john_doe
id -G john_doe

Output

Screenshot from 2024-02-03 12-27-32

Ques 25 : Configure the password aging for the user "john_doe" to enforce a maximum password age of 60 days. Confirm that the changes take effect.

sudo chage -M 60 john_doe
sudo chage -l john_doe

Output

Screenshot from 2024-02-03 12-32-24