Excalibur's Sheath

Essential Linux Commands

Mar 23, 2025 • linux,server,bash,cli

There are commands for working in the Linux command line.

The basic commands can be broken down in the following groups:

The following commands help us navigate the filesystem:

  • pwd
  • ls
  • cd

pwd

The pwd command prints the current directory.

It is used this way:

pwd

ls

The ls directory lists directory contents. This command has several flags that can be passed to it which will modify the output.

It can be called as:

ls

There are four flags I find useful:

  • -l
  • -h
  • -a
  • -d

-l Displays detailed information about files and directories.

-h Print file sizes in a human readable format (1K, 1M, 1G).

-a Displays all files & directories, including hidden files & directories. Naming a file starting with a period (.) makes it hidden.

-d Lists directories instead of their contents.

This form of the ls command is called as:

ls -lhad

cd

The cd command changes the current working directory.

The cd command can change directory to a specific directory, or use a couple of shortcuts:

cd /path/to/destination

cd ~

cd ..

cd ~ changes directory to the user’s home directory.

cd .. changes directory up one level on the filesystem. You can move up the filesystem more than one level by using /.. for each level you want to go past the first level.

To go up two levels use this:

cd ../..

File Manipulation

The following commands help us manipulate files:

  • touch
  • cp
  • mv
  • rm

touch

touch will create an empty file with the specified name.

touch fileToCreate

cp

cp will copy one file or directory to another.

cp startingFile endingFile

There are flags which can be passed to the cp command, -R or -r is probably one of the most important one to use. It will copy a directory recursively.

cp -R directory1 directory2

mv

mv will move a file or directory from its current one to a new one.

mv fileOne fileTwo

mv is also used to rename a file.

mv oldName newName

rm

rm will delete files. It can take flags, and it can accept wildcards in the filenames.

rm fileToDelete

rm has two flags worth noting, -r & -f; -r will delete all files and directories in the named directory, -f will remove rm asking if the file(s) should be deleted.

There is a famous rm command you should NEVER use:

rm -rf /

This will delete your whole filesystem.

Permissions & Ownership

The following commands help us change & manage file and directory ownership, access, & permissions.

The commands are:

  • chmod
  • chown
  • umask

chmod

Linux Permissions work on the basis of Owner, Group, & Others.

We can set permissions using numbers for each group.

Each number is composed by adding the following together.

4 - Read 2 - Write 1 - Execute 0 - No Permissions

We add them together for each category of user: 7 - Read, Write, Execute 6 - Read, Write 5 - Read, Execute 4 - Read 3 - Write, Execute 2 - Write 1 - Execute 0 - No Permissions

In addition we can use + or - and r,w,x to add or remove read, write, & execute permissions.

Be sure to use the least amount of permissions needed for security.

chmod 744 fileName

chmod +x fileName

chown

Linux file ownership has three categories: Owner, Group, & Other.

The chown command allows files & directories to have their owner & group changed.

chown owner:group filename

A -R flag can be used to recursively change ownership

chown -R user:group diretory-to-change/*

umask

New files are created with a maximum of 666 (read & write for all).

New directories are created with 777 (read, write & execute for all).

But the actual permissions are calculated like this:

Final Permissions = Default Permissions - umask value

For Example:

If your umask is set to 022:

Files: 666 - 022 = 644 readable and writable by the owner, readable by group and others.

Directories: 777 - 022 = 755 owner can read, write, execute; group and others can read and execute.

To check your current umask setting:

umask

Output might be something like 0022.

To change the umask value for a session:

umask 027

This will restrict group to read-only and deny all permissions to others.

To make it permanent, add it to your shell’s configuration file (e.g., ~/.bashrc or /etc/profile for system-wide settings).

A misconfigured umask could leave your files vulnerable to prying eyes or accidental edits. Whether securing a personal development environment or hardening production systems, umask ensures that newly forged files have the right armor from the start.

Recommended Defaults

Workstation (Developer-friendly): umask 022

Hardened Server: umask 027 or 077

Reading Files

cat (short for concatenate) is your fast, no-frills tool for reading file contents straight to the terminal.

Usage:

cat filename

When to Use It:

To quickly display the full contents of a small file.

To concatenate multiple files together:

cat file1 file2 > combined_file

Strengths:

Fast and straightforward for short files.

Weaknesses:

If used on large files, it can flood your terminal, making it hard to navigate or control.

less

less is a pager utility—think of it as your reconnaissance drone. It lets you navigate large files interactively, without overwhelming your screen.

Usage:

less filename

Navigation:

Scroll down: SPACE or j

Scroll up: b or k

Search forward: /search_term

Quit: q

When to Use It:

To safely inspect large log files, code, or configuration files.

To search through a file without loading it all into memory.

Strengths:

Handles massive files smoothly.

Provides search and navigation tools right inside the viewer.

Weaknesses:

A tiny learning curve if you’re new to pagers.

file

file peers beneath the surface and tells you what a file actually is, based on its contents—not just its name.

Usage:

file filename

When to Use It:

To detect the true type of a file (text, binary, executable, image, etc.).

To investigate mystery files without blindly opening them.

Example Output:

file script.sh #Output: script.sh: Bourne-Again shell script, ASCII text executable

file document.pdf #Output: document.pdf: PDF document, version 1.7

Strengths:

Prevents accidental execution or misinterpretation of unknown files.

Aids in digital forensics and troubleshooting.

Weaknesses:

Limited on certain obfuscated or encrypted files (but still highly useful).

Conclusion

Learning these commands including their common flags is essential for Linux system administration. Practice these commands, and try new flags. The more familiar you are the faster issues can be resolved. Find trusted sources to use as references to learn new commands.