Basic Commands¶
File & Directory Management¶
| Command | Explanation |
|---|---|
ls | List directory contents |
cd | Change directory |
pwd | Print working directory |
mkdir | Make directory |
touch | Create empty files or update timestamps |
cp | Copy files and directories |
mv | Move or rename files |
rm | Remove files or directories |
01. ls - List directory contents¶
The ls command lists the contents of a directory. By default, it lists the files in the current directory sorted alphabetically.
Common Flags¶
| Command Flag | Description |
|---|---|
-l | Long listing format. Shows file details: permissions, owner, size, modification time. |
-a | All files. Shows hidden files (those starting with .), such as .bashrc or .git. |
-h | Human-readable. When used with -l, prints sizes in human-readable format (e.g., 1K, 234M, 2G) instead of bytes. |
-R | Recursive. Lists files in the current directory and all subdirectories recursively. |
-t | Sort by time. Shows the most recently modified files first. |
-r | Reverse. Reverses the sort order. |
Examples¶
1. List all files in long format with human-readable sizes
Output Explanation:
drwxr-xr-x 5 user staff 160B Dec 11 10:00 .
drwxr-xr-x 10 user staff 320B Dec 10 14:30 ..
-rw-r--r-- 1 user staff 220B Dec 11 09:00 .bashrc
-rw-r--r-- 1 user staff 1.2K Dec 11 10:00 file.txt
drwxr-xr-x: Directory permissions. - user staff: Owner and Group. - 1.2K: File size. - .bashrc: Hidden file shown due to -a. 2. List files sorted by modification time (newest first)
3. List files in reverse alphabetical order
Questions¶
- What does the
lscommand do by default? - Which flag would you use to see hidden files (starting with
.)? - How can you list files in long format with human-readable sizes?
02. cd - Change directory¶
The cd command changes the current working directory of the shell.
Common Usage¶
| Path | Description |
|---|---|
cd /path/to/dir | Absolute path. Changes to the directory specified from the root. |
cd dir | Relative path. Changes to a subdirectory inside the current directory. |
cd .. | Parent directory. Moves up one level in the directory tree. |
cd ~ or cd | Home directory. Moves to the current user’s home directory. |
cd - | Previous directory. Switches back to the last directory you were in. |
Examples¶
1. Navigate to a system directory
2. Move up two levels
3. Toggle between two directories
Output: Prints the path of the directory you returned to.4. Change to the home directory
Questions¶
- What command changes the current directory?
- How do you go to the parent directory?
- What does
cd -do?
03. pwd - Print working directory¶
The pwd command prints the full absolute path of the current working directory.
Common Flags¶
| Command Flag | Description |
|---|---|
-L | Logical. Displays the logical current working directory, including symbolic links (default). |
-P | Physical. Displays the physical current working directory, resolving all symbolic links. |
Examples¶
1. Check where you are
Output:
2. Show the physical path (resolving symlinks)
Questions¶
- What does
pwdstand for? - What is the difference between
pwd -Landpwd -P?
04. mkdir - Make directory¶
The mkdir command creates new directories.
Common Flags¶
| Command Flag | Description |
|---|---|
-p | Parents. No error if existing, make parent directories as needed. Useful for creating nested structures. |
-v | Verbose. Print a message for each created directory. |
-m | Mode. Set file mode (permissions) for the new directory (e.g., -m 755). |
Examples¶
1. Create a directory structure
Explanation: Createsproject folder and src, bin, logs inside it. 2. Create a directory with specific permissions
Explanation: Creates a folder that only the owner can read, write, and execute.3. Create multiple directories at once
Questions¶
- What does
mkdirdo? - Which flag allows creating parent directories if they don’t exist?
- How can you set permissions when creating a directory?
05. touch - Create empty files or update timestamps¶
The touch command is used to create an empty file if it doesn’t exist. If the file exists, it updates the access and modification timestamps to the current time.
Common Flags¶
| Command Flag | Description |
|---|---|
-a | Access time. Change only the access time. |
-m | Modification time. Change only the modification time. |
-c | No create. Do not create any files if they don’t exist. |
-t | Time. Use specified time instead of current time (format: [[CC]YY]MMDDhhmm[.ss]). |
Examples¶
1. Create a new file
2. Update timestamp of an existing file
Explanation: Useful to trigger build systems that rely on file modification times.3. Create multiple files at once
Questions¶
- What does
touchdo if the file doesn’t exist? - What happens if you run
touchon an existing file? - Which flag changes only the access time?
System & Process Management¶
| Command | Explanation |
|---|---|
ps | Report a snapshot of the current processes |
kill | Terminate a process |
top | Display Linux processes |
df | Report file system disk space usage |
du | Estimate file space usage |
06. cp - Copy files and directories¶
The cp command copies files and directories from a source to a destination.
Common Flags¶
| Command Flag | Description |
|---|---|
-r or -R | Recursive. Copy directories and their contents recursively. |
-i | Interactive. Prompt before overwriting an existing file. |
-v | Verbose. Explain what is being done. |
-u | Update. Copy only when the SOURCE file is newer than the destination file or when the destination file is missing. |
-p | Preserve. Preserve mode, ownership, and timestamps. |
Examples¶
1. Copy a file to another directory
2. Copy a directory recursively
3. Safe copy (prompt before overwrite)
Output:cp: overwrite '/data/important.txt'? 4. Copy and preserve attributes
Questions¶
- What is the purpose of the
cpcommand? - Which flag do you use to copy directories recursively?
- What does the
-iflag do?
07. mv - Move or rename files¶
The mv command moves files or directories from one place to another. It is also used to rename files.
Common Flags¶
| Command Flag | Description |
|---|---|
-i | Interactive. Prompt before overwriting. |
-u | Update. Move only when the SOURCE file is newer than the destination file. |
-v | Verbose. Explain what is being done. |
-n | No clobber. Do not overwrite an existing file. |
Examples¶
1. Rename a file
2. Move files to a directory
3. Move a directory
Questions¶
- How does
mvdiffer fromcp? - What happens if you use
mvto move a file to a different name in the same directory? - Which flag prevents overwriting existing files?
08. rm - Remove files or directories¶
The rm command removes (deletes) files and directories. Warning: Deleted files are usually not recoverable.
Common Flags¶
| Command Flag | Description |
|---|---|
-r or -R | Recursive. Remove directories and their contents recursively. |
-f | Force. Ignore nonexistent files and arguments, never prompt. |
-i | Interactive. Prompt before every removal. |
-v | Verbose. Explain what is being done. |
Examples¶
1. Remove a single file
2. Remove a directory and all its contents (Use with caution)
3. Interactive removal
Output:rm: remove regular file 'file1.txt'? 4. Remove files forcefully without prompts
Questions¶
- Why is the
rmcommand dangerous? - Which flag removes directories recursively?
- What does
-fdo?
Text Processing¶
| Command | Explanation |
|---|---|
cat | Concatenate and display files |
head | Output the first part of files |
tail | Output the last part of files |
grep | Print lines matching a pattern |
wc | Print newline, word, and byte counts |
sort | Sort lines of text files |
uniq | Report or omit repeated lines |
less | Opposite of more |
09. cat - Concatenate and display files¶
The cat command reads data from the file and outputs its content. It is often used to combine files.
Common Flags¶
| Command Flag | Description |
|---|---|
-n | Number. Number all output lines. |
-b | Number non-blank. Number nonempty output lines, overrides -n. |
-s | Squeeze blank. Suppress repeated empty output lines. |
-E | Show ends. Display $ at the end of each line. |
Examples¶
1. Display file content
2. Concatenate multiple files
3. Create a file from terminal input (Press Ctrl+D to save)
4. Number all lines in a file
Questions¶
- What does
catstand for? - How can you use
catto create a new file? - Which flag numbers the output lines?
10. head - Output the first part of files¶
The head command outputs the first part of files. By default, it shows the first 10 lines.
Common Flags¶
| Command Flag | Description |
|---|---|
-n NUM | Lines. Print the first NUM lines instead of the first 10. |
-c NUM | Bytes. Print the first NUM bytes of each file. |
-q | Quiet. Never print headers giving file names. |
Examples¶
1. Show the first 5 lines of a file
2. View the top of a script to check the shebang
Output:#!/bin/bash 3. Show the first 10 bytes of a file
Questions¶
- What is the default number of lines
headdisplays? - How do you display the first 20 lines of a file?
- What does
-cdo?
11. tail - Output the last part of files¶
The tail command outputs the last part of files. By default, it shows the last 10 lines. It is extremely useful for monitoring logs.
Common Flags¶
| Command Flag | Description |
|---|---|
-n NUM | Lines. Output the last NUM lines, instead of the last 10. |
-f | Follow. Output appended data as the file grows. Useful for watching log files in real-time. |
-c NUM | Bytes. Output the last NUM bytes. |
Examples¶
1. Show the last 20 lines of a log file
2. Monitor a log file in real-time
Explanation: The command will stay running and print new lines as they are written to the file. PressCtrl+C to stop. 3. Show the last 50 bytes of a file
Questions¶
- What is
tail -fused for? - How do you display the last 50 lines?
- What is the difference between
headandtail?
12. grep - Print lines matching a pattern¶
The grep command searches for patterns (strings or regular expressions) in files and prints the matching lines.
Common Flags¶
| Command Flag | Description |
|---|---|
-i | Ignore case. Ignore case distinctions in patterns and data. |
-r or -R | Recursive. Read all files under each directory, recursively. |
-v | Invert match. Select non-matching lines. |
-n | Line number. Prefix each line of output with the 1-based line number. |
-l | Files with matches. Suppress normal output; instead print the name of each input file from which output would normally have been printed. |
-c | Count. Suppress normal output; instead print a count of matching lines. |
Examples¶
1. Search for a string in a file
2. Search recursively for a string in the current directory
3. Count occurrences of a word
4. Search case-insensitively
Questions¶
- What does
grepdo? - Which flag searches recursively?
- How do you invert the match?
13. wc - Print newline, word, and byte counts¶
The wc command prints newline, word, and byte counts for each file.
Common Flags¶
| Command Flag | Description |
|---|---|
-l | Lines. Print the newline counts. |
-w | Words. Print the word counts. |
-c | Bytes. Print the byte counts. |
Examples¶
1. Count lines in a file
2. Count words
3. Count everything
Questions¶
- What does
wcstand for? - How do you count only lines?
- What are the three default counts
wcdisplays?
14. sort - Sort lines of text files¶
The sort command sorts lines of text files.
Common Flags¶
| Command Flag | Description |
|---|---|
-r | Reverse. Reverse the result of comparisons. |
-n | Numeric. Compare according to string numerical value. |
-u | Unique. Output only the first of an equal run. |
Examples¶
1. Sort a file alphabetically
2. Sort numbers
3. Sort in reverse order
Questions¶
- How do you sort numerically?
- How do you reverse the sort order?
- Which flag removes duplicates?
15. uniq - Report or omit repeated lines¶
The uniq command reports or omits repeated lines. Note: uniq does not detect repeated lines unless they are adjacent.
Common Flags¶
| Command Flag | Description |
|---|---|
-c | Count. Prefix lines by the number of occurrences. |
-d | Repeated. Only print duplicate lines, one for each group. |
-u | Unique. Only print unique lines. |
Examples¶
1. Remove adjacent duplicates
2. Count occurrences
3. Show only duplicates
Questions¶
- Why do you often use
sortbeforeuniq? - How do you count occurrences of lines?
- Which flag shows only duplicate lines?
16. less - Opposite of more¶
The less command is a program similar to more, but it allows backward movement in the file as well as forward movement.
Common Flags¶
| Command Flag | Description |
|---|---|
-N | Line numbers. Show line numbers. |
-S | Chop long lines. Causes lines longer than the screen width to be chopped rather than wrapped. |
+F | Follow. Scroll forward, and keep trying to read when the end of file is reached (like tail -f). |
Examples¶
1. View a file
2. View with line numbers
3. Search inside less
Questions¶
- How do you quit
less? - How do you search forward in
less? - What is the advantage of
lessovercat?
User & Permissions¶
| Command | Explanation |
|---|---|
chmod | Change file mode bits |
chown | Change file owner and group |
sudo | Execute a command as another user |
whoami | Print effective userid |
17. chmod - Change file mode bits¶
The chmod command changes the file mode bits (permissions) of a file or directory. Permissions define who can read, write, or execute a file.
Common Flags¶
| Command Flag | Description |
|---|---|
-R | Recursive. Change files and directories recursively. |
-v | Verbose. Output a diagnostic for every file processed. |
-c | Changes. Like verbose but report only when a change is made. |
Permission Modes¶
Symbolic Modes
| Category | Symbol | Description |
|---|---|---|
| Who | u | User |
g | Group | |
o | Others | |
a | All | |
| Operator | + | Add |
- | Remove | |
= | Set | |
| Permission | r | Read |
w | Write | |
x | Execute |
Octal Modes
| Value | Permission |
|---|---|
4 | Read |
2 | Write |
1 | Execute |
Examples¶
1. Make a script executable for the owner
2. Set permissions to 755 (rwxr-xr-x)
Explanation: Owner has full access (7), Group and Others can read and execute (5).3. Remove write permission for others
4. Add execute permission for group
Questions¶
- What does
chmoddo? - Explain the octal permission 755.
- How do you make a file executable?
18. chown - Change file owner and group¶
The chown command changes the user and/or group ownership of a given file.
Common Flags¶
| Command Flag | Description |
|---|---|
-R | Recursive. Operate on files and directories recursively. |
-v | Verbose. Output a diagnostic for every file processed. |
-c | Changes. Like verbose but report only when a change is made. |
Examples¶
1. Change owner of a file
2. Change owner and group
3. Change ownership recursively
4. Change only the group
Questions¶
- What is the difference between
chownandchmod? - Which flag changes ownership recursively?
- How do you change both owner and group?
19. sudo - Execute a command as another user¶
The sudo command allows a permitted user to execute a command as the superuser or another user.
Common Flags¶
| Command Flag | Description |
|---|---|
-u user | User. Run command as specified user. |
-i | Login. Run the shell specified by the target user’s password database entry as a login shell. |
-l | List. List user’s privileges or check a specific command. |
Examples¶
1. Run command as root
2. Switch to root user
3. Run command as another user
Questions¶
- What does
sudostand for? - How do you switch to the root user environment?
- How do you run a command as a specific user?
20. whoami - Print effective userid¶
The whoami command prints the user name associated with the current effective user ID.
Common Flags¶
| Command Flag | Description |
|---|---|
--help | Help. Display this help and exit. |
--version | Version. Output version information and exit. |
Examples¶
1. Check current user
Questions¶
- What does
whoamiprint? - Is
whoamithe same asid -un? - When is
whoamiuseful?
System Information & Management¶
| Command | Explanation |
|---|---|
man | An interface to the system reference manuals |
ps | Report a snapshot of the current processes |
kill | Terminate a process |
top | Display Linux processes |
df | Report file system disk space usage |
du | Estimate file space usage |
21. man - An interface to the system reference manuals¶
The man command displays the user manual of any command that we can run on the terminal.
Common Flags¶
| Command Flag | Description |
|---|---|
-k | Apropos. Search the short descriptions and manual page names for the keyword. |
-f | Whatis. Display short description from the manual page. |
-w | Where. Print the location of the manual page file. |
Examples¶
1. Read the manual for ls
q to quit. 2. Search for commands related to “copy”
3. Get a short description of a command
Questions¶
- What does
mando? - How do you search for manual pages?
- What key do you press to quit the man page viewer?
22. ps - Report a snapshot of the current processes¶
The ps command displays information about a selection of the active processes.
Common Flags¶
| Command Flag | Description |
|---|---|
-e | Every. Select all processes. |
-f | Full. Do full-format listing. |
-u user | User. Select by effective user ID (EUID) or name. |
aux | BSD Style. a (all users), u (user oriented), x (no terminal). |
Examples¶
1. View all running processes
2. View processes for a specific user
3. View processes with CPU/Memory usage (BSD style)
Questions¶
- What does
psstand for? - Which flag selects all processes?
- How do you view processes for a specific user?
23. kill - Terminate a process¶
The kill command sends a signal to a process. The default signal is TERM (terminate).
Common Flags¶
| Command Flag | Description |
|---|---|
-9 | SIGKILL. Force kill the process immediately. |
-l | List. List all available signal names. |
-s SIG | Signal. Send a specific signal. |
Examples¶
1. Terminate a process by PID
2. Force kill a process
3. List available signals
Questions¶
- What is the default signal sent by
kill? - Which signal forces a process to stop immediately?
- How do you list available signals?
24. top - Display Linux processes¶
The top command provides a dynamic real-time view of a running system.
Common Flags¶
| Command Flag | Description |
|---|---|
-d SECS | Delay. Specifies the delay between screen updates. |
-u user | User. Monitor only processes for a given user. |
-p PID | PID. Monitor only processes with specified process IDs. |
Examples¶
1. Start top
2. Monitor a specific user
3. Refresh every 5 seconds
Questions¶
- What does
topdisplay? - How do you filter processes by user in
top? - How do you change the update interval?
25. df - Report file system disk space usage¶
The df command displays the amount of disk space available on the file system containing each file name argument.
Common Flags¶
| Command Flag | Description |
|---|---|
-h | Human-readable. Print sizes in powers of 1024 (e.g., 1023M). |
-T | Type. Print file system type. |
-i | Inodes. List inode information instead of block usage. |
Examples¶
1. Check disk space usage
2. Human readable format
3. Show file system type
Questions¶
- What does
dfreport? - Which flag shows sizes in human-readable format?
- How do you see the file system type?
26. du - Estimate file space usage¶
The du command estimates file space usage.
Common Flags¶
| Command Flag | Description |
|---|---|
-h | Human-readable. Print sizes in human readable format (e.g., 1K 234M 2G). |
-s | Summarize. Display only a total for each argument. |
-c | Total. Produce a grand total. |
Examples¶
1. Check size of current directory
2. Summary of a directory
3. Check size of specific files
Questions¶
- What is the difference between
dfanddu? - How do you get a summary of a directory’s size?
- Which flag gives a grand total?
Advanced Search¶
| Command | Explanation |
|---|---|
find | Search for files in a directory hierarchy |
27. find - Search for files in a directory hierarchy¶
The find command searches for files in a directory hierarchy.
Common Flags¶
| Command Flag | Description |
|---|---|
-name pattern | Name. Base of file name (the path with the leading directories removed) matches shell pattern pattern. |
-type c | Type. File is of type c: f (regular file), d (directory). |
-exec command ; | Execute. Execute command; true if 0 status is returned. |
-mtime n | Modification time. File’s data was last modified n*24 hours ago. |
Examples¶
1. Find files by name
2. Find directories only
3. Find and delete files (Be careful)
Questions¶
- How do you find files by name?
- How do you find only directories?
- What does
-mtimedo?
Networking¶
| Command | Explanation |
|---|---|
ping | Send ICMP ECHO_REQUEST to network hosts |
curl | Transfer a URL |
28. ping - Send ICMP ECHO_REQUEST to network hosts¶
The ping command uses the ICMP protocol’s mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway.
Common Flags¶
| Command Flag | Description |
|---|---|
-c count | Count. Stop after sending count ECHO_REQUEST packets. |
-i interval | Interval. Wait interval seconds between sending each packet. |
-t ttl | TTL. Set the IP Time to Live. |
Examples¶
1. Ping a host
2. Ping 5 times
3. Ping with specific interval
Questions¶
- What protocol does
pinguse? - How do you limit the number of pings?
- What does TTL stand for?
29. curl - Transfer a URL¶
The curl command is a tool to transfer data from or to a server.
Common Flags¶
| Command Flag | Description |
|---|---|
-O | Remote name. Write output to a local file named like the remote file we get. |
-I | Head. Fetch the HTTP-header only. |
-L | Location. Follow redirects. |
-d data | Data. Sends the specified data in a POST request. |
Examples¶
1. Download a file
2. Check headers
3. Follow redirects
Questions¶
- What does
curldo? - How do you save the output to a file with the same name?
- Which flag follows redirects?
Archives¶
| Command | Explanation |
|---|---|
tar | An archiving utility |
zip | Package and compress (archive) files |
unzip | List, test and extract compressed files in a ZIP archive |
30. tar - An archiving utility¶
The tar command is an archiving utility used to store and extract files from an archive file known as a tarball.
Common Flags¶
| Command Flag | Description |
|---|---|
-c | Create. Create a new archive. |
-x | Extract. Extract files from an archive. |
-v | Verbose. Verbosely list files processed. |
-f | File. Use archive file or device ARCHIVE. |
-z | Gzip. Filter the archive through gzip. |
Examples¶
1. Create a tar.gz archive
2. Extract a tar.gz archive
3. List contents of an archive
Questions¶
- What does
tarstand for? - Which flag creates a new archive?
- How do you compress the archive with gzip?
31. zip - Package and compress (archive) files¶
The zip command is a compression and file packaging utility.
Common Flags¶
| Command Flag | Description |
|---|---|
-r | Recursive. Travel the directory structure recursively. |
-e | Encrypt. Encrypt the contents of the zip archive. |
-d | Delete. Delete entries from the zip archive. |
Examples¶
1. Zip a directory
2. Zip multiple files
3. Password protect zip
Questions¶
- How do you zip a directory recursively?
- How do you password protect a zip file?
- What is the difference between
tarandzip?
32. unzip - List, test and extract compressed files in a ZIP archive¶
The unzip command will list, test, or extract files from a ZIP archive.
Common Flags¶
| Command Flag | Description |
|---|---|
-l | List. List archive files (short format). |
-t | Test. Test archive files. |
-d dir | Directory. An optional directory to which to extract files. |
Examples¶
1. Unzip a file
2. List contents without extracting
3. Extract to specific directory
Questions¶
- How do you list contents of a zip file?
- How do you extract to a specific directory?
- What does
unzip -tdo?