The Art of Finding Files in Linux

It doesn’t matter if you have worked on Linux your entire life or if you are a complete noob. Being able to find and manage files in Linux is a skill you will use every day.

In this article, You will learn how to:

  • Find files
  • Read files
  • Manipulate test
  • Link files

Finding files is a crucial skill needed for any system admin.

Several operator flags are useful for the find command.

Finding Files with the find Command

The find command is your go-to tool for locating files in Linux. It offers a wide range of options to narrow your search. Let’s explore some key file types and search criteria:

Common File Types:

  • -type f: Regular files (e.g., text documents, configuration files)
  • -type d: Directories (folders)
  • -type l: Symbolic links (shortcuts to other files or directories)

• f = regular file

Bash
find . -type f –name httpd.conf

• d = directory

Bash
find . –type d –name html

• l = symbolic link

Bash
find . –type l –name redhat-release

user

• File is owned by the user (username or UID)

Find the top 5 files.

Bash
find -type f -exec du -Sh {} + | sort -rh | head -n 5

Reading Files

cat Command: Your Multi-Purpose Text Tool

This reads the file and outputs all of it to stdout.

The cat command in Linux is a versatile tool for working with text files. Here are some useful examples:

Displaying File Contents:

Bash
cat filename

This command will display the entire content of the specified file on the terminal.

Concatenating Multiple Files:

Bash
cat file1 file2 > newfile

This concatenates the content of file1 and file2 and writes the result to newfile.

Appending to a File:

Bash
cat file1 >> existingfile

Appends the content of file1 to the end of existingfile.

Numbering Lines:

Bash
cat -n filename

Displays the content of the file with line numbers.

Displaying Line Contents Matching a Pattern:

Bash
cat filename | grep "pattern"

Displays lines containing the specified pattern using the grep command.

Displaying Non-Printable Characters:

Bash
cat -v filename

Displays non-printable characters as visible representations.

Creating a New File with Content from the Terminal:

Bash
cat > newfile

Allows you to type content directly into the terminal, and when you press Ctrl+D, it saves the input to newfile.

Viewing Multiple Files with Page Breaks:

Bash
cat file1 file2 | more

Displays the content of file1 and file2 with a page break, allowing you to navigate through the content.

Displaying Tabs as ^I:

Bash
cat -T filename

Shows tabs in the file as ^I.

Displaying Line Endings:

Bash
cat -E filename

Displays lines with a $ symbol at the end, indicating the line endings.

less and more: Navigating Large Files

Also known as”pagers” these commands will output the contents of a file to the screen, but allow you to navigate through the file as well”Also known as “pagers” these commands will output the contents of a file to the screen, but allow you to navigate through the file as well

less filename: View file with navigation (use arrow keys, spacebar, and / to search).more filename: View file with basic scrolling (use spacebar for next page, q to quit).

head and tail: The Beginning and the End

  • head filename: Displays the first 10 lines of a file (customize with -n option).
  • tail filename: Displays the last 10 lines of a file (useful for logs).

Text Manipulation

  • sort: Sorts text numerically, alphabetically, or by specific fields.
  • wc: Counts lines, words, and characters in a file.
  • diff: Shows differences between two files (commonly used for comparing versions).

Linking Files

Every file in Linux is associated with an “inode” that stores its metadata (permissions, timestamps, etc.). The filename itself is just a pointer to the inode.

Types of Links:

  • Hard Link: Creates a new filename that points to the same inode as the original file. Changes to either file affect both.
  • Soft Link (Symbolic Link): Creates a new filename that points to the original filename (not the inode). If the original file is deleted, the soft link becomes broken.

That’s it. Thanks for reading this article. We welcome all feedback in the comments section below.

Elsewhere On TurboGeek:  How to Configure Linux Local Storage

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

7 Responses

  1. 01/09/2022

    […] Part 2 – How to manipulate files in RedHat […]

  2. 19/10/2022

    […] Part 2 – How to manipulate files in RedHat […]

  3. 19/10/2022

    […] Part 2 – How to manipulate files in RedHat […]

  4. 20/10/2022

    […] Part 2 – How to manipulate files in RedHat […]

  5. 24/03/2023

    […] Part 2 – How to manipulate files in RedHat […]

  6. 23/07/2023

    […] Part 2 – How to manipulate files in RedHat […]

  7. 16/10/2023

    […] Part 2 – How to manipulate files in RedHat […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »