Catalog / Unix Command Line Cheatsheet

Unix Command Line Cheatsheet

A quick reference guide to essential Unix command-line shortcuts and commands for efficient navigation and system management.

Navigation & File Management

Basic Navigation

pwd

Print Working Directory: Displays the current directory path.

cd

Change Directory: Navigates to the specified directory.
cd .. - Move up one directory.
cd ~ - Go to home directory.
cd - - Go to previous directory.

ls

List Directory Contents: Shows files and directories in the current directory.
ls -l - Long listing format (permissions, size, date).
ls -a - Show all files, including hidden ones (starting with .).
ls -t - Sort by modification time (newest first).

mkdir

Make Directory: Creates a new directory.
mkdir directory_name

rmdir

Remove Directory: Deletes an empty directory.
rmdir directory_name

touch

Create an empty file or update the timestamp of an existing file.
touch file_name

File Operations

cp

Copy: Copies files or directories.
cp source destination
cp -r source_directory destination_directory (recursive copy for directories)

mv

Move/Rename: Moves files or directories, or renames them.
mv old_name new_name
mv file destination_directory

rm

Remove: Deletes files.
rm file_name
rm -r directory_name (recursive removal for directories)
rm -f file_name (force removal, bypass prompts)
rm -rf directory_name (force recursive removal)

ln

Create links between files.
ln -s source_file symbolic_link (creates a symbolic link)

cat

Concatenate and display file content.
cat file_name

less

View file content page by page.
less file_name
Use arrow keys to navigate, q to quit.

Searching & Redirection

Searching Files

find

Find files based on criteria.
find . -name "*.txt" (find all .txt files in the current directory and its subdirectories).
find / -name "important_file" (find ‘important_file’ in the entire system).
find . -size +10M (find files larger than 10MB in the current directory).

grep

Search for patterns within files.
grep 'pattern' file_name (search for ‘pattern’ in file_name).
grep -i 'pattern' file_name (case-insensitive search).
grep -r 'pattern' directory_name (recursive search in directory).
grep -v 'pattern' file_name (find lines that do NOT contain the pattern).

which

Locate the executable file associated with a command.
which command_name

locate

Find files by name using a pre-built index. Requires updatedb to update the index.
locate file_name

Input/Output Redirection

>

Redirect output to a file (overwrites existing content).
command > file.txt

>>

Append output to a file (adds to existing content).
command >> file.txt

2>

Redirect standard error to a file.
command 2> error.txt

&> or >&

Redirect both standard output and standard error to a file.
command &> output.txt or command >& output.txt

| (pipe)

Pipe: Sends the output of one command as input to another.
command1 | command2

<

Redirect input from a file to a command.
command < input.txt

System Information & Process Management

System Information

uname

Print system information.
uname -a (all information).
uname -r (kernel release).

df

Disk Free: Shows disk space usage.
df -h (human-readable format).

du

Disk Usage: Shows disk space usage of files and directories.
du -sh directory_name (summary in human-readable format).
du -h --max-depth=1 directory_name (show usage for each subdirectory).

free

Display amount of free and used memory in the system.
free -m (in MB).
free -g (in GB).

uptime

Show how long the system has been running, current time, number of users, and system load average.

date

Display the current date and time.

Process Management

ps

Process Status: Displays information about active processes.
ps aux (show all processes).
ps -ef (show all processes with full command lines).

top

Display real-time system processes, CPU usage, and memory usage.
Press q to quit.

kill

Terminate a process.
kill process_id (sends a TERM signal).
kill -9 process_id (forcefully kills the process with a KILL signal).

pkill

Kill processes by name.
pkill process_name

bg

Resume a stopped process in the background.
bg %job_id (where job_id is the job number).

fg

Bring a background process to the foreground.
fg %job_id

User Management & Permissions

User Management

whoami

Display the current user’s username.

id

Display user ID, group ID, and groups.

w

Show who is logged on and what they are doing.

last

List last logged-in users.

passwd

Change user password.

su

Switch user.
su username

File Permissions

chmod

Change file permissions.
chmod 755 file_name (rwxr-xr-x)
chmod +x file_name (add execute permission for all)
chmod u+w file_name (add write permission for the user)
chmod g-r file_name (remove read permission for the group)

chown

Change file owner.
chown user file_name
chown user:group file_name (change owner and group)

chgrp

Change file group.
chgrp group file_name

umask

Set default file permissions.
umask 022 (removes write permission for group and others)

ls -l

Shows permissions for the file.
Example: -rwxr-xr--
1st character: d (directory), - (file), l (link)
Next 3 characters: owner permissions (read, write, execute)
Next 3 characters: group permissions
Last 3 characters: others permissions