Sign In
Sign In

How to Copy Files and Directories in Linux

How to Copy Files and Directories in Linux
Emmanuel Oyibo
Technical writer
Linux
07.02.2025
Reading time: 8 min

When you first start working with Linux, one of the essential tasks you’ll encounter is file management. Whether you’re organizing your personal documents, migrating system files, or preparing comprehensive backups, knowing how to duplicate your files accurately is crucial. At the heart of this process is the cp command—a robust utility designed to replicate files and directories effortlessly.

This guide is designed to help you master the cp command. We’ll explore everything from basic file copying to recursive directory replication, along with tips for preserving file metadata and preventing accidental data loss. With detailed examples, real-world scenarios, and best practices, you’ll soon be equipped to use cp like a seasoned Linux professional.

Diving into the cp Command

In Linux, the cp command functions as your primary tool for copying data. Its versatility allows you to handle everything from a single file copy to mirroring complex directory structures with nested subfolders. Unlike graphical file managers, the cp command works entirely from the terminal, giving you precise control over every aspect of the copy process.

How It Works

At its simplest, cp takes a source file (or directory) and duplicates it to a new location. Its flexibility, however, lies in its options—flags that let you modify its behavior to suit your needs. Whether you’re preserving file permissions, ensuring no accidental overwrites occur, or copying entire folder trees, cp has a flag for every scenario.

Basic Command Structure

The cp command follows a simple format. Here’s the canonical syntax:

cp [options] source destination
  • cp: The command to initiate a copy.
  • [options]: Additional parameters (flags) that control the behavior of the copy process.
  • source: The file or directory you wish to duplicate.
  • destination: The target location or filename for the copy.

This straightforward structure makes cp a favorite among system administrators and casual users alike.

Exploring Key Options

The true power of cp is unlocked through its myriad options. Let’s review some of the most useful ones:

  • Recursive Copying (-r or -R): When you need to copy an entire directory—complete with all its subdirectories and files—the recursive flag is indispensable. It tells cp to traverse the directory tree, ensuring nothing is left behind.

  • Interactive Mode (-i): Safety first! The interactive option prompts you before replacing an existing file. This extra step is critical when you’re working with important data, as it minimizes the risk of accidental overwrites.

  • Force Copy (-f): Sometimes you need to override warnings and ensure the file is copied no matter what. The force flag does just that, replacing existing files without a prompt. Use this with caution.

  • Preserve Attributes (-p): File integrity matters, especially when dealing with permissions, timestamps, and ownership information. The preserve flag ensures that the new copy retains all of these attributes, making it perfect for backups or sensitive system files.

  • Verbose Output (-v): For a detailed view of what’s happening during the copy process, the verbose option prints each step to the terminal. This can be particularly helpful when copying large sets of files or debugging complex operations.

Practical Examples: Copying Files

Let’s now dive into some practical examples to see how these options come together in everyday tasks.

Copying a Single File

Imagine you have a file named notes.txt and you want to create a backup copy in the same directory. You can simply run:

cp notes.txt notes_backup.txt

This command creates an exact duplicate named notes_backup.txt. However, if a file by that name already exists and you want to avoid overwriting it without confirmation, you can use:

cp -i notes.txt notes_backup.txt

The -i flag ensures that you’re asked before any overwriting takes place.

Transferring Files Between Folders

If your goal is to move a file from one location to another, specify the destination directory. For instance, to move report.pdf to a directory called archive, use:

cp report.pdf /home/username/archive/

Make sure that the destination directory already exists; cp will not create it for you. If it doesn’t, you can create it with the mkdir command beforehand.

Copying Multiple Files at Once

Sometimes, you might need to duplicate several files simultaneously. To copy file1.txt, file2.txt, and file3.txt into a directory named backup, you would type:

cp file1.txt file2.txt file3.txt /home/username/backup/

This command handles multiple files in one go. If you’re dealing with many files that share a common pattern—say, all log files—you can use a wildcard:

cp *.log /home/username/logs/

This instructs cp to copy every file ending with .log into the logs directory, streamlining the process when working with numerous files.

Mastering Recursive Copying for Directories

Often, the task isn’t limited to a single file but involves entire directories. Copying directories requires a recursive approach to capture every nested file and folder.

Recursively Duplicating a Directory

Suppose you want to duplicate a website’s content located in /var/www/html to create a backup. The command would be:

cp -r /var/www/html /backup/html_backup

Here, the -r flag tells cp to copy everything within /var/www/html—subdirectories, hidden files, and all.

Combining Recursive and Preserve Options

When backing up directories, it’s often crucial to maintain file permissions, timestamps, and other metadata. In such cases, combine the recursive flag with the preserve flag:

cp -rp /var/www/html /backup/html_backup

This command ensures that every file in /var/www/html is copied to /backup/html_backup with all its original attributes intact. It’s an ideal solution for sensitive data or system configurations.

Tips, Tricks, and Advanced Techniques

Now that you understand the basics, let’s explore some advanced strategies and best practices for using the cp command effectively.

Combine Options for Enhanced Safety

It’s common to use multiple options together to tailor the behavior of cp. For instance, to safely copy a directory while preserving file attributes and prompting for overwrites, you can use:

cp -rpi /data/source_directory /data/destination_directory

This powerful combination ensures a thorough and secure copy process.

Handling File Names with Special Characters

File names in Linux may include spaces or special characters. To ensure these names are handled correctly, enclose them in quotes. For example:

cp "My Important Document.txt" "My Important Document Copy.txt"

This prevents the shell from misinterpreting spaces as delimiters between different arguments.

Avoiding Unintentional Overwrites

For batch operations or automated scripts, you might want to ensure that existing files are never overwritten. The -n option (short for no-clobber) achieves this:

cp -n *.conf /backup/configs/

This command copies configuration files only if a file with the same name doesn’t already exist in the destination, adding an extra layer of safety.

Use Verbose Mode for Debugging

When dealing with a large volume of files or troubleshooting a copy operation, the verbose flag (-v) can be immensely helpful:

cp -rv /source/folder /destination/folder

Verbose mode prints every file as it is processed, giving you a clear view of the ongoing operation and making it easier to identify any issues.

Real-World Applications and Scenarios

The cp command isn’t just for occasional use—it’s a vital tool in many professional settings. Here are a few real-world scenarios where mastering cp can make a significant difference:

System Administration and Backups

System administrators often use cp to create backups before making critical changes to system configurations. For instance:

cp -rp /etc /backup/etc_backup

This command creates a comprehensive backup of the /etc directory, preserving all system settings and permissions. In the event of an error or system failure, such backups are indispensable.

Data Migration and Server Transfers

When moving data between servers or different parts of a network, cp helps ensure that all files are transferred accurately. Combining cp with other tools like rsync can create robust solutions for data migration.

Development and Testing

Developers frequently duplicate directories to create test environments or sandbox copies of their projects. Whether you’re testing a new feature or debugging an issue, copying the entire project directory with preserved attributes can save you time and prevent potential errors.

Best Practices for Using cp Effectively

To wrap up, here are some key recommendations to keep in mind when using the cp command:

  • Double-check Destination Paths: Always verify that the target directory exists to avoid errors during the copy process.
  • Use Interactive Mode for Critical Files: When working with important data, the -i flag can prevent unintentional overwrites by asking for confirmation.
  • Quote File Names with Spaces: Ensure that any file names containing spaces or special characters are enclosed in quotes.
  • Plan Your Backup Strategy: Regularly back up essential directories using recursive and preserve options to maintain data integrity.
  • Combine Options Thoughtfully: Mix and match flags such as -r, -p, and -v to tailor cp to your specific needs, ensuring safety and clarity in your file operations.

Final Thoughts

The Linux cp command is a cornerstone of effective file management. Its simplicity belies the powerful functionality hidden within its many options. By mastering cp, you not only streamline your workflow but also protect your data through careful handling of file attributes, recursive copying, and thoughtful automation.

Whether you’re a novice stepping into the Linux world or an experienced user looking to refine your skills, the techniques and examples provided in this guide will serve as a reliable reference for your file duplication tasks. Remember to consult the manual page (man cp) for additional details and advanced options.

Embrace the versatility of the cp command, and soon you’ll find that managing files and directories on Linux becomes second nature.

Linux
07.02.2025
Reading time: 8 min

Similar

Linux

How to Compress Files in Linux Using tar Command

The tar command basically functions to put together all files and directories into one archive without altering their structure. The approach simplifies organization, creation of the backup, and the transfer of files. Once packaged, you can compress these archives by using multiple ways such as using gzip, bzip2, or xz, which help optimize storage and enhance transfer speeds. Modern Linux distributions come with updated versions of tar, enabling seamless integration with compression tools like gzip for more efficient data handling. This makes tar a valuable asset for users managing large datasets, as it supports both file consolidation and compression in a single command. Thanks to its flexibility, tar is widely used across different Linux environments. It not only facilitates backup creation but also streamlines software distribution and the management of the important data. With an array of choices available, all users can customize archives according to their requirements, whether by excluding particular directories or files, preserving permissions, or securing sensitive data. For anyone dealing with extensive information or complex storage requirements, learning everything about the tar command is crucial. This all makes it an important utility to learn for Linux users. Understand the Syntax of tar  The tar command is fundamentally divided into four distinct parts: tar (keyword) -flags (options), used to execute a specific action name of the archive path to the desired file or directory It would be written as follows: tar -flags (archive_name) (path) Archiving Files and Directories tar used with the flag -cvf has the power to essentially archive the files and also the directories. For a File: tar -cvf collectionX.tar snake.txt For a Directory: tar -cvf DRcollection.tar newDir/ This would essentially archive the file snake.txt to collectionX.tar and the directory newDir to DRcollection.tar respectively.  If desired outcome is to archive multiple files and directories, then use the following commands.For Multiple Files: tar -cvf collectionX.tar snake.txt panther.txt Tiger.txt For Multiple Directories: tar -cvf DRcollection.tar newDir1/ newDir2/ newDir3/ Compressing Files and Directories tar used with the flag -czvf has the power to compress the files as well as the directories: For a File: tar -czvf collectionX.tar.gz snake.txt For a Directory:  tar -czvf DRcollection.tar.gz newDir/ -c archives the directories and files, -z pushes for gzip compression, -v is verbose which essentially shows what’s going on with compression, and -f allows to name the archive that is going to be compressed. Add .gz after tar, if you want to compress the files and directories. For Multiple Files: tar -cvf collectionX.tar.gz snake.txt panther.txt Tiger.txt  For Multiple Directories: tar -cvf DRcollection.tar.gz newDir1/ newDir2/ newDir3/ .bz2 used with tar and both used with -cjf allow to archive and compress files and directories. -j applies bzip2 compression. For a File (with bz2): tar -cjf collectionX.tar.bz2 snake.txt For a Directory (with bz2): tar -cjf DRcollection.tar.bz2 newDir/ .xz used with .tar and both used with -cJf allow you to archive and compress files and directories. In -cJf, -J means compress with xz. For a File (with xz): tar -cJf DRcollection.tar.xz file1.txt For a Directory (with xz): tar -cJf collectionX.tar.xz newDir/ Extracting Compressed .tar Files arch1.tar.gz, arch1.tar.bz2 and arch1.tar.xz are three compressed files. Extract tar.gz: tar -xvzf arch1.tar.gz -x stands for file extraction. Extract tar.bz2: tar -xvjf arch1.tar.bz2 Extract tar.xz: tar -xvJf arch1.tar.xz Extracting Specific Files Using Wildcards If you need to extract only a specific type of file out of an archive, do this: tar -xvf arch1.tar --wildcards '*.sh' It will give you only the files with .sh extension. --wildcards help search that specific type of file and enable pattern matching while *.sh ensures that you only extract the .sh type of files. Extracting to a Specific Directory If you need to extract the complete archive to a specific directory, do this: tar -xvf arch1.tar -C ./destinationDir/pathDir/ -C changes to the specified directory path and -xvf helps extract the archive there.  Managing .tar Archives Check Contents without Extracting If you need to know what's inside an archive but don't want to uncompress files, use commands like this: tar -tzf arch1.tar.gztar -tjf arch1.tar.bz2tar -tJf arch1.tar.xz -t gives details about what’s inside the compressed archives without performing extraction on it. Appending Files to an Existing Archive To append a new file to an archive: tar -rvf arch1.tar new.sh new.sh will be added to arch1.tar. That’s how you append a file into an existing archive.  Removing a Specific File from an Archive What if you need to delete a file from an archive without having to extract it, it can be done by using --delete. tar --delete -f arch1.tar new.sh  This will remove the file new.sh from the archive arch1.tar without extracting it.  Note that --delete does not work on the compressed files, only on archives.  Comparing Archive Contents with Current Directory If you have to examine the contents of your current working directory and compare them with the archive? use: tar --diff -f arch1.tar --diff will help compare the contents of arch1.tar with the content available in the present working directory. Troubleshooting Common .tar Errors "tar: Removing leading '/' from member names" This warning appears when absolute paths are used in an archive: tar -cvf arch1.tar /home/user/file.txt Solution: Use -p to preserve absolute paths. tar -cvpf arch1.tar /home/user/file.txt "tar: Error opening archive: Unrecognized archive format" This error occurs when the archive is corrupt or the wrong decompression command is used. Solution: Verify the file type: file arch1.tar.gz Use the correct decompression command: tar -xvzf arch1.tar.gz  # For .tar.gztar -xvjf arch1.tar.bz2  # For .tar.bz2tar -xvJf arch1.tar.xz   # For .tar.xz If corruption is suspected, check integrity: gzip -t arch1.tar.gzbzip2 -tv arch1.tar.bz2 Conclusion The tar utility serves as an important tool for archiving, compression and extraction. It provides efficiency, making it a crucial component of Linux storage management. With a variety of configurations and settings, tar functions as an evergreen solution catering to diverse use scenarios. Options such as -czvf and -xvzf determine the way files are stored and retrieved, granting users complete control over data compression. Furthermore, tar supports multiple compression tools like gzip, bzip2, and xz, allowing users to optimize both speed and compression ratio based on their specific needs. For Information Technology professionals, developers, and Linux users, learning everything about tar is invaluable. Whether it’s for managing backups, distribution of data effectively, or optimizing storage, tar is by far one of the most influential archiving tools. By selecting the right configurations and commands, users can significantly enhance their workflow, automate tasks, and efficiently handle large datasets.
12 March 2025 · 6 min to read
Linux

How to Use the Screen Utility in Linux

The Screen utility is a Linux window manager that allows you to switch between multiple processes in a single physical terminal. Screen provides a scrollable history buffer and a mechanism for copying and pasting text between windows. With Screen, you can create new windows with different programs, close the current windows, view a list of active windows, enable and disable output logging, and switch between windows. All windows work independently, and programs continue to run even when the session is disconnected from the user's terminal. This makes Screen a useful tool for efficiently managing multiple tasks in a single terminal. Installing the Screen Utility The Screen may be pre-installed in the operating system or require separate installation depending on the distribution. To install Screen, use the following command: For Ubuntu and Debian: apt install -y screen For CentOS and Fedora: yum install -y screen Or: dnf install -y screen Basic Commands Let's go over the basic commands for managing Linux Screen sessions. Starting a Screen Session To start Screen, simply enter the following command in your terminal: screen This will open a Screen session, create a new window, start a shell in it, and you will see a new window. Press Enter to proceed to enter commands. Creating a Named Session You can name your sessions, which is especially useful when working with multiple Screen sessions. To create a named session, use the following command: screen -S session_name It’s always helpful to choose a descriptive name for the session. Detaching from a Screen Session in Linux To detach from a Screen session at any time, type: Ctrl+a d The program running in the Screen session will continue to run in the background after you detach. Reattaching to a Screen Session in Linux To resume your Screen session, use the command: screen -r If you have multiple Screen sessions running, you need to specify the session ID or its name after the -r parameter. To see the list of currently running sessions, use: screen -ls You will see a list of sessions like this: There are screens on: 1468393.hostman (01/25/2025 02:07:34 PM) (Detached) 1466624.pts-3.1495851-user (01/25/2025 01:54:05 PM) (Detached) 2 Sockets in /run/screens/S-linuxize. To resume the session with ID 1466624.pts-3.1495851-username, enter: screen -r 1466624 To resume a session using its name, type: screen -r session_name Additional Options Screen offers a variety of useful features for convenient session management in the terminal.  You can customize each window to suit your needs, such as adjusting its size according to display settings or configuring options using a custom configuration file.  You can also pause a session and resume it later or run Screen in daemon mode to keep it running in the background.  Additionally, you can customize command keys, manage data flow, and enable logging.  It's also possible to switch between windows, change their titles, and use UTF-8 encoding, making terminal work more comfortable and adaptable to different tasks. Screen options: -a: Enables all possible features for each window, maximizing functionality. -A -[r|R]: Automatically adjusts all windows to fit the new screen width and height. -c file: Specifies an alternate configuration file instead of the default .screenrc. -d (-r): Detaches the current Screen session without terminating it so that you can reconnect later. -D (-r): Terminates the active connection to a remote session, but the session itself remains running and can be resumed. -D -RR: Takes all necessary actions to reconnect to an existing Screen session if one is available. If not, it starts a new one. -e xy: Changes the default keybindings for Screen commands to custom ones, which is useful for avoiding conflicts with other programs. -f: Enables data flow control; -fn disables it; -fa enables automatic flow control, which is helpful when working with large amounts of data. -h lines: Sets the scrollback buffer size, allowing you to scroll through more command history. -i: Interrupts data output on the screen when flow control is enabled, preventing terminal overload. -l: Logs session information to the system log to keep track of active sessions; -ln disables this. -ls [pattern]: Displays a list of all active Screen sessions currently connected. -L: Enables logging of all terminal output to a log file. -p window: Automatically selects the specified window on startup if it exists. -q: Runs Screen in “quiet” mode, suppressing unnecessary error messages. -V: Displays the version of Screen and then exits. -r [session]: Reconnects to a previously started but detached Screen session. -R: If an existing session is found, it reconnects to it; if not, it starts a new one. -S session_name: Assigns a name to the new session, making it easier to reconnect to it later. -t title: Sets a title for the window, which is displayed in the window list. -U: Enables UTF-8 encoding support for text display. -v: Displays the current version of the Screen program. -x: Attaches to an active session, allowing it to be used simultaneously on multiple screens. -X: Executes the specified command within an active Screen session. Working with Screen Windows You can work with multiple Screen sessions simultaneously, with several windows open for each session. To create a new window with a shell, press: Ctrl+a c The window will be automatically assigned a number from 0 to 9. Below are the common commands for managing windows in Screen: Ctrl+a c — Create a new window (with a shell). Ctrl+a " — Display a list of all windows. Ctrl+a 0 — Switch to window 0 (by number). Ctrl+a A — Rename the current window. Ctrl+a S — Split the current region horizontally into two regions. Ctrl+a | — Split the current region vertically into two regions. Ctrl+a tab — Move the input focus to the next region. Ctrl+a Ctrl+a — Toggle between the current and previous windows. Ctrl+a Q — Close all regions except the current one. Ctrl+a X — Close the current region. To see all commands, enter: Ctrl+a ? When Screen starts, it reads its configuration settings from /etc/screenrc and ~/.screenrc, if they exist. You can customize Screen's default settings to suit your preferences using the .screenrc file. This example includes a custom status line and several additional options: # Disable the startup message startup_message off # Automatically detach from the session when the connection is lost autodetach on # Set the scrollback buffer to 10,000 lines defscrollback 10000 # Enable logging for the current session logfile /path/to/screenlog Additional recommendations for customizing Screen configuration: Automatic Window Splitting on Startup Useful if you frequently work with multiple windows and want them to open automatically when Screen starts. screen -t shell1 split focus screen -t shell2 Logging All Sessions Useful for keeping a record of work. deflog on logfile $HOME/.screen/screenlog.%t Automatic Reconnection on Disconnection Useful when working with unstable connections. autodetach on reattach on Examples of Using Screen Example 1: To monitor file changes in real-time, you can use two Screen windows: one for editing the file and another for displaying its content using the command: tail -f filename This allows you to instantly see all changes made without having to re-run the command. Example 2: When working in a terminal over SSH using a Screen session, you won’t lose data if the connection is interrupted. Even if the connection drops, you can reconnect and resume work exactly where you left off by simply reattaching to the Screen session. Example 3: For long-running tasks, such as compiling code or performing a backup, you can start the task in one Screen session and monitor its progress. You can safely disconnect anytime, knowing the task will continue running. Later, you can reconnect to the session to check the results. Conclusion In this guide, we covered how to use Linux Screen to manage terminal sessions effectively. You learned how to: Create multiple windows within a single session. Switch between windows. Manage sessions, including detaching and resuming them. We also discussed how to customize the terminal using the .screenrc configuration file to make your work environment more convenient and personalized. You can now use Screen for a more comfortable and productive terminal experience by mastering these basic features. For more information about Screen, check out the Screen user's manual.
20 February 2025 · 7 min to read
Linux

How to Use if-else in Bash

Many programming languages have conditional statements, such as if-else. These statements are also present in Bash, the default shell used in almost all modern Linux distributions. The if-else statements are used to check conditions — they allow the execution of specific commands depending on whether the condition is true or false. The if-else statements work exactly the same way as in any programming language. In this article, we will discuss how to use if-else statements in the Bash shell through practical examples. The if Statement in Bash The if statement in Bash allows you to execute specific commands depending on the truth value of the given condition. Two logical statements are used to check for truth: True and False. The if statement is used when you need to check a condition. It controls the flow of script execution, allowing decisions to be made based on variable values, command results, and other conditions. The if statement works as follows: First, the program checks the condition (the condition can be a command or a mathematical expression) contained in the if statement. If the condition is true, the program executes the commands listed after the then keyword. If the condition is false, the program executes the commands listed after the else statement. The syntax of the if statement in Bash is as follows: if [condition]; then # commands to execute if the condition is true fi Let's break down the operation of the if statement with a simple practical example. We will create a script that asks the user for a number, and if the number entered is greater than 10, the system will return the message "The number is greater than 10." Create a new file with a .sh extension, for example, using the nano editor: nano greater_than_10.sh Insert the following code: #!/bin/bash read -p "Enter a number: " number if [ $number -gt 10 ]; then echo "The number is greater than 10." fi Provide the file with execute permissions: chmod +x greater_than_10.sh Now, run the script: ./greater_than_10.sh Output: Enter a number: Enter any number, for example, 32, and press Enter. Since 32 is greater than 10, and this condition returns True, the program will execute the echo command. Enter a number: 32The number is greater than 10 Let’s break down the script in more detail: The conditions are written in square brackets. In this example, the -gt operator is used (greater than, equivalent to the > symbol). Next, we check the condition. If it’s True, the program executes the command after the then keyword. The script ends with the fi keyword, signaling the end of the if block. However, this script has one major drawback: it does not handle the case when the entered number is less than 10. The script will not return anything because there is no condition for that case. To address this issue, we will use the else statement, which we will discuss in the next chapter. The if-else Statement in Bash In the previous section, we ran a script with only one condition in the if statement — True. We didn’t specify any action for the False condition. As a result, if we entered a value leading to False, there was no response. If we want the script to perform specific actions for the false condition False, we need to use the else statement, which follows the if statement. The if-else statement in Bash is used to perform conditional operations. It allows the execution of specific commands depending on whether the condition is true or false. The syntax for if-else is as follows: if [condition]; then # commands executed if the condition is true else # commands executed if the condition is false fi Remember that keywords, including if and else, in Bash shell scripts are case-sensitive. Be careful when using keywords in script files. Let's consider using the if-else statements in a practical example. In this case, we will create a Bash script that asks the user for a number, and the system will display whether the number is greater than or less than 10. Create a new file with a .sh extension: nano check.sh Insert the following code: #!/bin/bash read -p "Enter a number: " number if [ $number -gt 10 ]; then echo "The number is greater than 10." else echo "The number is less than or equal to 10." fi Grant the file execute permissions: chmod +x check.sh Now, run the script: ./check.sh The algorithm for the script works as follows: After the if keyword, we specify the condition in square brackets. In this example, we use the -gt operator (greater than, equivalent to the > symbol). The condition is checked. If the condition is true, the program executes the command after the then keyword— in this case, it prints the message "The number is greater than 10". If the condition is false, the program executes the command after the else keyword, printing the message "The number is less than or equal to 10". Once one of the conditions is met, the program will end, as indicated by the fi keyword at the end. Output if the number is greater than 10: Enter a number: 56The number is greater than 10. Output if the number is less than 10: Enter a number: 6The number is less than or equal to 10. Practical Use of if-else in Bash Let's look at the practical application of the if-else statement in Bash, which can be used when writing scripts. Script Example 1. Checking if Run as root First, we will create a script that checks whether the script file is run as the root user. This can be useful when writing scripts that require root privileges, such as installing packages as the root user. Create a file named check-for-root.sh: nano check-for-root.sh Use the following code to check for root user: #!/bin/bash if [[ $EUID -ne 0 ]]; then /usr/bin/printf "${R}>>>>${NC} Please run as root\n" exit 1 fi Grant the file execute permissions: chmod +x check-for-root.sh And run it: ./check-for-root.sh If the script is run as a regular user, the console will print the message "Please run as root". The check for the root user uses the condition $EUID -ne 0, where: $EUID is an environment variable that holds the numeric user ID. In Linux systems, the root user always has the ID 0, while other user accounts start at 1000. -ne is a comparison operator meaning "not equal". Instead of ne, you can also use !=. Script Example 2. Checking the Linux distribution Next, let's create another script that checks which Linux distribution is being used. If the script is run on Ubuntu, it will print the message "This is Ubuntu". If the script is run on any other Linux distribution, it will print "Not Ubuntu. You can run this script only on Ubuntu distributions". Create a file named check-for-distribution.sh: nano check-for-distribution.sh Use the following code: #!/bin/bash dist=`grep DISTRIB_ID /etc/*-release | awk -F '=' '{print $2}'` if [ "$dist" == "Ubuntu" ]; then echo "This is Ubuntu" else echo "Not Ubuntu. You can run this script only on Ubuntu distributions" fi Make the file executable: chmod +x check-for-distribution.sh And run it: ./check-for-distribution.sh If the script is run on an Ubuntu system, it will print "This is Ubuntu". On any other distribution, it will print "Not Ubuntu. You can run this script only on Ubuntu distributions". Script Example 3. Checking if File Exists Now, let’s look at another practical example. We will create a Bash script that checks if a file named file1.txt exists. If it doesn't exist, the script will create it. The script checks for the file in the same directory it is run. If the file already exists, the script will print a message without creating the file. Create a file named check-file.sh: nano check-file.sh Use the following script code: #!/bin/bash FILE="file1.txt" if [ ! -f "$FILE" ]; then touch "$FILE" echo "$FILE has been created." else echo "$FILE already exists." fi Grant execute permissions for the script: chmod +x check-file.sh Run the script: ./check-file.sh If the file1.txt file already exists in the directory from which the script is run, you will see the message "file1.txt already exists.". The file will not be created. Conclusion In this article, we reviewed the principles of logical statements such as if-else in the Bash shell and provided practical examples of using these statements. These examples are useful when writing scripts to automate system tasks or checks.
18 February 2025 · 7 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support