Linux Commands Flashcards

1
Q

pwd

A

print working directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

cd

A

change directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ls

A

List directory contents

-l - Long format listing, providing detailed information including file permissions, ownership, size, and modification date.
-a - Lists all files, including hidden files (those starting with .).
-h - Human-readable format for file sizes (e.g., 1K, 5M) when used with -l.
-R - Recursively lists all files and directories within subdirectories.
-t - Sorts files by modification time, with the newest first.
-r - Reverses the order of the sorting (e.g., oldest first when combined with -t).
-d - Lists directories themselves, not their contents (useful to avoid recursing into directories).
-S - Sorts files by size, with the largest first.

ls -la lists all files (including hidden ones) in long format.
ls -lh lists files in long format with human-readable sizes.
ls -lR lists all files and directories recursively in long format.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

cat

A

Returns the contents of the files listed as arguments.

-n - Adds line numbers to the output.

Often, cat output is piped to a pager (cat | more or cat | less) to control scrolling. You can also redirect the output to another file. In Linux, there are overwrite and append redirection operators:

Overwrite any data at the destination file:
cat > file

Append the cat data to the destination file:
cat >> file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

find

A

The find command in Linux is used to search for files and directories within a directory hierarchy based on a variety of criteria. It’s very powerful and supports many options for searching.

Here are some of the most important and commonly used switches:

-name <pattern> - Finds files and directories matching a specific name or pattern.

Example: find . -name "*.txt" finds all .txt files in the current directory and its subdirectories.

-type <type> - Searches for specific types of items:

  • f for files
  • d for directories

Example: find /path -type d finds all directories in the specified path.

-size <size> - Finds files based on size.

Use + or - for greater or smaller than, and units like c (bytes), k (kilobytes), M (megabytes).

Example: find . -size +10M finds all files larger than 10 MB in the current directory.

-mtime <days> - Searches for files based on modification time.

Use + for older than the specified number of days and - for newer.

Example: find /var/log -mtime -7 finds files modified in the last 7 days.

-exec <command> {} \; - Executes a command on each file found.

Example: find . -name "*.log" -exec rm {} \; finds and deletes all .log files in the current directory.

-iname <pattern> - Similar to -name but case-insensitive.

Example: find . -iname "*.PDF" finds all files with .pdf extension regardless of case.

-user <username> - Finds files owned by a specific user.

Example: find /home -user john finds all files owned by the user “john.”

-perm <permissions> - Finds files with specific permissions.

Example: find . -perm 644 finds files with permissions 644.
-maxdepth <level> - Limits the search to a certain depth in the directory hierarchy.</level>

Example: find . -maxdepth 2 -name "*.sh" searches for .sh files only within two levels of directories.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

grep

A

The grep (Globally search a Regular Expression and Print) command is used to search and filter the contents of files. Its output prints (displays) the lines that contain a match for the search string. The search string can be a simple text value to match (a literal) or can use a pattern-matching language called regular expressions (regex).

grep is especially useful for searching long files such as system logs. For example, the following command displays only the lines in the Linux system log file for messages that contain the text uid=1003, ignoring the case of the text with the -i switch:

grep -i "uid=1003" /var/log/messages

The grep command can also be used as a file name search tool by piping a directory list as input. For example, ls -l | grep audit command returns a long listing of any files in the current directory whose name contains audit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

cp

A

Copy files and directories

-r or --recursive - Recursively copy directories and their contents.

-i or --interactive - Prompt before overwriting existing files.

-u or --update - Copy only when the source file is newer than the destination file or when the destination file is missing.

-v or --verbose - Show the files as they are being copied.

-a or --archive - Copy directories and their contents, preserving attributes such as timestamps, symlinks, and file permissions (essentially equivalent to -dR –preserve=all).

-f or --force - Overwrite existing files without prompting.

-p or --preserve - Preserve file attributes like mode, ownership, and timestamps.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

mv

A

Move files and directories

-i or --interactive - Prompt before overwriting existing files.

-u or --update - Move only when the source file is newer than the destination file or when the destination file is missing.

-v or --verbose - Show the files as they are being moved.

-f or --force - Overwrite existing files without prompting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

rm

A

Delete files and directories

-r or -R or --recursive - Recursively remove directories and their contents.

-i or --interactive - Prompt before every removal.

-f or --force - Force removal of files without prompting, ignoring any non-existent files.

-v or --verbose - Display the files being removed.

-d or --dir - Remove empty directories.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

df

A

(“disk free”) enables you to view the device’s free space, file system, total size, space used, percentage value of space used, and mount point.

-h - use human-friendly units

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

du

A

(“disk usage”) displays how a device is used, including the size of directory trees and files within it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

chown

A

The command chown allows the superuser to change the owner of a file or directory. Note that this right is reserved to superuser or sudoer. Even if a regular user owns a file, they cannot use chown. The file owner can change the group using the chgrp command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

ps

A

The ps command invokes the process table, a record that summarizes the current running processes on a system.

-e or -A - Show information for all processes.

-f or --full - Display a full-format listing (more detailed information).

-u <user> - Display processes for a specific user.

-aux - A common combination to display all processes from all users in a user-friendly format (includes details such as CPU and memory usage).

-l - Display a long format, showing more details like the process state and priority.

-p <pid> - Display information about a specific process ID (PID).

-T or --threads - Show all threads associated with processes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

top

A

Like ps, the top command lists all processes running on a Linux system. It acts as a process management tool by enabling you to prioritize, sort, or terminate processes interactively. It displays a dynamic process status, reflecting real-time changes.

Different keystrokes within this tool execute various process management actions. Some of the frequently used command keys include the following.

ENTER - Refresh the status of all processes.

SHIFT+N - Sort processes in the decreasing order of their PID.

M - Sort processes by memory usage.

P - Sort processes by CPU usage.

u - Display processes belonging to the user specified at the prompt.

q - Exit the process list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

chmod

A

The chmod command can be used to secure files and directories, using either symbolic or octal notation. Only the owner can change permissions.

drwxr-xr-x 2 bobby admins Desktop

-rwx-r-x r-- 1 bobby admins scan.sh

The leading character designates the file type. For example,- represents a regular file and d indicates a directory. The permissions for the Desktop directory show that the owner (bobby) has full (rwx) permissions, whereas the group (admins) and others have read and execute but not write (r-x). For the scan.sh file, the user has read/write/execute (rwx) permission, the group has read and execute permission (r-x), and world has read permission only (r--).

Permissions can also be expressed numerically, using the octal value format. An octal value can represent up to eight digits (0–7). 0 represents deny (no permissions), read=4, write=2, and execute=1. You can add those values together to get a particular combination of permissions.

For example, a file with numeric permission 0754 can be converted to symbolic notation as follows:

The leading zero identifies the value as an octal but can often be omitted.
7 in the first position grants all rights to the owner: 4(r)+2(w)+1(x).
5 in the second position grants read and execute to the group: 4(r)+0+1(x).
4 in the third position grants read to world: 4(r)+0+0.
The other common combination is 6 (read and write).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

ip

A

Replaces legacy ifconfig

ip addr - replicates the basic reporting functionality of ifconfig (show the current address configuration).

ip addr show dev eth0 - reports a single interface only.

ip link - shows the status of interfaces

ip -s link - reports interface statistics

ip link set eth0 up|down - enable or disable an interface

ip addr add|delete - modify the IP address configuration. These changes are not persistent and apply only to the running configuration, unless run as part of a startup script.

16
Q

dig

A

dig is powerful tool for gathering information and testing name resolution. It is installed on most Linux distributions. Output is displayed in an answer section. Output will include the IP address mapped to the domain name, the DNS server that answered the query, and how long it took to receive that answer.

The basic syntax is: dig domainame

The command dig @server domainname will resolve the domain name against the DNS server specified by the server argument.

17
Q

cron

A

If you want to run a batch of commands or a script to perform a backup or other maintenance task, there is a scheduling service called cron. Every user of the system is allowed to schedule programs or tasks in their own personal crontab (cron table). These tables are merged by cron to create an overall system schedule. Every minute, the cron service checks the schedule and executes the programs for that period.

To add or delete a scheduled job, use the crontab editor. To review a user’s crontab jobs, enter the command:
crontab –l

To remove jobs from the scheduled list, use the command:
crontab -r

To enter the editor, run the command crontab –e. crontab uses the vi editor by default.
The basic syntax for scheduling a job using crontab includes the following:

mm—specifies the minutes past the hour when the task is to initiate (0–59).

hh—specifies the hour (0–23).

dd—can be used to specify the date within the month (0–31).

MM—specifies the month in either numerical or text format (1–12 or jan, feb, mar).

weekday—sets the day of the week (1–7 or mon, tue, wed).

command—the command or script to run. This should include the full path to the file.

It is important to note that any of the time/date related parameters can be replaced by wildcards:

* specifies any or other characters.

, allows multiple values.

- allows a range of values.

/2 indicates every other.

For example, consider the following crontab entry:

§15 02 * * 5 /usr/bin/rsync –av --delete /home/sam/mount/rsync

This would cause the system to run the rsync backup program at 2:15 a.m. on a Friday (day 5), synchronizing the /home/sam directory with the /mount/sync folder (which could be a mount point to an external backup device).

18
Q

dd

A

The dd command is primarily used to create an exact, bit-by-bit image of any form of block storage, meaning mass storage devices such as hard drive volumes, thumb drives, and optical media. In its most simple form, the dd command is just:

$ dd if=<source block device> of=<destination image file location>

Copying a Hard Drive

Let’s say you have a hard drive (sda) you want to copy onto another hard drive (sdb). In this case, we will say they are exactly the same size. The following command will copy the entire sda drive, partition table, file systems… everything to the sdb drive:

dd if=/dev/sda of=/dev/sdb

Backing Up a Thumb Drive

Let’s say you have a thumb drive full of important files you really want to back up. Using dd as follows, you can copy the entire USB drive and make an image file (I chose to call it thumbBackup.bak) and place that image file on your Desktop:

dd if=/dev/sdc of=/home/mike/Desktop/thumbBackup.bak

Wiping a Disk

I have a drive (sdb) that I want to totally wipe. The dd command can take input from anywhere, but in this case, I’ll use Linux’s random number generator, /dev/urandom, to write a stream of random bits completely over the entire drive. It’s not a perfect wipe, but it will stop all but the most sophisticated tools.

dd if=/dev/urandom of=/dev/sdb