Linux Commands Flashcards
pwd
print working directory
cd
change directory
ls
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.
cat
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
find
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.
grep
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
.
cp
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.
mv
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.
rm
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.
df
(“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
du
(“disk usage”) displays how a device is used, including the size of directory trees and files within it.
chown
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.
ps
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.
top
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.
chmod
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).