File and Folder Management Quiz

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What command is used to list files and directories in long format?

  • ls -lt
  • ls -l (correct)
  • ls
  • ls -a

Which command will create an empty file named 'newfile'?

  • create newfile
  • touch newfile (correct)
  • mkdir newfile
  • makefile newfile

What is the purpose of the 'pwd' command?

  • To pause the terminal
  • To list all directories
  • To print the current time
  • To display the present working directory (correct)

Which command correctly replaces 'old' with 'new' in line 2 to 4 of a file in vi?

<p>esc:2,4s/old/new/g (D)</p> Signup and view all the answers

Which of the following commands will create a nested directory structure?

<p>mkdir -p dir1/dir2 (D)</p> Signup and view all the answers

What does the command 'cp -r dir1 dir2' do?

<p>Copies a directory and all its contents from dir1 to dir2 (B)</p> Signup and view all the answers

What command is used to delete a particular line while in vi?

<p>esc press dd (A)</p> Signup and view all the answers

In vi, how can you save and quit a file after editing?

<p>esc: :wq (D)</p> Signup and view all the answers

Which command is used to delete the last line of a file?

<p>sed '$d' filename (D)</p> Signup and view all the answers

What is the purpose of the command 'grep -c "linux" test_1'?

<p>Count the number of lines containing 'linux' (A)</p> Signup and view all the answers

How can you print the 3rd line of a file using sed?

<p>sed -n '3p' filename (C)</p> Signup and view all the answers

What does the command 'cut -d " " -f1 data' do?

<p>Cuts the first column using space as a delimiter (C)</p> Signup and view all the answers

Which command can print lines that start with a specific pattern?

<p>grep &quot;^pattern&quot; filename (A)</p> Signup and view all the answers

What is the result of using 'sed '2,6s/linux/unix/g' filename'?

<p>Replaces 'linux' with 'unix' from line 2 to 6 (B)</p> Signup and view all the answers

Which awk command retrieves the last column of a file?

<p>awk -F &quot; &quot; '{print $NF}' data (A)</p> Signup and view all the answers

Which command lists files that were modified more than 3 months ago?

<p>find . -type f -mtime +90 (B)</p> Signup and view all the answers

What command would you use to stop a service named 'name' gracefully?

<p>sudo service name stop (A)</p> Signup and view all the answers

What does the umask command affect when set to '000'?

<p>Created files and directories will have full permissions for all users (D)</p> 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?

<p>less filename (C)</p> Signup and view all the answers

What is the purpose of the 'sudo' command?

<p>To execute commands with root permission (C)</p> Signup and view all the answers

What does the umask setting of '022' imply for newly created files?

<p>Read and execute permissions for user and group, read-only for others (C)</p> Signup and view all the answers

How would you add a new user named 'ABC' and give them sudo permissions?

<p>sudo useradd ABC and edit /etc/sudoers (A)</p> Signup and view all the answers

What does the 'uniq' command do?

<p>Prints only unique data and removes duplicates (C)</p> Signup and view all the answers

Which command would you use to print the current system date?

<p>date +%Y-%m-%d (A)</p> Signup and view all the answers

What command should be used to find files that have been modified in the last 50 minutes?

<p>find . -type f -mmin -50 (D)</p> Signup and view all the answers

Which command will successfully list all files with a size greater than 1MB?

<p>find . -type f -size +1M (B)</p> Signup and view all the answers

What is the purpose of using maxdepth in the find command?

<p>To limit the command to a certain directory level (B)</p> Signup and view all the answers

How does a hard link differ from a soft link?

<p>A hard link points to the inode of a file (C)</p> Signup and view all the answers

Which command would you use to delete all files modified over 3 months ago?

<p>find . -type f -mtime +90 | xargs rm -rf (D)</p> Signup and view all the answers

What will happen if you delete the original file that a soft link points to?

<p>The soft link will become broken and unusable (C)</p> Signup and view all the answers

Which command would you use to forcefully stop a running process?

<p>kill -9 PID (A)</p> Signup and view all the answers

What does the command ps -ef | grep 'processname' accomplish?

<p>It searches for running processes with the name 'processname' (A)</p> Signup and view all the answers

What command would you use to move multiple files into a directory?

<p>mv file1 file2 file3 dir1 (D)</p> Signup and view all the answers

What does the command 'chmod 777 filename' do?

<p>Grants read, write, and execute permissions to all user types (A)</p> Signup and view all the answers

Which of the following commands shows the disk usage of all files and directories in the present directory?

<p>du -sh * (D)</p> Signup and view all the answers

What does the command 'echo -e "hi\nhow r u"' do?

<p>Prints 'hi' on one line and 'how r u' on another (A)</p> Signup and view all the answers

How do you display only the last 5 lines of a file named 'filename'?

<p>tail -5 filename (D)</p> Signup and view all the answers

What will the command 'head -99 file | tail -1' display?

<p>The 99th line of 'file' (D)</p> Signup and view all the answers

What command would you use to find the count of words in the 99th line of a file?

<p>head -99 log | tail -1 | wc -w (A)</p> Signup and view all the answers

What is the purpose of the command 'grep -i "Linux" test_1'?

<p>To search for any occurrence of 'Linux' regardless of case (D)</p> Signup and view all the answers

Which command is used to change the ownership of a file or directory?

<p>chown (D)</p> Signup and view all the answers

What does the symbol '|' (pipe) do in command line operations?

<p>Gives output of one command as input to the next (B)</p> Signup and view all the answers

Flashcards

ls command

Lists files and directories.

ls -l

Lists files and directories with detailed information (permissions, size, etc.).

touch command

Creates an empty file.

mkdir command

Creates a new directory.

Signup and view all the flashcards

cd command

Changes the current working directory.

Signup and view all the flashcards

cd ..

Moves up one level in the directory tree.

Signup and view all the flashcards

mkdir -p

Creates a directory and any necessary parent directories.

Signup and view all the flashcards

cp command

Copies files or directories.

Signup and view all the flashcards

cp -r

Copies directories recursively (including subdirectories).

Signup and view all the flashcards

mv command

Moves or renames files and directories.

Signup and view all the flashcards

chmod command

Changes file permissions.

Signup and view all the flashcards

chown command

Changes file ownership.

Signup and view all the flashcards

df -h

Displays disk space usage.

Signup and view all the flashcards

du -sh

Displays file/directory size in human-readable format.

Signup and view all the flashcards

echo command

Displays a string to the terminal.

Signup and view all the flashcards

vi editor

Used for text editing.

Signup and view all the flashcards

:wq!

Saves and quits vi.

Signup and view all the flashcards

:q!

Quits vi without saving.

Signup and view all the flashcards

grep command

Searches for patterns in files.

Signup and view all the flashcards

sed command

Used for stream editing (replace or delete strings).

Signup and view all the flashcards

cut command

Extracts columns from a file.

Signup and view all the flashcards

awk command

Processes data in a file (column-wise).

Signup and view all the flashcards

find command

Locates files based on criteria.

Signup and view all the flashcards

xargs command

Takes output and runs a command on it.

Signup and view all the flashcards

uniq command

Prints unique lines from input.

Signup and view all the flashcards

more / less

Displays file content page-by-page.

Signup and view all the flashcards

ps command

Displays a list of running processes.

Signup and view all the flashcards

kill command

Terminates a process.

Signup and view all the flashcards

sleep command

Pauses execution for a specified amount of time.

Signup and view all the flashcards

Study Notes

### File and Folder Management

  • ls command is used to list files and directories. To get a more detailed information, use ls -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 by i.
  • 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 of ls -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 string this 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" filenamesearches 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 the find 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.

Quiz Team

Related Documents

Linux Commands PDF

More Like This

File Management and Types Quiz
10 questions
File Management Tools Quiz
10 questions

File Management Tools Quiz

CalmRetinalite8196 avatar
CalmRetinalite8196
File Management Systems Quiz
18 questions
Use Quizgecko on...
Browser
Browser