Operating System File 4
Operating System File 4
Operating System File 4
Program 4: Implement the basic and user status commands like: su, sudo,
man, help, history, who, whoami, id, uname, uptime, free, tty, cal, date,
hostname, reboot,clear.
Introduction
Linux is famous for its powerful commands. To use Linux effectively, all users
should know how to use terminal commands. Although the OS has a GUI, many
functionalities work faster when run as commands through the terminal.
Prerequisites
All Linux commands fall into one of the following four categories:
• Shell built ins - Commands built directly into the shell with the
fastestexecution.
• Shell functions - Shell scripts (grouped commands).
• Aliases - Custom command shortcuts.
• Executable programs - Compiled and installed programs or scripts.
1. pwd command
The pwd command (print working directory) is a shell builtin command that prints
the current location. The output shows an absolute directory path, starting with
the root directory (/).
pwd <options>
To see how the command works, run the following in the terminal:
pwd
The output prints the current location in the /home/<username> format.
2. ls command
The ls command (list) prints a list of the current directory's contents. Run the
following:
ls
Additional options provide flexibility with the display output. Typical usage
includes combining the following options:
• Show as a list:
ls -l
ls -la
ls -lah
3. cd command
The cd command (change directory) is a shell builtin command for changing the
current working directory:
cd <directory>
cd Documents
4. cat command
The cat command (concatenate) displays the contents of a file in the terminal
(standard output or stdout). To use the command, provide a file name from the
current directory:
cat <filename>
cat ><filename>
Alternatively, provide a path to the file along with the file name:
cat <path>/<filename>
The command can also:
cat ><filename>
cat -n <filename>
5. touch command
cat <path>/<filename>
The command creates an empty file if it does not exist. Due to this
effect, touchis also a quick way to make a new file (or a batch of files).
6. cp command
The main way to copy files and directories in Linux is through the cp (copy)
command. Try the command with:
7. mv command
Use the mv (move) command to move files or directories from one location to
another. For example, to move a file from the current directory to ~/Documents,
run:
8. mkdir command
The mkdir (make directory) command creates a new directory in the provided
location. Use the command in the following format:
cat -n <filename>
9. rmdir command
Use the rmdir (remove directory) command to delete an empty directory. For
example:
touch <filename>
If the directory is not empty, the command fails.
10. rm command
The rm command (remove) deletes files or directories. To use the command for
non-empty directories, add the -rtag:
Unlike the rmdir command, rm also removes all the contents from the
directory.
11. locate command
The locate command is a simple Linux tool for finding a file. The command
checks a file database on a system to perform the search quickly. However, the
result is sometimes inaccurate if the database is not updated.
To use the command, install locate and try the following example:
mv <filename> ~/Documents/<filename>
The output prints the file's location path. The matching is unclear and outputs
any file that contains the file name.
12. find command
Use the find command to perform a thorough search on the system. Add the -
nametag to search for a file or directory by name:
The grep (global regular expression print) enables searching through text in a
file or a standard output. The basic syntax is:
The output highlights all matches. Advanced commands include using grep for
multiple strings or writing grep regex statements.
rm -r <file or directory>
Use the command with caution to avoid making accidental changes permanent.
15. df command
The df (disk free) command shows the statistics about the available disk space on
the filesystem. To see how df works, run the following:
locate <filename>
The output shows the amount of space used by different drives. Add the -htag to
make the output in human-readable format (kilobytes, megabytes, and
gigabytes).
16. du command
The du (disk usage) command helps show how much space a file or directory
takes up. Run the command without any parameters:
The output shows the amount of space used by files and directories in the
current directory. The size displays in blocks, and adding the -h tag changes the
measure to human-readable format.
Use the head command to truncate long outputs. The command can truncate files,
for example:
sudo <command>
For example, to see the first ten lines of the du command, run:
df
The Linux tail command does the opposite of head. Use the command to show
the last ten lines of a file:
du
head <filename>
For example, use tail to see the last ten lines of the du command:
<command> | head
Both headand tail commands are helpful when reading Linux log files.
19. diff command
The diff (difference) command compares two files and prints the difference. To
use the command, run:
du | head
tail <filename>