Linux Commands
Shell Prompts
| Shell | Prompt Symbol |
|---|---|
zsh |
% |
bash |
$ |
Basic Navigation & Directories
-
cd— takes you to the default home directory. -
mkdir— create a new directory / folder. -
pwd— show the current directory location. -
ls— list files/folders in the current directory. -
List all files/folders recursively and save to a file:
ls -R > list.txt
About less
less is a program similar to more, but it allows backward
movement in the file as well as forward movement. It does not have to read the
entire input file before starting, so with large input files it starts faster
than text editors like vi.
less uses termcap (or terminfo on some systems), so it can run on a
variety of terminals. There is even limited support for hardcopy terminals
(on a hardcopy terminal, lines that should be printed at the top of the screen
are prefixed with a caret).
Commands are based on both more and vi. Commands may be
preceded by a decimal number N. The number is used by some commands
as indicated in the manual.
Common Commands
| Command | Description / Example |
|---|---|
ls |
List files/folders in the current directory. |
less |
Read a file one screen at a time. |
mkdir |
Make a directory. |
rm |
Remove a file. |
rm -r |
Remove a directory and all files within it, e.g. rm -r directory-name. |
whoami |
Show the current user. |
cp |
Copy a file from one location to another,
e.g. cp file-name ~/Location/Folder.
|
Ditto |
Copy a file from one location to another using macOS ditto
(example text notes: hey file-name ~/Location/Folder).
|
mv |
Move (or rename) a file,
e.g. mv file-name ~/Location/Folder.
|
df -h |
Show disk space usage in human-readable form. |
| Ctrl + C | Cancel the current command and return to the prompt. |
nano |
Start editing a file directly in the terminal (text editor).
To save and exit: Ctrl + X, then Y, then Enter.
|
open |
Open files, folders, or applications (macOS). |
Networking & System Information
-
ping nameofwebsite.com— send network packets to a host to test connectivity.
Use Ctrl + C to stop. -
ifconfig— show network interface configuration. -
ifconfig en0— show config for a specific interface. -
Example for extracting IP addresses:
ifconfig en0 | grep inet | awk ' { print $2 } ' -
traceroute kunalamin.com— trace route to a host. -
dig kunalamin.com— query DNS to get “all DNS goodness”.
Processes & Resource Usage
-
ps— see processes on your computer. -
ps -ax— see all processes with more details. -
top— see which processes are using the most CPU. -
top -o rsize— sort by resident memory size (most memory usage). -
kill— stop a process.-
First, get the process ID (PID):
ps -ax | grep thisprocess -
Then kill it, e.g.:
kill -9 545433
-
First, get the process ID (PID):
Hidden Files in macOS
To hide a folder (it will still be there, just hidden):
chflags hidden ~/Documents/Adobe
To make it visible again:
chflags nohidden ~/Documents/Adobe
Shell Information
-
Check which shell you are using:
which $SHELL -
Type
bashto switch to Bash (commonly on Windows environments with WSL or similar). -
Type
zshto switch to Zsh (default on modern macOS).
File Comparison
-
diff test1 test2— compare two files and show the differences.
Case Conversion Scripts (Zsh)
Note: These examples use Zsh parameter expansion (:u) and glob qualifiers.
Adjust as needed for your environment.
1) Change Folder Names to Uppercase in Current Directory
Change directory names (only directories in current folder) to uppercase.
for dir in *(/); do
new_name=${dir:u} # Convert to uppercase
[[ "$dir" != "$new_name" ]] && mv -- "$dir" "$new_name"
done
2) Change File Names to Uppercase in Current Folder (Recursive)
Change file names (not directories) to uppercase, keeping the path.
for file in **/*(.); do
new_name=${file:h}/${file:t:u} # Convert filename to uppercase while keeping path
[[ "$file" != "$new_name" ]] && mv -- "$file" "$new_name"
done
3) Change File and Folder Names to Uppercase (Recursive)
Change both file and folder names to uppercase, recursively.
for item in **/*; do
new_name=${item:h}/${item:t:u} # Convert name to uppercase
[[ "$item" != "$new_name" ]] && mv -- "$item" "$new_name"
done
4) Change File and Folder Names to Title Case (Camel / Title Case)
Convert file and folder names to a title-style case (first letter of each word uppercase,
rest lowercase, splitting on - and _).
for item in **/*; do
dir=${item:h} # Get directory path
base=${item:t} # Get file/folder name
# Convert to Title Case (First letter of each word uppercase, rest lowercase)
new_name=$dir/${(C)${(j: :)${(s:_:)${(s:-:)${(L)base}}}}}
[[ "$item" != "$new_name" ]] && mv -- "$item" "$new_name"
done