Basic Linux Commands You Must Know

Vincent Conlon

Linux Tux Penguin Mascot Cutout Lying on a Tray with Ice Cubes

Linux commands are essential tools for managing and using Linux operating systems. They allow users to perform tasks quickly and efficiently through the command line interface. Learning the 30 most used basic Linux commands can greatly improve productivity and system control.

These commands range from simple file operations to complex system management tasks. Users can navigate directories, create and delete files, manage processes, and configure system settings. The command line offers more power and flexibility than graphical interfaces for many tasks.

Mastering these core Linux commands opens up new possibilities for users. It enables them to automate tasks, troubleshoot issues, and customize their system. This knowledge is valuable for both beginners and experienced Linux users alike.

Essential Linux Commands

CommandDescription
lsLists files and directories in the current directory.
cdChanges the current working directory.
pwdPrints the current working directory.
mkdirCreates a new directory.
rmdirRemoves an empty directory.
cpCopies files and directories.
mvMoves or renames files and directories.
rmRemoves files and directories.
catDisplays the contents of a file.
lessViews files page by page.
grepSearches for patterns in files.
findSearches for files and directories.
chmodChanges file permissions.
chownChanges file ownership.
sudoExecutes a command with superuser privileges.
apt or yumPackage manager for installing, updating, and removing software (specific to Debian-based or Red Hat-based distributions, respectively).
manDisplays the manual page for a command.
helpProvides help for built-in commands.

This table provides a good starting point for learning Linux commands. Remember, there are many more commands available, and you can use the man command or online resources to learn more about specific commands and their options.

Here’s a list of additional important Linux commands:

  • File and Directory Management
    • touch: Creates a new empty file or updates the timestamp of an existing file
    • ln: Creates links (symbolic or hard) between files
    • file: Determines the file type
    • tar: Creates and extracts archive files
    • gzip, bzip2, xz: Compresses and decompresses files
  • Process Management
    • ps: Lists currently running processes
    • top: Displays real-time information about system processes
    • kill: Terminates a process
    • bg: Sends a process to the background
    • fg: Brings a background process to the foreground
  • Networking
    • ping: Tests network connectivity
    • ifconfig or ip: Configures and displays network interface information
    • netstat: Displays network connections, routing tables, and interface statistics
    • ssh: Securely connects to a remote system
    • scp: Securely copies files between systems
    • wget: Downloads files from the web
    • curl: Transfers data to or from a server using various protocols
  • System Information and Configuration
    • uname: Displays system information
    • df: Shows disk space usage
    • du: Estimates file and directory space usage
    • free: Displays memory usage
    • date: Displays or sets the system date and time
    • /etc/init.d or systemctl: Starts, stops, and restarts system services (specific to SysVinit or systemd systems, respectively)
  • Text Processing and Editing
    • head: Displays the first few lines of a file
    • tail: Displays the last few lines of a file
    • cut: Extracts specific fields or columns from a file
    • sort: Sorts the lines of a file
    • uniq: Removes duplicate lines from a file
    • wc: Counts words, lines, and characters in a file
    • sed: Performs text transformations and substitutions
    • awk: Processes text files and performs complex pattern matching and actions
    • nano or vim: Text editors for creating and modifying files
  • Other Useful Commands
    • history: Displays a list of previously executed commands
    • alias: Creates shortcuts for frequently used commands
    • export: Sets environment variables
    • echo: Prints text to the terminal
    • clear: Clears the terminal screen

Getting Started with the Terminal

The terminal is a key part of using Linux. It lets you type commands to control your computer. Learning some basic commands will help you use Linux better.

Understanding the Linux Command Line

The command line is where you type instructions for your computer. It shows a prompt, often with your username and computer name. After the prompt, you can type commands.

Most Linux systems use a program called Bash to run commands. Bash is a type of shell, which is software that reads and runs commands. When you open a terminal, you’re using a shell.

To run a command, type it and press Enter. The computer will then do what you asked. If there’s an error, you’ll see a message explaining what went wrong.

Navigating the File System

Moving around files and folders is a common task in Linux. Here are some key commands:

  • pwd: Shows your current location
  • cd: Changes directories
  • ls: Lists files and folders

To see where you are, type pwd and press Enter. To move to a new folder, use cd. For example, cd Documents takes you to the Documents folder.

To see what’s in a folder, use ls. This shows files and folders in your current spot. You can add options to ls for more info. Try ls -l for a detailed list.

Working with Files and Directories

You can make, copy, move, and delete files and folders with these commands:

  • mkdir: Makes a new folder
  • touch: Creates an empty file
  • cp: Copies files or folders
  • mv: Moves or renames files
  • rm: Deletes files or folders

To make a new folder named “test”, type mkdir test. To create a new empty file, use touch. For example, touch newfile.txt makes a file called newfile.txt.

To copy a file, use cp. The format is cp oldfile newfile. To move or rename, use mv in the same way.

Be careful with rm. It deletes files for good. To remove a file, type rm filename. For folders, use rm -r foldername.

File Manipulation and Management

Linux offers many commands for working with files and folders. These tools let users view, change, copy, and find files easily. They make it simple to manage data on a Linux system.

Viewing and Editing Files

The cat command shows file contents on the screen. It’s good for quick looks at small files. For bigger files, less and more are better. They let users scroll through text.

The vi editor is a powerful tool for changing files. It has many features but takes time to learn. For simple edits, nano is easier to use.

Head and tail are useful for seeing parts of files. Head shows the start, while tail shows the end. This helps check log files or large datasets.

Copying and Deleting Files

The cp command copies files and folders. It can make backups or move data to new places. Users can copy single files or whole directories at once.

To remove files, rm is the main tool. It deletes files forever, so users must be careful. The -i flag asks for confirmation before deleting.

For moving or renaming, mv is the command to use. It can shift files between folders or give them new names. Mv works on both files and directories.

Finding Files and Directories

The find command helps locate files and folders. Users can search by name, size, or type. It’s great for finding lost data or cleaning up old files.

Find can also run commands on the files it discovers. This makes it a strong tool for batch operations. Users can delete, copy, or change many files at once.

For quicker searches, locate is a good choice. It uses a database of file names, making it faster than find for simple queries.

File Permissions and Security

Linux uses a system of file permissions and user roles to keep data safe. This setup controls who can read, change, or run files on the system.

Managing Permissions

The chmod command is key for setting file permissions. It uses numbers or letters to set read, write, and execute rights. For example:

  • 4 means read
  • 2 means write
  • 1 means execute

To give full rights to the owner and read-only to others:

chmod 744 filename

The chown command changes file ownership. To make “user1” the owner:

chown user1 filename

Use ls -l to check current permissions. It shows details like this:

-rw-r--r-- 1 user1 group1 4096 Aug 11 2024 filename

This means the owner can read and write, while others can only read.

User and Group Administration

Linux manages users and groups to control system access. Key commands include:

  • useradd: Makes new user accounts
  • usermod: Changes user settings
  • passwd: Sets or changes passwords
  • groups: Shows group memberships

To add a new user:

sudo useradd newuser
sudo passwd newuser

The sudo command lets users run tasks with admin rights. It’s safer than always using the root account.

To see who you’re logged in as, use the whoami command. The who command shows all logged-in users.

Regular checks and updates help keep the system secure. Change passwords often and limit sudo access to trusted users.

Monitoring and Process Management

Linux offers powerful tools for keeping tabs on system resources and managing running programs. These tools help users track performance and control processes effectively.

Monitoring System Resources

The df command shows disk space usage. It displays free and used space on file systems. Users can see total, used, and available space in human-readable format.

The du command measures file and directory sizes. It helps find large files taking up disk space. Users can sort output to spot the biggest space hogs.

Free memory can be checked with the free command. It shows total, used, and available RAM. The command also displays swap space usage.

Top is a real-time system monitor. It shows running processes and resource usage. Users see CPU, memory, and other stats updated live.

Managing Processes

The ps command lists running processes. It gives details like process ID, CPU use, and memory use. Users can filter output to find specific processes.

To stop a process, the kill command is used. It sends a signal to end a program. The most common signal is SIGTERM, which asks a process to exit cleanly.

The service command starts, stops, and restarts system services. It’s useful for managing background programs. Users can check service status too.

Analyzing System Logs

The grep command searches text files for patterns. It’s great for finding specific log entries. Users can look for error messages or other important info.

Tail shows the end of a file. It’s handy for watching the newest log entries. The -f option follows the file, showing updates in real-time.

The less command views files one screen at a time. It lets users scroll through long log files easily. Search features help find relevant parts quickly.

Cat displays entire file contents. It’s useful for short log files. Users can pipe its output to other commands for more processing.

Networking and Remote Access

Linux offers powerful tools for managing network connections and accessing remote systems. These commands help users set up networks and work with distant computers.

Using Network Utilities

The ping command checks if a computer is online. It sends a small packet of data to another device. If the device gets the packet, it sends one back. This shows the connection works.

Ifconfig sets up network cards. It can change IP addresses and other settings. The newer ip command does similar tasks. It’s now the main tool for network setup on many Linux systems.

Netstat shows active network links. It lists open ports and current connections. This helps find issues with network programs.

Accessing Remote Systems

SSH lets users log in to other computers safely. It encrypts all data sent between machines. This keeps information private.

SCP copies files between computers using SSH. It works like the regular copy command but for remote systems.

Wget downloads files from the web. It can get single files or entire websites. Wget works with HTTP, HTTPS, and FTP.

These tools make it easy to work with far-away computers. They help users manage servers and get data from the internet.

Package Management and Updates

Linux systems use package managers to install and update software. These tools make it easy to keep your system up-to-date and secure.

Managing Packages with apt and yum

Apt and yum are two popular package managers for Linux. Apt is used on Debian-based systems like Ubuntu. Yum is used on Red Hat-based systems like CentOS.

To update your system with apt:

  1. Open a terminal
  2. Type sudo apt update
  3. Then type sudo apt upgrade

This will update all installed packages.

To install a new package with apt:

sudo apt install package_name

Yum works in a similar way. To update your system with yum:

sudo yum update

To install a new package with yum:

sudo yum install package_name

Both apt and yum make it easy to find and install software on your Linux system.

Compiling Programs from Source

Sometimes you may need to install a program that isn’t in your package manager. In these cases, you can compile the program from source code.

To compile a program:

  1. Download the source code
  2. Extract it with the tar command
  3. Run the configure script
  4. Use the make command to compile
  5. Install with make install

Here’s an example:

tar -xvf program.tar.gz
cd program
./configure
make
sudo make install

This process can be more complex than using a package manager. But it allows you to install any program on your system.

Customizing the Shell Environment

The shell environment can be tailored to fit your needs. You can set up shortcuts and control how programs run.

Setting and Using Aliases

Aliases are nicknames for long commands. They save time and make work easier. To create an alias, use this format:

alias nickname='command'

For example:

alias update='sudo apt-get update'

Now typing “update” will run the full command. To list all aliases, type “alias” without options. To remove an alias, use the unalias command.

Aliases only last for the current session. To make them permanent, add them to your shell config file (like .bashrc).

Environment Variables and Paths

Environment variables store info that programs can use. The $PATH variable tells the system where to find programs.

To see all variables, use the “env” command. To set a new variable:

export VARIABLE_NAME=value

To add a directory to $PATH:

export PATH=$PATH:/new/directory

This change only lasts for the current session. For a permanent change, add the line to your shell config file.

You can also use “echo” to view a single variable:

echo $PATH

These tools let you shape your shell to work best for you.

Advanced Command Line Techniques

Linux offers powerful tools for complex tasks. These methods help users work faster and more efficiently.

Text Processing and Searching

The grep command finds text in files. It can search for words or patterns. Here’s a simple example:

grep "error" logfile.txt

This shows all lines with “error” in logfile.txt.

The sort command orders text. It can sort numbers or words. Use it like this:

sort names.txt

This puts names in alphabetical order.

The comm command compares two files. It shows lines that are the same or different. The diff command also compares files. It shows what changed between them.

These tools are great for finding info in big files. They also help spot changes in data.

Archiving and Backup

The tar command makes file archives. It packs many files into one. This is useful for backups or sharing files. Here’s how to make an archive:

tar -cvf backup.tar /home/user

This creates a backup.tar file with all files from /home/user.

The zip command compresses files. It makes them smaller to save space. Use it like this:

zip -r archive.zip folder/

This zips all files in “folder” into archive.zip.

For disk copying, dd is a handy tool. It can copy whole hard drives. Be careful with dd. It can erase data if used wrong.

Disk Management

The mount command links file systems to folders. It lets you access files on other drives. For example:

mount /dev/sdb1 /mnt/usb

This connects a USB drive to the /mnt/usb folder.

The df command shows disk space usage. It tells you how much room is left on your drives. Use it like this:

df -h

The -h option shows sizes in a human-readable format.

For partitioning disks, fdisk is a key tool. It can create, delete, and change disk partitions. Always back up data before using fdisk.

These tools help manage storage space and keep data safe. They’re vital for system admins and power users.

Help and Documentation

Linux offers tools to learn about commands and get help. These tools make it easier to use the system and find information about different commands.

Accessing Manual Pages

The man command shows detailed info about other commands. To use it, type “man” followed by the command name. For example, “man ls” gives details about the ls command.

Man pages have sections. These include a description, options, and examples. You can move through the page using arrow keys. Press “q” to exit.

Some commands also have an info page. The info command gives more in-depth details than man pages. It works like man, but with a different format.

Getting Help with Commands

Many commands have a built-in help option. You can use “–help” or “-h” after a command to see basic info. This shows how to use the command and its main options.

The which command finds where a program is located. It tells you the full path of the command.

Type shows what a command is. It tells if it’s built-in or an external program.

These tools help users learn about commands quickly. They make it easier to use Linux and solve problems.

Frequently Asked Questions

Linux commands are key tools for system management and daily tasks. Learning the most important ones can boost productivity and efficiency.

What are the essential Linux commands for beginners?

New Linux users should start with basic commands. These include ls for listing files, cd for changing directories, and pwd for showing the current directory. The mkdir command creates new folders, while rm deletes files.

Can you provide examples of common Linux commands used in daily operations?

Daily Linux tasks often use simple commands. The cat command displays file contents. cp copies files, and mv moves or renames them. grep searches for text patterns in files. chmod changes file permissions.

What is the best way to memorize frequently used Linux commands?

Practice is key to learning Linux commands. Regular use helps build memory. Creating a cheat sheet with common commands can also help. Trying different options for each command aids understanding and recall.

Which Linux commands are indispensable for file management?

File management in Linux relies on several core commands. These include touch for creating files, rm for deleting, and mv for moving or renaming. The find command locates files, while du shows disk usage.

How can one quickly reference syntax for various Linux commands?

The man command provides detailed help for Linux commands. Typing –help after a command shows basic usage info. Online resources and command cheat sheets offer quick references. Some terminals have built-in help features.

What are some must-know Linux commands for system administration?

System admins use specific Linux commands often. The top command shows running processes. ps displays process status. df checks disk space. useradd creates new user accounts. sudo runs commands with admin rights.