The ls Command in Linux

The ls Command in Linux
Hostman Team
Technical writer
Linux
09.07.2024
Reading time: 8 min

The ls command is essential for navigating the Linux or UNIX file system. To understand how to use it effectively, you need to be familiar with basic navigation in the Linux OS and the additional options available.

Navigation in Linux

The file system of Linux distributions is structured like a tree. At the base is the root directory, denoted by a slash (/), which contains all the information in the OS. From the root directory, "branches" or other directories spread out.

You can specify a path to a folder using either a full (absolute) or relative path.

  • A full (absolute) path specifies the location of files and directories starting from the root directory. This path always starts with a slash. Example of a full path:

/home/hostman/file1/
  • A relative path specifies the location of objects relative to the current position (current directory). In the beginning of the address line, you might see:

    • ~ — home directory;

    • . — indicates the current directory;

    • .. — indicates the parent directory.

Description of the ls Command

The Linux ls command displays information about directories and files located in folders. The command name comes from the word "list." By specifying additional options, you can customize the output format as needed.

Syntax of the command:

ls [OPTIONS][FILE|FOLDER]

In place of [OPTIONS], you specify the desired option and then refer to a file [FILE] or directory [FOLDER]. If you do not specify a directory name, you will get a list of the contents of the directory you are currently working in by default.

In some cases, there is no need to enter the folder itself. You can use a simplified version by specifying the path in the context of the ls command:

ls /PATH/TO/FILE

Methods for specifying the path are described above.

Options

  • -a – display all contents of the directory, including hidden files (their names start with a dot).

  • -A – display all files except hidden ones.

  • --author – display the author information.

  • -b – output the file name, even if it contains special characters that are not visible when printed.

  • -B – do not display backup copies. Backup copies are recognized by the tilde ~ at the end of the name.

  • -c – display files sorted by the time of the last modification. By default, new files are listed first.

  • -C – display files in columns.

  • -d – display information about the directory without showing its contents.

  • -f – disable sorting.

  • -F – enable visibility of the object type. The object type can be identified by the assigned symbol at the file name's end.

  • --full-time – display complete information, including time in ISO format.

  • -i – display the inode where the file is located.

  • -l – display a long list with detailed information.

  • -g – similar to the -l option, but without displaying the owner's name.

  • -m – separate list elements with a comma.

  • -1 (one) – display information about only one object per line.

  • -n – output objects' names without quotes.

  • -h – convert file size values to the required format. By default, file sizes are displayed in bytes without specifying the unit of measurement.

  • --color – use or disable coloring of objects when displayed on the screen. You can set variables to three values, one of which is automatic color output:

--color=auto/always/never
  • -R – display a list of subdirectories recursively.

  • -S – display sorted files by size, from largest to smallest.

  • -Sr – opposite of the previous command: files are displayed from smallest to largest.

  • -u – sort objects by the time of the last access.

  • -p – if the object is a directory, a slash is displayed at the end of the name.

  • -q – a question mark if NPC control characters are in the file name.

  • -T – set the tab width. By default, it is 8.

  • -w – set the column width.

  • -v – sort by file version number.

Safe and scalable Virtual Servers and VPC

Using Multiple Options Simultaneously

The ls -l command in Linux allows you to display a long list of all folders. It is often combined with other options to show only selective information.

  • ls -lr – display a list of all elements sorted in reverse order;

  • ls -lt – display information according to the last modification date;

  • ls -l --author – display the document's creator.

In this way, you can combine most options.

In addition to basic files, the ls- la command displays hidden documents starting with a dot in the name.

Sorting Files

There is a sorting switch that allows you to quickly sort the output by the desired parameter, such as size, time, or version.

The syntax of the command is as follows:

ls -l --sort=WORD/-x

After the equals sign, you enter the parameters: size, time, version, extension. You can also enter only the first letter instead of the whole word.

Setting the Output Format for Files

When using the ls on Linux, files are arranged in columns by default. To display them vertically, horizontally, or list them with a comma, use the command:

--format=WORD/-x

(The function is set as a whole word or symbol)

  • Across/-x – arrange files alphabetically by column.

  • Horizontal/-x – sort output horizontally.

  • Vertical/-C – sort output vertically.

  • Commas/-m – list directory contents separated by a comma.

  • Long/-l – output detailed information about files in a long list.

  • Single-column/-1 – arrange all files in one column.

Output Format for File Sizes

To view file sizes in a specific unit of measurement, use the option:

--block-size=SIZE

After the equals sign, specify the first letter of the unit of measurement (K, M, G, etc.). In this case, the multiplier is 1024. If you specify kilobytes or megabytes, the multiplier is 1000. For this, you can use the separate option --si.

Examples of Using the ls Command

Let's see how the Linux ls command is used in practice.

  1. Open a terminal window.

  2. Run the ls command and specify the path if needed.

To navigate directories, use the cd (change directory) command using the syntax:

cd [location]

Run the command with arguments; otherwise, it will return to the home directory.

Use the pwd (Print Working Directory) command to find out the working directory. This command does not use arguments.

Now you can start testing the options you are interested in. Below are some examples of what the inputs should look like and what the output information should be.

First, enter the command in the terminal window without using arguments. In this case, you will get a list of directories and files in the usual format.

ls

Output:

Videos   file.html lib    Downloads
File7   Pictures   Dir
Documents   020.Pcap   Public

Display files in reverse order:

ls -r

Output:

Public 020.Pcap Documents
Dir Pictures File7
Downloads lib file.html Videos

Next, to display a long list, use the -l option:

ls -l

Output:

drwxr-xr-x. 3 root root 1785 Jun 29 10:11 Videos
-rw-r--r--. 2 root root 989 Aug 10 12:38 file.html
-rw-r--r--. 2 root root 989 Aug 10 12:38 lib
drwxr-xr-x. 4 root root 1580 Jul 16 01:20 Downloads
-rw-r--r--. 1 root root 3948 Aug 09 03:01 File7
drwxr-xr-x. 3 root root 5170 May 28 13:40 Pictures
drwxr-xr-x. 4 root root 3580 Jun 14 17:45 Dir
drwxr-xr-x. 1 root root 28320 Jul 25 10:11 Documents
-rw-r--r--. 2 root root 1444 May 27 17:45 020.Pcap
drwxr-xr-x. 1 root root 32150 Jun 10 09:58 Public

Now let's interpret what was displayed on the screen using the long list option.

  1. Column 1: The type of file. If the line starts with a hyphen, it is a regular file. If the line begins with the letter d, it is a directory.

  2. Column 2: The next 9 letters and symbols represent access rights to these elements in the file system. The letter r gives the right to read the file, x the right to write to the file, xr the right to execute the file.

  3. Column 3: The number indicates how many hard links point to this file.

  4. Column 4: Displays the creator of the object and the file group.

  5. Column 5: The file size.

  6. Column 6: Time data when the last changes were made.

  7. Column 7: The name of the element for which information is displayed.

Let's work with this list. Now display hidden files using the following option:

ls -a

Output:

.opera  .gconf   .  Videos  file.html
.libreoffice lib Downloads .cshrc File7 ..
Pictures  .pki Dir 020.Pcap   Public

Sort files by last modification date:

ls -lt

Output:

-rw-r--r--. 2 root root 1444 May 27 17:45 020.Pcap
drwxr-xr-x. 3 root root 5170 May 28 13:40 Pictures
drwxr-xr-x. 1 root root 32150 Jun 10 09:58 Public
drwxr-xr-x. 4 root root 3580 Jun 14 17:45 Dir
drwxr-xr-x. 3 root root 1785 Jun 29 10:11 Videos
drwxr-xr-x. 4 root root 1580 Jul 16 01:20 Downloads
-rw-r--r--. 2 root root 989 Aug 10 12:38 file.html
drwxr-xr-x. 1 root root 28320 Jul 25 10:11 Documents
-rw-r--r--. 1 root root 3948 Aug 09 03:01 File7
-rw-r--r--. 2 root root 989 Aug 10 12:38 lib

Test the -F option to find out which objects are directories:

ls -F

Output:

Videos/   file.html    Downloads/
File7     Pictures/    Dir/
Documents/020.Pcap    Public/

Conclusion

We reviewed a basic usage of the ls command in Linux for working with the file system. This tool is available in all operating system distributions and is much more efficient than viewing document properties through a graphical user interface.

You can try our reliable Linux VPS hosting for your projects. 

Linux
09.07.2024
Reading time: 8 min

Similar

Linux

How To Use SSHFS to Mount Remote File Systems Over SSH

SSHFS is a Linux tool for mounting remote folders over SSH. It allows users to manage network-shared files just like local ones. This tool is secure and efficient, providing seamless management of network shared folders across different environments. Required Setup Before you start, ensure you have: Root or sudo permissions. An external server with SSH enabled. An SSH service working on your local machine. Using SSHFS Step 1: Install SSHFS First, install SSHFS on your local system through the package manager. This tool installation on the other system is not needed. On Ubuntu/Debian: sudo apt install sshfs On CentOS/RHEL: sudo yum install sshfs Step 2: Create a Connection Point Set up a folder in your home or any desired location. This will act as the connection point for the network shared directory. sudo mkdir remote_dir Step 3: Attach a Directory Attach the linked folder to the local computer for seamless access. Use the below-given command to perform remote filesystem mounting: sudo sshfs -o [options] user@host:/remote_path /local_mount Substitute user with your real remote server’s username, host with the IP address or hostname of the server, and /remote_path with the directory path you want to connect. The [options] include: allow_other: Grants access to other local machine users for accessing the mounted folder. reconnect: Automatically re-establishes the connection in case it drops. IdentityFile=/loc/of/private_key: Specify the location where SSH private key is stored. idmap=user: Aligns the ID of remote user to the local user ID. default_permissions: Applies the remote file system's default permissions. To connect the linux home folder from 192.X.X.X to /home/ubuntu/remote_dir, utilize: sudo sshfs -o allow_other,default_permissions [email protected]:/home/linux/ /home/ubuntu/remote_dir/ To employ an SSH key found at /home/ubuntu/.ssh/id_rsa, use: sudo sshfs -o allow_other,default_permissions,IdentityFile=/home/ubuntu/.ssh/id_rsa [email protected]:/home/linux/ /home/ubuntu/remote_dir/ Type 'yes' to accept the server’s fingerprint and add it to known hosts. Enter the password for authentication. Use the key if set up. After verification, the folder will be linked to the local path. Step 4: Verification Create a new folder or file in the attached directory and verify its existence on the external server. If the folder or file appears in the external server's directory, the operation is successful. This ensures changes in your local directory are mirrored on the external system. If you experience the "Permission denied" error when trying to create or modify an existing file, follow these instructions to resolve it: Run the ls -l command to view the current permission of files or directory. Execute the chmod command to modify the permissions. sudo chmod 644 /path/to/file_or_directory If the file or directory is owned by another person, run the chown command to change the ownership. sudo chown your_username  /path/to/file_or_directory Step 5: Unmounting Once finished, simply unmount the folder: sudo umount /remote_directory Additional Options Below are some additional things you can also do:  Auto-Mounting at Boot To automatically connect remote filesystem at startup, utilize these steps: Step 1: Edit fstab Access the /etc/fstab file with elevated privileges: sudo nano /etc/fstab Step 2: Add Entry Append an entry to the end of the file: user@remote_host:/remote/directory /local/mount/point fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,allow_other,reconnect 0 0 Example: [email protected]:/home/linux/ /home/ubuntu/remote_dir fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,allow_other,reconnect 0 0 Where: noauto: Automatically stops the mount from happening at boot. x-systemd.automount: Uses systemd to dynamically connect the filesystem upon access. _netdev: Indicates that network access is required for the process. users: Grant non-root users the ability to mount and unmount. idmap=user: Associates external user with local one. allow_other: Permits another person from retrieving the connected directory. reconnect: Ensures automatic reconnection in case connection drops. Step 3: Create a Connection Point Make sure the local mount point directory exists, and if not, create it: sudo mkdir -p /home/ubuntu/remote_dir Step 4: Testing Test the connectivity: sudo mount -a This command initiates the connection of all filesystems listed in /etc/fstab. If no errors arise, the process is successful. Utilizing SSHFS Without SFTP SSHFS usually utilizes SFTP for transferring. To bypass this, run: sshfs -o sftp_server=/usr/lib/openssh/sftp-server user@host:/remote_directory ~/remote_mount Configuration File To save commonly used options, create a .sshfs_config file in your home location. This will allow you to store and easily apply your preferred settings. nano ~/.sshfs_config Add your options and connect via the configuration file. sshfs -F ~/.sshfs_config username@remote_host:/remote/directory ~/remote_mount Resolving Typical Problems Below are some common problems and solutions. Connectivity Problems To ensure seamless connectivity, make certain that SSH service is configured in the correct way on both your local and external systems. Also, check that the service port is open and that your firewall settings allow access, which is crucial for maintaining an uninterrupted connection. Performance Issues For better performance, use the -o direct_io and -o cache=yes options. sshfs -o direct_io -o cache=yes user@host:/remote_directory ~/remote_mount Connection Reset by Peer Cause: The external SSH server may be down, or there could be network instability. Solution: Verify that the SSH server is operational on the external machine. Ensure a stable network connection for consistent communication. Permission Denied Cause: The user lacks the required permissions to access the network-shared folder. Solution: Confirm that you have the correct permissions. Proper access rights are essential for successful connection. Running SSHFS on Windows To utilize SSHFS for Windows, follow these instructions: Download and set up SSHFS-Win from this location. Right-click on This PC and go with the option Map network drive from the context menu: Choose an available drive letter from the menu. In the Folder field, input the command as follows: \\sshfs\user@host\remote_path Click Finish to complete the mapping process. Enter your credentials and provide the required username and password (or SSH key, if configured). Once connected, access the directory via Windows Explorer. Here are the additional options for the sshfs command based on different used cases: sshfs: Integrates the remote home directory locally. sshfs.r: Links to the remote server's root directory. sshfs.k: Uses [local-user]/.ssh/id_rsa to map the remote home directory. sshfs.kr: Utilizes a locally stored SSH key to access the root directory. When finished, right-click the network drive and choose Disconnect to detach the directory. Conclusion SSHFS provides an efficient and secure method for mounting remote file systems through SSH. This guide helps you set up and use this tool to improve file management on Linux systems. Whether performing the SSHFS mount as root, avoiding SFTP, or utilizing configuration files, this tool offers flexibility and control for various scenarios.
24 December 2024 · 6 min to read
Linux

How to Use the find Command in Linux

One of the most effective tools for locating files and directories according to a number of criteria is the Linux find command. Learning how to use this tool can save a lot of time and effort, whether you're a system administrator resolving file system problems or a casual Linux user attempting to search for lost files. You will learn all about find in this tutorial, from its fundamental syntax to its extensive application cases. By the end, you'll be able to use this tool effectively and confidently. Why Use the find Command? Utilizing a number of characteristics, such as permissions, modification date, size, kind, and name, the find program assists you in locating files and folders. It provides unprecedented control and accuracy in contrast to graphical search tools. Here’s what makes it special: Works recursively within directories. Supports complex filtering options. Executes actions on found files (like deleting, moving, or editing them). Handles large datasets efficiently. Let’s dive into its practical applications. Basics of find This is how the general syntax for find looks: find [starting_point] [expression] [starting_point]: The directory where the search starts. Use . to represent the current directory, / for the entire filesystem, or specify a particular path. [expression]: Defines what to search for. This can include file names, types, permissions, sizes, and other attributes. A Simple Example To find a file named notes.txt in your current directory and its subdirectories, run: find . -name "notes.txt" Let’s break this down: .: Search starts within the current directory. -name: Search based on file name. "notes.txt": The target file. Searching by File Name You can search for files using -name or -iname (case-insensitive). Case-Sensitive Search find /home -name "project.txt" Case-Insensitive Search find /home -iname "project.txt" Partial Matches Use wildcards (*) to find files containing specific text: find /var/log -name "*.log" This command locates all .log files in /var/log. Exploring File Types Linux treats everything as a file, but find lets you filter by type using the -type option: Regular Files: -type f Directories: -type d Symbolic Links: -type l Sockets: -type s Character Devices: -type c Block Devices: -type b Example: Finding Directories Only find /etc -type d Filtering by Time Your search can be narrowed down based on when files were accessed, modified, or created. Modified Time (-mtime) Identify which files were changed in the past 7 days: find . -mtime -7 Files modified exactly 7 days ago: find . -mtime 7 Access Time (-atime) Locate files accessed in the last 3 days: find /tmp -atime -3 Change Time (-ctime)  Use -ctime to find files whose metadata changed, such as permissions or ownership. find /var/www -ctime 5 Searching by Size The -size flag allows you to search for files of a specific size. Units of size: k: Kilobytes M: Megabytes G: Gigabytes Exact Size Find files that are 1 MB: find /var -size 1M Greater or Smaller Files Larger than 100MB: find /home -size +100M Smaller than 500KB: find /data -size -500k Combining Conditions Utilize logical operators to combine several search criteria: AND (-a): Default behavior. OR (-o): Specify explicitly. NOT (!): Exclude matches. To find .txt files larger than 1 MB: find . -name "*.txt" -a -size +1M To exclude directories named backup: find /data -type d ! -name "backup" Taking Action on Located Files Find can do more than just search; it can also apply operations on the found files. To do this, use the -exec or -ok flags. Delete Files find /tmp -name "*.tmp" -exec rm {} \; {}: Placeholder for the located file. \;: End of the -exec command. For safer deletion, prompt for confirmation: find /tmp -name "*.tmp" -ok rm {} \; Move or Copy Files find /home/user/docs -name "*.pdf" -exec mv {} /home/user/backup/ \; Run Custom Commands  You can run virtually any command on the located files. For instance, compress all .log files: find /var/log -name "*.log" -exec gzip {} \; Working with Permissions Use these options to search files by ownership or permissions: File Permissions (-perm) Find files with exact permissions: find /etc -perm 644 Locate files writable by others: find /data -perm -o=w User and Group Ownership Files owned by root: find / -user root Files owned by group admin: find / -group admin Avoiding Errors and Boosting Efficiency Ignoring Errors If you lack permissions for certain directories, suppress errors using 2>/dev/null: find / -name "config.yaml" 2>/dev/null Optimizing Searches To improve performance, limit your search depth using -maxdepth: find . -maxdepth 2 -name "*.sh" Use -mindepth to start searching from a certain depth. find . -mindepth 2 -name "*.txt" Real-World Use Cases Housekeeping Logs find /var/log -name "*.log" -mtime +30 -exec rm {} \; Archiving Old Files Move files unused for over a year to an archive directory: find /projects -atime +365 -exec mv {} /archive/ \; Security Audits Find world-writable files, which may pose security risks: find / -perm -o=w Backup Automation Copy all .docx files to a backup directory: find /documents -name "*.docx" -exec cp {} /backup/ \; Combining find with Logical Operators Logical operators such as -and, -or, and -not can be used to build more complex search expressions. These operators allow to search for files that fulfill various conditions concurrently. Find Files with Specific Extensions find . \( -name "*.jpg" -or -name "*.png" \) This searches for files that are either .jpg or .png. Exclude Certain Files find /var/log -type f -not -name "*.gz" This excludes .gz files from the search results. Find Files Modified in the Last Week and Owned by a User find . -mtime -7 -and -user alice Using find for Security Audits The find command can be a valuable tool for identifying security vulnerabilities, such as world-writable files or files with unsafe permissions. Find World-Writable Files find / -type f -perm /o=w This identifies files that are writable by any user. Locate SUID/SGID Files find / -perm /4000 -o -perm /2000 This finds files with the SUID or SGID bit set, which can sometimes pose security risks. Managing Large Data Sets When managing large file systems, searching efficiently is key. Here are a few tips for optimizing find usage: Limit Results with -print and head find /data -type f -name "*.csv" -print | head -n 10 This command quickly previews the first 10 results. Search in Parallel Use find with xargs for parallel processing: find /large_dir -type f -print0 | xargs -0 -P4 -I{} echo "Processing {}" This processes files in parallel using four threads (-P4). Managing Special File Name Characters Files with spaces, newlines, or other special characters in their names can cause issues when using find. To avoid problems, use -print0 with xargs or other commands. Delete Files Containing Special Characters Safely find . -name "*.bak" -print0 | xargs -0 rm Conclusion One useful utility that can revolutionize your Linux system interaction is the find command. It gives you the ability to handle files with accuracy and originality, from basic searches to intricate workflows. You will soon be able to utilize this program to its fullest extent if you practice the instructions provided here and try out various settings. No matter your level of experience as an administrator, find is a crucial tool for your Linux toolbox. You can try our reliable Linux VPS hosting for your projects.
16 December 2024 · 6 min to read
Linux

Bash Regular Expressions

One of the core principles of Unix systems is the extensive use of text data: configuration files, as well as input and output data in *nix systems, are often organized as plain text. Regular expressions are a powerful tool for manipulating text data. This guide delves into the intricacies of using regular expressions in Bash, helping you fully harness the power of the command line and scripts in Linux. What Are Regular Expressions? Regular expressions are specially formatted strings used to search for character patterns in text. They resemble shell wildcards in some ways, but their capabilities are much broader. Many text-processing utilities in Linux and programming languages include a regular expression engine. However, different programs and languages often employ different regular expression dialects. This article focuses on the POSIX standard to which most Linux utilities adhere. The grep Utrequires at least one match of theility The grep program is the primary tool for working with regular expressions. grep reads data from standard input, searches for matches to a specified pattern, and outputs all matching lines. grep is typically pre-installed on most distributions. You can try the commands in a virtual machine or a VPS to practice using regular expressions. The syntax of grep is as follows: grep [options] regular_expression [file...] The simplest use case for grep is finding lines that contain a fixed substring. In the example below, grep outputs all lines that contain the sequence nologin: grep nologin /etc/passwd Output: daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin games:x:5:60:games:/usr/games:/usr/sbin/nologin ... grep has many options, which are detailed in the documentation. Here are some useful options for working with regular expressions: -v — Inverts the match criteria. With this option, grep outputs lines that do not contain matches: ls /bin | grep -v zip# Output:411toppm 7z 7za 7zr ... -i — Ignores case. -o — Outputs only the matches, not the entire lines: ls /bin | grep -o zip# Output:zip zip zip zip ... -w — Searches for lines containing whole words matching the pattern. ls /bin | grep -w zip# Output:gpg-zipzip For comparison, the same command without the -w option also includes lines where the pattern appears as a substring within a word. ls /bin | grep zip# Output:bunzip2 bzip2 bzip2recover funzip Basic Regular Expressions (BRE) As previously mentioned, there are multiple dialects of regular expressions. The POSIX standard defines two main types of implementations: Basic Regular Expressions (BRE), which are supported by almost all POSIX-compliant programs, and Extended Regular Expressions (ERE), which allow for more complex patterns but aren't supported by all utilities. We'll start by exploring the features of BRE. Metacharacters and Literals We've already encountered simple regular expressions. For example, the expression “zip” represents a string with the following criteria: it must contain at least three characters; it includes the characters “z”, “i”, and “p” in that exact order; and there are no other characters in between. Characters that match themselves (like “z”, “i”, and “p”) are called literals. Another category is metacharacters, which are used to define various search criteria. Metacharacters in BRE include: ^ $ . [ ] * \ - To use a metacharacter as a literal, you need to escape it with a backslash (\). Note that some metacharacters have special meanings in the shell, so enclose it in quotes when passing a regular expression as a command argument. Any Character The dot (.) metacharacter matches any character in that position. For example: ls /bin | grep '.zip' Output: bunzip2 bzip2 bzip2recover funzip gpg-zip gunzip gzip mzip p7zip pbzip2 preunzip prezip prezip-bin streamzip unzip unzipsfx One important detail: the zip program itself isn’t included in the output because the dot (.) metacharacter increases the required match length to four characters. Anchors The caret (^) and dollar sign ($) in regular expressions serve as anchors. This means that, when included, a match can only occur at the start of a line (^) or at the end ($). ls /bin | grep '^zip'# Output:zip zipcloak zipdetails zipgrep …ls /bin | grep 'zip$'# Output:funzip gpg-zip gunzip ...ls /bin | grep '^zip$'# Output:zip The regular expression ^$ matches empty lines. Character Sets Besides matching any character in a given position (.), regular expressions allow for matching a character from a specific set. This is done with square brackets. The following example searches for strings matching bzip or gzip: ls /bin | grep '[bg]zip'# Output:bzip2bzip2recovergzip All metacharacters lose their special meaning within square brackets, except two. If a caret (^) is placed immediately after the opening bracket, the characters in the set are treated as excluded from that position. For example: ls /bin | grep '[^bg]zip' Output: bunzip2 funzip gpg-zip gunzip mzip p7zip preunzip prezip prezip-bin streamzip unzip unzipsfx With negation, we get a list of filenames containing zip but preceded by any character other than b or g. Note that zip is not included here; the negation requires the presence of some character in that position. The caret serves as a negation only if it appears immediately after the opening bracket; otherwise, it loses its special meaning. Using a hyphen (-), you can specify character ranges. This lets you match a range of characters or even multiple ranges. For instance, to find all filenames that start with a letter or a number: ls ~ | grep '^[A-Za-z0-9]' Output: backup bin Books Desktop docker Documents Downloads GNS3 ... POSIX Character Classes When using character ranges, one challenge is that ranges can be interpreted differently based on locale settings. For instance, the range [A-Z] may sometimes be interpreted lexicographically, potentially excluding lowercase a. To address this, the POSIX standard provides several classes that represent various character sets. Some of these classes include: [:alnum:] — Alphanumeric characters; equivalent to [A-Za-z0-9] in ASCII. [:alpha:] — Alphabetic characters; equivalent to [A-Za-z] in ASCII. [:digit:] — Digits from 0 to 9. [:lower:] and [:upper:] — Lowercase and uppercase letters, respectively. [:space:] — Whitespace characters, including space, tab, carriage return, newline, vertical tab, and form feed. Character classes don’t provide an easy way to express partial ranges, like [A-M]. Here’s an example of using a character class: ls ~ | grep '[[:upper:]].*' Output: Books Desktop Documents Downloads GNS3 GOG Games Learning Music ... Extended Regular Expressions (ERE) Most POSIX-compliant applications and those using BRE (such as grep and the stream editor sed) support the features discussed above. The POSIX ERE standard allows for more expressive regular expressions, though not all programs support it. The egrep program traditionally supported the ERE dialect, but the GNU version of grep also supports ERE when run with the -E option. In ERE, the set of metacharacters is expanded to include: ( ) { } ? + | Alternation Alternation allows for a match with one of multiple expressions. Similar to square brackets that allow a character to match one of several characters, alternation allows for matching one of multiple strings or regular expressions. Alternation is represented by the pipe (|): echo "AAA" | grep -E 'AAA|BBB'# Output:AAA echo "BBB" | grep -E 'AAA|BBB'# Output:BBB echo "CCC" | grep -E 'AAA|BBB'# Output: (no match) Grouping You can group elements of regular expressions and treat them as a single unit using parentheses. The following expression matches filenames starting with bz, gz, or zip. Without the parentheses, the regular expression would change meaning to match filenames starting with bz or containing gz or zip. ls /bin | grep -E '^(bz|gz|zip)' Output: bzcat bzgrep bzip2 bzip2recover bzless bzmore gzexe gzip zip zipdetails zipgrep zipinfo zipsplit Quantifiers Quantifiers specify the number of times an element can occur. BRE supports several quantifiers: ? — Matches the preceding element zero or one time, meaning the previous element is optional: echo "tet" | grep -E 'tes?t'# Output:tet echo "test" | grep -E 'tes?t'# Output:test echo "tesst" | grep -E 'tes?t'# Output: (no match) * — Matches the preceding element zero or more times. Unlike ?, this element can appear any number of times: echo "tet" | grep -E 'tes*t'# Output:tet echo "test" | grep -E 'tes*t'# Output:test echo "tesst" | grep -E 'tes*t'# Output:tesst + — Similar to *, but requires at least one match of the preceding element: echo "tet" | grep -E 'tes+t'# Output: (no match) echo "test" | grep -E 'tes+t'# Output:test echo "tesst" | grep -E 'tes+t'# Output:tesst In BRE, special metacharacters { and } allow you to specify minimum and maximum match counts for the preceding element in four possible ways: {n} — Matches if the preceding element occurs exactly n times. {n,m} — Matches if the preceding element occurs at least n and at most m times. {n,} — Matches if the preceding element occurs n or more times. {,m} — Matches if the preceding element occurs no more than m times. Example: echo "tet" | grep -E "tes{1,3}t"# Output: (no match) echo "test" | grep -E "tes{1,3}t"# Output:test echo "tesst" | grep -E "tes{1,3}t"# Output:tesst echo "tessst" | grep -E "tes{1,3}t"# Output:tessst echo "tesssst" | grep -E "tes{1,3}t"# Output: (no match) Only the lines where s appears one, two, or three times match the pattern. Regular Expressions in Practice To conclude, let’s look at a couple of practical examples of how regular expressions can be applied. Validating Phone Numbers Suppose we have a list of phone numbers where the correct format is (nnn) nnn-nnnn. Out of a list of 10 numbers, three are incorrectly formatted. cat phonenumbers.txt Output: (185) 136-1035 (95) 213-1874 (37) 207-2639 (285) 227-1602 (275) 298-1043 (107) 204-2197 (799) 240-1839 (218) 750-7390 (114) 776-2276 (7012) 219-3089 The task is to identify the incorrect numbers. We can use the following command: grep -Ev '^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$' phonenumbers.txt Output: (95) 213-1874(37) 207-2639(7012) 219-3089 Here, we used the -v option to invert the match and output only lines that do not match the specified format. Since parentheses are considered metacharacters in ERE, we escaped them with backslashes to treat them as literals. Finding Improper File Names The find command supports checking paths with regular expressions. It’s important to note that, unlike grep which matches parts of lines, find requires the whole path to match. Suppose we want to identify files and directories containing spaces or potentially problematic characters. find . -regex '.*[^-_./0-9a-zA-Z].*' The .* sequences at the beginning and end represent any number of any characters, which is necessary because find expects the entire path to match. Inside the square brackets, we use negation to exclude valid filename characters, meaning any file or directory name containing characters other than hyphens, underscores, digits, or Latin letters will appear in the output. Conclusion This article has covered a few practical examples of Bash regular expressions. Creating complex regular expressions might seem challenging at first. But over time, you’ll gain experience and skill in using regular expressions for searches across various applications that support them.
01 November 2024 · 10 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