Basic Linux Concepts and Commands
What is the difference between an absolute path and a relative path in Linux?
Answer:
An absolute path starts from the root directory (/) and specifies the full location of a file or directory. A relative path specifies the location relative to the current working directory. For example, /home/user/documents is absolute, while documents or ../data are relative.
Explain the purpose of the ls command and some common options.
Answer:
The ls command lists the contents of a directory. Common options include ls -l for a long listing format (permissions, owner, size, date), ls -a to show all files including hidden ones (starting with '.'), and ls -h for human-readable file sizes.
How do you create a new directory and remove an empty directory in Linux?
Answer:
To create a new directory, use mkdir directory_name. To remove an empty directory, use rmdir directory_name. If the directory is not empty, rmdir will fail, and you would typically use rm -r directory_name to remove it recursively.
What is the grep command used for?
Answer:
The grep command is used to search for patterns (text) within files. It stands for 'Global Regular Expression Print'. For example, grep 'error' /var/log/syslog would find all lines containing 'error' in the syslog file.
How can you view the contents of a text file without opening it in an editor?
Answer:
You can use cat filename to display the entire file content to standard output. For larger files, less filename allows you to view content page by page, and head filename or tail filename show the beginning or end of the file, respectively.
Answer:
Standard input (stdin, descriptor 0) is where a program receives its input, typically from the keyboard. Standard output (stdout, descriptor 1) is where a program sends its normal output, typically to the screen. Standard error (stderr, descriptor 2) is where a program sends error messages, also typically to the screen.
How do you redirect standard output to a file, and what is the difference between > and >>?
Answer:
You redirect standard output using >. For example, ls -l > file.txt sends the output of ls -l to file.txt, overwriting its content. >> appends the output to the file instead of overwriting it, e.g., echo 'new line' >> file.txt.
What is the purpose of the man command?
Answer:
The man command (short for manual) is used to display the manual pages for commands, utilities, and functions. It provides detailed information about a command's usage, options, and examples. For instance, man ls shows the manual page for the ls command.
How do you change file permissions in Linux?
Answer:
File permissions are changed using the chmod command. Permissions can be set numerically (e.g., chmod 755 file.sh for rwx r-x r-x) or symbolically (e.g., chmod u+x file.sh to add execute permission for the user). Permissions control read, write, and execute access for the owner, group, and others.
What is the sudo command used for?
Answer:
The sudo command (superuser do) allows a permitted user to execute a command as the superuser or another user, as specified by the security policy. It's used to perform administrative tasks that require elevated privileges without logging in as root directly. For example, sudo apt update.
How do you find your current working directory?
Answer:
You can find your current working directory using the pwd command, which stands for 'print working directory'. It will output the absolute path of the directory you are currently in.
What is a symbolic link (symlink) and how do you create one?
Answer:
A symbolic link, or symlink, is a special type of file that points to another file or directory. It's similar to a shortcut in Windows. You create one using the ln -s command, for example: ln -s /path/to/original /path/to/symlink.