Home Linux Commands Fundamental Linux Commands For Newbies (2024 Update)

Fundamental Linux Commands For Newbies (2024 Update)

Mastering the Linux Basics: Essential Linux Commands for New and Intermediate Users.

By sk
Published: Updated: 2.1K views

This comprehensive guide explains fundamental Linux commands every user should know. From navigating directories and managing files to monitoring system resources and troubleshooting issues, these essential Linux commands will help you to master the Linux command line with ease.

We have categorized the commands into various groups, each accompanied by practical examples for better understanding. By the end, you'll have a solid foundation for using the Linux terminal with confidence.

File and Directory Management on Linux

For Linux users, working with files and directories through the command line is a fundamental skill.

Whether you're a newbie or an experienced user, the following file management commands will help you to navigate, create, copy, move, and manipulate files and directories with ease.

Listing Directory Contents

The ls command is your go-to tool for displaying the contents of a directory.

Run ls in your terminal, and it will show you the names of files and subdirectories within the current working directory.

ls

But what if you want more information? Use the -l option for a long listing format, revealing details like file permissions, ownership, size, and last modification date.

ls -l

Don't forget about those sneaky hidden files that start with a dot (.). To view the hidden files and directories, add the -a option.

ls -a

Combine these options with ls -al to get a comprehensive view of all files and directories, including hidden ones, in a detailed long listing format.

ls -al

Sample Output:

total 80
drwxr-x--- 16 ostechnix ostechnix 4096 May 13 13:18 .
drwxr-xr-x 3 root root 4096 May 13 13:13 ..
-rw------- 1 ostechnix ostechnix 169 May 13 13:29 .bash_history
-rw-r--r-- 1 ostechnix ostechnix 220 Mar 31 14:11 .bash_logout
-rw-r--r-- 1 ostechnix ostechnix 3771 Mar 31 14:11 .bashrc
drwx------ 10 ostechnix ostechnix 4096 May 13 17:50 .cache
drwx------ 12 ostechnix ostechnix 4096 May 13 17:42 .config
drwxr-xr-x 2 ostechnix ostechnix 4096 May 13 13:14 Desktop
drwxr-xr-x 2 ostechnix ostechnix 4096 May 13 13:14 Documents
drwxr-xr-x 2 ostechnix ostechnix 4096 May 13 13:14 Downloads
drwx------ 2 ostechnix ostechnix 4096 May 13 13:18 .gnupg
drwx------ 4 ostechnix ostechnix 4096 May 13 13:13 .local
drwxr-xr-x 2 ostechnix ostechnix 4096 May 13 13:14 Music
drwxr-xr-x 2 ostechnix ostechnix 4096 May 13 13:14 Pictures
-rw-r--r-- 1 ostechnix ostechnix 807 Mar 31 14:11 .profile
drwxr-xr-x 2 ostechnix ostechnix 4096 May 13 13:14 Public
drwx------ 5 ostechnix ostechnix 4096 May 13 18:00 snap
drwx------ 2 ostechnix ostechnix 4096 May 13 13:14 .ssh
-rw-r--r-- 1 ostechnix ostechnix 0 May 13 13:14 .sudo_as_admin_successful
drwxr-xr-x 2 ostechnix ostechnix 4096 May 13 13:14 Templates
drwxr-xr-x 2 ostechnix ostechnix 4096 May 13 13:14 Videos

Related Read:

Navigating Directories

Now that you know how to look inside directories, it's time to learn how to move around. Here's where the cd command comes in handy.

cd directory_name

This command changes your current working directory to directory_name. For example, cd Documents will take you to the Documents folder.

But what if you want to move up a level? No problem! Just use cd .. to navigate to the parent directory.

cd ..

And if you ever feel lost or need to start over, simply run cd / to return to the root directory.

cd /

To double-check your current location, use the pwd command, which stands for "print working directory". It'll display the full path of your current working directory.

pwd

Related Read:

Creating and Deleting Directories

Now that you're a master of navigating directories, let's learn how to create and remove them.

To create a new directory, use the mkdir command followed by the desired directory name.

mkdir new_directory

But what if you need to create nested directories? No sweat! Just add the -p option, and mkdir will create any necessary parent directories along the way.

mkdir -p /path/to/new/directory

Please note that you can also create directories from a text file.

When it's time to clean up, use rmdir to remove an empty directory.

rmdir directory_to_remove

However, if you need to delete a directory and all its contents, use the rm command with the -r option (be careful with this one!).

rm -r directory_to_remove

Working with Files

Managing files is just as important as managing directories. Let's dive into some essential file commands.

To create a new, empty file or update the timestamp of an existing file, use the touch command.

touch new_file.txt

Copying files is a breeze with the cp command. Simply specify the source file and the destination file or directory.

cp source_file.txt destination_file.txt
cp source_file.txt directory/

Need to move or rename a file? No problem! The mv command has got you covered.

mv source_file.txt new_name.txt
mv source_file.txt directory/

Deleting files is as simple as using the rm command, but be careful – this operation is irreversible.

rm file_to_delete.txt

If you need to force the deletion of a file (e.g., if it's read-only), add the -f option.

rm -f read_only_file.txt

Sometimes, you just need to take a quick peek at the contents of a file. The cat command is just perfect for that.

cat file.txt

But what if the file is too long to fit on your screen? Use less for a more interactive viewing experience, with options to navigate through the file page by page.

less long_file.txt

Need to see just the beginning or end of a file? The head and tail commands have got you covered.

head file.txt    # Shows the first 10 lines
tail file.txt    # Shows the last 10 lines

And if you're monitoring a log file or any other constantly growing file, use tail -f to continuously display new lines as they're added.

tail -f log_file.log

Creating Symbolic Links

Now, let's talk about symbolic links.

These special file references allow you to access the target file from multiple locations without duplicating its contents.

To create a symbolic link, use the ln command with the -s option, followed by the source file and the desired link name.

ln -s source_file.txt link_to_file

With these commands, you'll be a file and directory management pro on Linux in no time!

Related Read:

Secure Remote Access with SSH on Linux

Working remotely is a common scenario for many Linux users and administrators. The Secure Shell protocol, better known as SSH, allows you to establish encrypted connections to remote systems over insecure networks like the internet.

Let's discuss some essential SSH commands that will make remote access a breeze.

Connecting to Remote Hosts

As stated already, SSH allows us to connect to remote hosts securely.

The basic syntax is straightforward:

ssh user@host

This command will initiate an SSH connection to the host system, prompting you for the user's password. Once authenticated, you'll have a secure shell session on the remote system.

Example:

ssh ostechnix@192.168.1.100

But what if the SSH server on the remote host is listening on a non-standard port? No problem! Just specify the port with the -p option.

ssh -p port_number user@host

Replace port_number with the actual port number the SSH server is using on the remote host.

Example:

ssh -p 2200 ostechnix@192.168.1.100

Related Read:

Dynamic Port Forwarding

SSH isn't just for interactive shell sessions; it can also be used for secure tunneling and port forwarding.

This feature is incredibly handy when you need to access services or resources on a remote network that might be blocked by firewalls or other security measures.

Dynamic application-level port forwarding allows you to forward arbitrary data streams through an encrypted SSH tunnel. Here's how you set it up:

ssh -D port_number user@host

This command will connect you to the remote host as user and set up a local SOCKS proxy server on the specified port_number.

Any application configured to use this SOCKS proxy will automatically have its traffic forwarded through the encrypted SSH tunnel, effectively bypassing any restrictions imposed by firewalls or network policies.

This feature is particularly useful when you need to access web services, email servers, or other internet resources from within a restricted network environment, such as a corporate or educational network.

With these SSH commands, you'll be able to securely access and manage remote systems with ease, regardless of your location or network constraints.

Essential Network Commands on Linux

Whether you're troubleshooting connectivity issues, downloading files from the web, or investigating domain information, the following network commands will become invaluable tools for Linux network mangement.

Testing Network Connectivity

When faced with network problems, the first step is often to check if you can reach a remote host.

Enter the ping command, your trusty network diagnostic tool:

ping host

Replace host with the IP address or domain name you want to test connectivity with. The ping command will send packets to the target host and report back on the response times, helping you identify potential network issues.

Related Read:

Investigating Domain Information

Curious about the details behind a particular domain name? The whois and dig commands have got you covered.

whois domain

This command retrieves the whois information for the specified domain, revealing details such as the domain registrar, registration dates, and contact information.

dig domain

The dig command, short for "domain information groper," queries DNS servers for information about the specified domain. This includes IP addresses, name servers, and other DNS records associated with the domain.

But dig can do more than just forward lookups. Need to find out what domain name corresponds to a specific IP address? Use the -x option for a reverse DNS lookup:

dig -x ip_address

Downloading Files from the Web

Sometimes, you might want grab a software package, documentation, or any other online resources from the Web. Enter wget, your command-line downloader utility.

wget file_url

Simply provide the URL of the file you want to download, and wget will fetch it for you. But that's just the beginning of its capabilities.

If your download gets interrupted for any reason, no need to start from scratch. Just use the -c option to continue where you left off:

wget -c file_url

And what if you need to download an entire website or directory structure? wget has you covered with its recursive download feature:

wget -r url

This command will recursively download files from the specified url, following links and mirroring the directory structure on your local system.

By using these network commands, you'll be able to diagnose connectivity issues, investigate domain information, and download files from the web with ease, all from the comfort of your Linux terminal.

System Information Commands on Linux

As a Linux user or administrator, you should know how to retrieve detailed information about your system's state and resources.

From checking system uptime and user activity to inspecting hardware details and disk usage, the following commands will provide you with a system insights and help you better understand and manage your Linux environment.

Keeping Track of Time and Dates

Let's start with the basics – staying up to date with the current time and date. The date command is your go-to tool for this:

date

Need to check the calendar for a specific month? The cal command has got you covered:

cal

You can even specify a different month or year by providing additional arguments.

Monitoring System Uptime and User Activity

Curious about how long your system has been running? The uptime command will give you that information, along with some additional load statistics:

uptime

Want to see who's currently logged in and what they're doing? The w command provides a concise overview of user activity:

w

Not sure about your current username? No problem! Just run whoami to find out:

whoami

Inspecting Hardware and System Details

Ready to dive deeper into your system's hardware specifications? The uname command is your friend:

uname -a

This command will display details such as the kernel version, operating system name, and processor architecture.

Speaking of processors, you can get detailed CPU information by inspecting the /proc/cpuinfo file:

cat /proc/cpuinfo

Similarly, the /proc/meminfo file provides insights into your system's memory usage and configuration:

cat /proc/meminfo

Related Read:

Analyzing Disk Space and Memory Usage

Keeping an eye on disk space and memory usage is crucial for maintaining a healthy and efficient system.

The df command shows you how much disk space is available and used on your various file systems:

df

To view the output in human-readable format, add -h option.

df -h

Need a more granular view of directory space usage? The du command has got you covered:

du directory_path

For a human-readable summary of a directory's size, add the -sh options:

du -sh directory_path

Check the following link to understand df and du commands in detail.

And when it comes to memory, the free command gives you a breakdown of available and used memory, including swap space:

free -m

The -m option displays the values in megabytes for easier comprehension.

Locating Executable Files

If you ever need to find out the exact location of an executable file on your system, the which command is here to help:

which application_name

This command will search your system's PATH and print the full path to the specified application_name executable.

Example:

$ which firefox
/usr/bin/firefox

Access Command History

The history command is a built-in shell command that lists the previously executed commands in the current terminal session. It provides a convenient way to review, repeat, or modify previous commands without having to retype them from scratch.

Basic Usage:

history

This will display a numbered list of all commands executed in the current session, with the most recent command listed last.

Limiting Output:

history 20

This will show only the last 20 commands in the history list.

Repeating a Command:

!n

Where n is the line number of the command in the history list. This will execute the command associated with that line number.

For example, !515 would execute the command listed at line 515 in the history.

Searching for a Command:

ctrl+r

This will allow you to search for a specific command in the history by typing part of the command. Press ctrl+r repeatedly to cycle through matching commands.

Clearing History:

history -c

This will clear the history list for the current session.

The history command is particularly useful when you need to repeat a complex command, review your recent activity, or debug issues by analyzing the commands you've executed.

It can significantly improve your productivity and efficiency when working with the Linux command line.

Related Read - How To Clear Command Line History In Linux

Getting Command Help

Feeling lost or unsure about how to use a particular command? The man (short for "manual") command is your companion.

It provides comprehensive documentation and usage examples for the given command.

man command

Replace command with the name of the command you need help with, and the manual will be displayed right in your terminal.

Example:

man uname

Related Read:

With these system information commands, you'll have a deep understanding of your Linux system's state, resources, and hardware configuration.

Linux Process Management Commands

Whether you're managing system resources, troubleshooting issues, or simply keeping an eye on what's running, these process management commands will give you complete control over your system's processes.

Listing Running Processes

To get a glimpse of the processes currently running on your system, you can use the ps command.

Run the ps command without any arguments to see the processes associated with your current shell:

ps

But for a more comprehensive view, use the aux options to display detailed information about all running processes, including those owned by other users:

ps aux

This will show you a detailed information for each process, such as the user who owns it, the CPU and memory usage, and the command that launched it.

Related Read:

Terminating Processes

Sometimes, you may need to terminate a misbehaving or unresponsive process. The kill command is your friend in these situations:

kill pid

Replace pid with the process ID (PID) of the process you want to terminate. You can find the PID by running ps aux and looking for the process in question.

If you'd rather terminate all instances of a particular process, use the killall command instead:

killall process_name

This command will send a termination signal to all processes with the specified process_name.

Related Read: Fkill – Interactively Search And Kill Processes On Linux

Managing Background and Foreground Processes

In Linux, you can run processes in the background or foreground. Background processes run independently, allowing you to continue using your terminal for other tasks, while foreground processes occupy your terminal until they're completed or stopped.

If you have a process running in the foreground that you'd like to move to the background, use the bg command:

bg

This will resume the most recent job in the background, freeing up your terminal for other tasks.

Conversely, if you have a background process that you'd like to bring to the foreground, use the fg command:

fg

This will move the most recent background job to the foreground, allowing you to interact with it directly.

But what if you have multiple background jobs and need to bring a specific one to the foreground? No problem! Just provide the job number:

fg job_number

You can find the job number by running the jobs command, which will list all background jobs.

By using these process management commands, you'll have complete control over your system's processes, enabling you to monitor, terminate, and manage them easily.

Securing Your Files with Linux Permissions

In Linux, file permissions play a crucial role in maintaining system security and controlling access to sensitive data.

The following commands allow you to set and modify permissions for files and directories, ensuring that only authorized users can read, write, or execute them.

Understanding File Permissions

Before we dive into the commands, let's quickly review how file permissions work in Linux.

Each file or directory has three sets of permissions: one for the owner, one for the group, and one for others (all users not in the owner's group).

Each set of permissions consists of three flags:

  • r (read): Grants the ability to read the contents of the file or list the contents of a directory.
  • w (write): Grants the ability to modify or delete the file or create new files in a directory.
  • x (execute): Grants the ability to execute the file (if it's a program or script) or access the contents of a directory.

Changing File Permissions with chmod

The chmod command is used for modifying file permissions. It allows you to set permissions using either symbolic or octal notation:

chmod octal_value file

In octal notation, each set of permissions (owner, group, others) is represented by a single digit, calculated by adding the values of the desired permissions:

  • 4 for read
  • 2 for write
  • 1 for execute

For example, to grant read, write, and execute permissions for the owner, and read and execute permissions for group and others, you would use:

chmod 755 file

The octal value 755 breaks down to:

  • 7 (4+2+1) for the owner (read, write, execute)
  • 5 (4+1) for the group (read, execute)
  • 5 (4+1) for others (read, execute)

You can also use symbolic notation to modify specific permissions:

chmod u=rwx,g=rx,o=r file

This command grants read, write, and execute permissions to the owner (u=rwx), read and execute permissions to the group (g=rx), and read permissions to others (o=r).

Recursively Changing Permissions

If you need to change permissions for an entire directory structure, including all subdirectories and files, use the -R option with chmod:

chmod -R 755 directory

This command will recursively apply the specified permissions (755 in this case) to the directory and all its contents.

File permissions are a important aspect of Linux security, and mastering these commands will help you maintain a secure and controlled environment for your files and directories.

Remember to use these commands responsibly and always double-check your settings before making changes to ensure you don't inadvertently grant or revoke access to sensitive data.

We have a dedicated guide on explaining Linux permissions in detail. If you're interested, please check the following link:

Compressing and Archiving Files in Linux

Compression and archiving play a vital role in saving disk space and facilitating the transfer of large files or collections of files.

Linux offers a powerful set of commands for creating, extracting, and managing compressed archives, allowing you to work efficiently with your data.

The tar Command

At the heart of compression and archiving on Linux lies the tar command, which stands for "tape archiver".

Although originally designed for creating archives on tape backup devices, tar has become an indispensable tool for compressing and decompressing files on modern systems.

Creating Archives

To create a new archive, simply use the c option followed by the desired archive name and the files or directories you want to include:

tar cf archive.tar files_to_include

This command will create a new archive named archive.tar containing the specified files_to_include. You can include multiple files and directories by separating them with spaces.

Related Read:

Extracting Archives

When you need to extract the contents of an archive, use the x option with tar:

tar xf archive.tar

This command will extract the files and directories from archive.tar into the current working directory.

Listing Archive Contents

Before extracting an archive, you may want to preview its contents. The t option allows you to list the files and directories inside an archive:

tar tf archive.tar

This command will display the contents of archive.tar without actually extracting them, giving you a glimpse of what's inside.

Compressing Archives

While tar is great for creating archives, it doesn't actually compress the data by default.

To create compressed archives and save disk space, you'll need to combine tar with a compression utility like gzip or bzip2.

Creating Compressed Archives

Here's how you can create a compressed archive using gzip:

tar czf archive.tar.gz files_to_include

The c option creates a new archive, z specifies that the archive should be compressed using gzip, and f indicates that the archive name should follow.

Similarly, you can use bzip2 for better compression ratios (but slower compression/decompression speeds):

tar cjf archive.tar.bz2 files_to_include

Extracting Compressed Archives

Extracting compressed archives follows the same pattern as uncompressed ones, but you'll need to specify the appropriate decompression utility:

tar xzf archive.tar.gz  # For gzip-compressed archives
tar xjf archive.tar.bz2 # For bzip2-compressed archives

Learning these compression and archiving commands will help you efficiently manage your files, save disk space, and transfer large data across systems or networks.

NOTE: Remember, compression can be a double-edged sword – while it saves space, it also increases CPU load during compression and decompression. Always consider your use case and performance requirements when working with compressed archives.

File and Directory Searching Commands

Linux directory structure is very complex. The ability to search and find specific files, directories, or patterns within files is an invaluable skill.

Whether you're looking for a configuration file, debugging log entries, or searching through code repositories, the following commands will be quite helpful.

The grep Command

The grep command, which stands for "global regular expression print", allows you to search for patterns within files, directories, or even the output of other commands.

Searching Within Files

To search for a specific pattern within one or more files, use the following syntax:

grep pattern file1 file2 ...

Replace pattern with the text or regular expression you're looking for, and specify the files you want to search through.

Recursive Directory Searches

If you need to search within an entire directory structure, including subdirectories, add the -r option:

grep -r pattern directory

This command will recursively search for the specified pattern within the directory and all its subdirectories.

Searching Command Output

One of the most powerful features of grep is its ability to search through the output of other commands. Use the pipe (|) symbol to feed the output of a command into grep:

command | grep pattern

This technique is particularly useful for filtering and searching through log files or command output in real-time.

For example:

ps aux | grep firefox

This will list all running processes that have "firefox" in their name or command line.

For more details, check our Grep tutorial in the following link:

Finding Files with locate

While grep is great for searching within files, the locate command is your go-to tool for finding files and directories on your system.

The locate command uses a pre-built database of file names and locations, allowing for lightning-fast searches:

locate file_name

This command will search the database and display the full paths of all files and directories matching file_name.

It's important to note that the locate database is periodically updated, so it may not always reflect the most recent changes to your file system.

If you need to ensure you're searching the latest data, use the updatedb command to update the database before running locate.

Using these searching commands, you can quickly and efficiently find the files, directories, or patterns you need in your Linux system.

Additional Commands

The commands covered so far are indeed fundamental and essential for managing files, processes, and system information on Linux.

But, they represent just the tip of the iceberg when it comes to the vast array of commands and tools available in the Linux ecosystem.

For a beginner and intermediate user, mastering these fundamental commands is an excellent starting point, as they will provide you with a solid foundation for navigating and administering your Linux system.

As you gain more experience and confidence, you'll likely want to expand your knowledge and learn additional commands and tools to perform more advanced tasks.

Here are some additional commands and tools that can be valuable for a Linux beginner to learn:

Text Editing Commands

  • nano - A simple and user-friendly command-line text editor, perfect for beginners.
  • vi or vim - A powerful and ubiquitous text editor with a steep learning curve but incredibly versatile once mastered.

Package Management Commands

  • apt (Debian/Ubuntu) or dnf (RHEL/CentOS) - Commands for installing, updating, and managing software packages on your Linux distribution.

User and Group Management Commands

  • useradd - Creates a new user account.
  • userdel - Removes a user account.
  • groupadd - Creates a new group.
  • groupdel - Removes a group.
  • usermod - Modifies user account properties.
  • groupmod - Modifies group properties.

System Administration Commands

  • systemctl - Manages and controls system services and daemons.
  • journalctl - Displays and manages system logs.
  • top or htop - Displays real-time information about running processes and system resource usage.
  • iptables or firewall-cmd - Configures and manages the system's firewall rules.

Network Configuration Commands

  • ip - Displays and configures network interfaces and routing tables.
  • ifconfig - An older command for configuring network interfaces (being replaced by ip).
  • netstat or ss - Displays network connections, routing tables, and network interface statistics.
  • nmap - A powerful network exploration and security auditing tool.

As you explore Linux further, you'll discover many more specialized commands and tools for specific tasks, such as system monitoring, security, virtualization, and software development.

We will cover them all in separate article in future.

Conclusion

In this tutorial, we covered many fundamental Linux commands for both beginners and intermediate users. While not exhaustive, these essentials provide a solid foundation.

The Linux command-line interface is a vast and powerful ecosystem, and continuous learning is key to mastering it.

Remember, practice is essential when learning new commands. Don't be afraid to experiment in a safe, non-production environment, and consult the extensive documentation and online resources available for Linux.

With time and dedication, you'll become proficient in navigating and administering your Linux system using the command line.


Related Read:


You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More