Podcast
Questions and Answers
What command is used to list files and directories in long format?
Which command will create an empty file named 'newfile'?
What is the purpose of the 'pwd' command?
Which command correctly replaces 'old' with 'new' in line 2 to 4 of a file in vi?
Signup and view all the answers
Which of the following commands will create a nested directory structure?
Signup and view all the answers
What does the command 'cp -r dir1 dir2' do?
Signup and view all the answers
What command is used to delete a particular line while in vi?
Signup and view all the answers
In vi, how can you save and quit a file after editing?
Signup and view all the answers
Which command is used to delete the last line of a file?
Signup and view all the answers
What is the purpose of the command 'grep -c "linux" test_1'?
Signup and view all the answers
How can you print the 3rd line of a file using sed?
Signup and view all the answers
What does the command 'cut -d " " -f1 data' do?
Signup and view all the answers
Which command can print lines that start with a specific pattern?
Signup and view all the answers
What is the result of using 'sed '2,6s/linux/unix/g' filename'?
Signup and view all the answers
Which awk command retrieves the last column of a file?
Signup and view all the answers
Which command lists files that were modified more than 3 months ago?
Signup and view all the answers
What command would you use to stop a service named 'name' gracefully?
Signup and view all the answers
What does the umask command affect when set to '000'?
Signup and view all the answers
Which command would allow a user to view the contents of a file page-wise, allowing scrolling up and down?
Signup and view all the answers
What is the purpose of the 'sudo' command?
Signup and view all the answers
What does the umask setting of '022' imply for newly created files?
Signup and view all the answers
How would you add a new user named 'ABC' and give them sudo permissions?
Signup and view all the answers
What does the 'uniq' command do?
Signup and view all the answers
Which command would you use to print the current system date?
Signup and view all the answers
What command should be used to find files that have been modified in the last 50 minutes?
Signup and view all the answers
Which command will successfully list all files with a size greater than 1MB?
Signup and view all the answers
What is the purpose of using maxdepth
in the find
command?
Signup and view all the answers
How does a hard link differ from a soft link?
Signup and view all the answers
Which command would you use to delete all files modified over 3 months ago?
Signup and view all the answers
What will happen if you delete the original file that a soft link points to?
Signup and view all the answers
Which command would you use to forcefully stop a running process?
Signup and view all the answers
What does the command ps -ef | grep 'processname'
accomplish?
Signup and view all the answers
What command would you use to move multiple files into a directory?
Signup and view all the answers
What does the command 'chmod 777 filename' do?
Signup and view all the answers
Which of the following commands shows the disk usage of all files and directories in the present directory?
Signup and view all the answers
What does the command 'echo -e "hi\nhow r u"' do?
Signup and view all the answers
How do you display only the last 5 lines of a file named 'filename'?
Signup and view all the answers
What will the command 'head -99 file | tail -1' display?
Signup and view all the answers
What command would you use to find the count of words in the 99th line of a file?
Signup and view all the answers
What is the purpose of the command 'grep -i "Linux" test_1'?
Signup and view all the answers
Which command is used to change the ownership of a file or directory?
Signup and view all the answers
What does the symbol '|' (pipe) do in command line operations?
Signup and view all the answers
Study Notes
### File and Folder Management
-
ls
command is used to list files and directories. To get a more detailed information, usels -l
. -
touch
command is used to create an empty file. -
mkdir dirname
is used to create a directory. -
cd dirname
is used to change the working directory. -
cd..
is used to navigate one level up in the directory structure. -
cd ../../..
navigates three levels up in the directory structure. -
mkdir -p temp1/temp2/temp3
is used to create a complete directory structure, including intermediate directories.
### File Editing with vi
-
vi
editor is used to edit files. - To enter insert mode, press
esc
followed byi
. - To save and quit the file, press
esc
followed by:wq!
. - To quit without saving, press
esc
followed by:q!
. -
:%s/current-string/new-string/g
is used to replace all instances of a string in a file. -
4s/linux/windows/g
replaces all instances of "linux" to "windows" in the 4th line. -
2,4s/linux/windows/g
replaces all instances of "linux" to "windows" from the 2nd to 4th line. -
2,$s/linux/windows/g
replaces all instances of "linux" to "windows" from the 2nd line to the end of the file. -
:set nu
enables line numbers in vi -
:set nonu
disables line numbers in vi. -
:line_number
moves the cursor to the specified line number. -
dd
deletes the current line in vi.
File Copying and Moving
-
cp
is used to copy files and directories. -
cp file1 file2
copies file1 to file2. -
cp test1 temp1/temp2/temp3/
copies test1 file to the specific directory path. -
cp -r dir1 dir2
copies directory dir1 to dir2, including all subdirectories and files. -
cp test1 file1 test3 temp
copies multiple files or directories to a destination directory. -
mv
is used to rename or move files and directories. -
mv file1 file2
renames file1 to file2. -
mv file1 dir2/
moves file1 to directory dir2. -
mv file1 file2 file3 dir1
moves multiple files to a directory.
File Permissions and Ownership
-
chmod
is used to change the permissions of a file or directory. -
chmod 777 filename
changes the permissions of the file to read, write, and execute for owner, group, and others. -
chmod -R 777 dir
recursively changes the permissions of all files and directories within the specified directory. -
chown newowner filename
changes the owner of the file to newowner. -
chown newowner:groupname filename
changes the owner and group of the file.
File Size and Disk Usage
-
df -h
displays the disk usage of the mounted file systems. -
du -sh filename
displays the size of a file in human-readable format. -
du -sh *
displays the size of all files and directories in the current directory.
Basic Output and Redirection
-
echo "hi how r u"
prints the string to the terminal. -
echo -e "hi\nhow r u"
prints the string with newline character. -
>
redirects the output of a command to a file. -
ls -lrt > log
redirects the output ofls -lrt
command to log file. -
>>
appends the output of a command to the end of a file. -
echo "this is linux class" >> log
appends the stringthis is linux class
to the log file.
File Counting and Content Manipulation
-
wc
command is used to count lines, words and characters in a file. -
wc -l log
counts the number of lines in the log file. -
wc -w log
counts the number of words in the log file. -
wc -c log
counts the number of characters in the log file. -
head
command is used to display the first n lines of a file. -
head -3 filename
displays the first 3 lines of the file. -
head filename
displays the first 10 lines of the file. -
tail
command is used to display the last n lines of a file. -
tail -3 filename
displays the last 3 lines of the file. -
tail filename
displays the last 10 lines of the file. -
|
(pipe) passes the output of one command as input to another command. -
ls -lrt | wc -l
counts the number of files and directories in the current directory. -
head -4 log | tail -1
displays the 4th line of the file. -
head -99 file | tail -1
displays the 99th line of the file. -
head -7 file | tail -3
displays the 4th to 7th line of the file. -
head -99 log | tail -1 | wc -w
counts the number of words in the 99th line of the file. -
tail -2 log | head -1
displays the 2nd last line of the file.
Pattern Searching with grep
-
grep
is used to search for patterns in files. -
grep "pattern" filename
searches for the specified pattern in the file. -
grep -w "string" filename
searches for the exact word specified. -
grep -i "Linux" test_1
searches case-insensitively for "Linux" in the file. -
grep -e "linux" -e "windows" test_1
searches for either "linux" or "windows". -
egrep
command can also be used to search for multiple patterns. -
grep -R -l "linux" *
lists the filenames which contain "linux". -
grep -v "linux" test_1
prints lines that don't contain "linux". -
grep "^pattern" filename
lists lines starting with "pattern". -
grep "s$" test_1
lists lines ending with "s". -
grep -c "linux" test_1
counts the number of lines containing "linux".
### Replacing Strings with sed
-
sed
is used to replace strings in files. -
sed 's/linux/unix/g' filename
replaces "linux" with "unix" in the file. -
sed -i 's/linux/unix/g' test_1
replaces "linux" with "unix" in the file and saves the changes. -
sed '4s/linux/unix/g'
replaces "linux" with "unix" in the 4th line. -
sed '2,6s/linux/unix/g'
replaces "linux" with "unix" from the 2nd to 6th line. -
sed '$s/linux/unix/g'
replaces "linux" with "unix" in the last line -
sed '4d' test_1
deletes the 4th line of the file. -
sed '$d' test_1
deletes the last line of the file. -
sed '2,6d' test_1
deletes lines from the 2nd to 6th line. -
sed '1d;3d' test_1
deletes the 1st and 3rd line. -
sed -n '3p' test_1
prints the 3rd line. -
sed -n '$p' test_1
prints the last line. -
sed -n '2,6p' test_1
prints lines from the 2nd to 6th line.
Column and Row Based File Cutting
-
cut
is used to cut files column-wise. -
cut -d " " -f1 data
prints the first column. -
cut -d " " -f1,3 data
prints the 1st and 3rd column. -
cut -d " " -f3-6 data
prints the 3rd to 6th column. -
awk
is used to cut files row-wise and column-wise. -
awk -F " " '{print $1}' data
prints the first column. -
awk -F " " '{print $NF}' data
prints the last column. -
awk -F " " '{print $(NF-1)}' data
prints the second last column.
### File Searching with find
-
find
is used to search for files. -
find . -iname "filename"
searches for "filename" case-insensitively. -
find . -type f -mtime +90
lists files modified more than 90 days ago. -
find . -type d -mtime +90
lists directories modified more than 90 days ago. -
find . -mtime +90
lists both files and directories modified more than 90 days ago. -
find . -type d -mtime -90
lists directories modified within 90 days. -
find . -type f -mmin -50
lists files modified within 50 minutes. -
find . -type f -mmin +50
lists files modified 50 minutes ago. -
find . -type f -perm 777
lists files with permissions 777. -
find . -type f -empty
lists empty files. -
find . -maxdepth 1 -iname "test1"
restricts the search to the first-level directory. -
xargs
is used to pass the output of one command as arguments to another command. -
find . -type f -mtime +90 | xargs rm -rf
deletes all files modified more than 90 days ago.
### Symbolic Links and Hard Links
-
ln
is used to create symbolic links and hard links. -
ln -s /home/ec2-user/file1 link1
creates a symbolic link to file1. -
ln /home/ec2-user/file1 hard1
creates a hard link to file1. - Deleting the actual file breaks a symbolic link but not a hard link.
Process Management
-
ps
is used to list currently running processes. -
ps -ef | grep "processname"
checks if a specific process is running. -
kill -9 PID
force-stops a process. -
sleep
command can be used to pause execution for a specified duration. -
sudo service name stop
stops a service gracefully. -
sudo service name start
starts a service. -
sudo service name restart
restarts a service. -
sudo systemctl stop name
stops a systemd service.
Permissions and Access Control
-
umask
sets default permissions for newly created files and directories. -
umask 000
sets full permissions (read, write, and execute) for the owner, group, and others. -
umask 777
sets no permissions for the owner, group, and others. -
su username
switches to another user or superuser. -
sudo
executes a command with root privileges. -
sudo useradd ABC
adds a new user ABC. -
sudo passwd ABC
sets password for user ABC. -
sudo su -
logs in as root user from ec2-user. - Edit
/etc/sudoers.d/90-cloud-init-users
to grant sudo permissions to specific users.
Displaying File Content
-
more
displays the content of a file page by page. -
less
also displays content page by page but allows scrolling up and down.
System Date and Time
-
date
prints current system date and time. -
date +%m
prints the current month.
Deleting Duplicate Lines
-
uniq
prints only unique lines from the input, removing duplicate lines.
Assigment for practice
- Write a command to list files larger than 1 MB.
- Use the
-exec
option in thefind
command to delete files larger than 10 MB. - Write the steps to kill all processes started by a specific user.
- Write the steps to kill all processes with a specific name.
- Write a command to display 99th to 103th line of a file.
- Find all files and directories in the current directory that have been modified within the last 3 days and recursively change their permissions to 755.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge on file and folder management commands and using the vi
editor. This quiz covers essential commands like ls
, mkdir
, and editing techniques in vi
. Challenge yourself and see how well you understand these important tools!