- 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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