DEV Community

Cover image for Linux: Finding Files
Mariam Reba Alexander
Mariam Reba Alexander

Posted on

1 1 1 1 1

Linux: Finding Files

Most of you will know how to navigate directories using the cd command and how to create a directory with mkdir. You will also be familiar with the mv command. The mv command is not just for moving files from one directory to another; it can also be used to rename files like this.

mv oldfilename.txt newfilename.txt
Enter fullscreen mode Exit fullscreen mode

In order to list files and directories in the current folder you are in, you may use the ls command.

You can also list files and folders not in your current working directory by providing the directory path with commands like this.

ls /home/John
# to list with more details of files
ls -l /home/John
# to list all files, including hidden files
ls -la /home/John
Enter fullscreen mode Exit fullscreen mode

If you look up the ls command using man ls, you will see that you can pass a lot of options. One of the useful ones is the -t option to sort the files in ascending order of time. For example, if you want to see the latest modified file at the top of the list, use the command below.

ls -t
Enter fullscreen mode Exit fullscreen mode

The ls command sometimes lists a long list of files and directories, but what if you want to filter out files based on the name, time, or some other criteria ? To search through files, one can use the find command.

# finds files in the specified folder with extension .txt and which are newer than the date 2025-01-01
find /home/John -type f -name *.txt -newermt 2025-01-01
Enter fullscreen mode Exit fullscreen mode

There is also an option to execute commands on results using -exec.

Use man find to see the full list of options.

A quick way to find the path of a file or directory is by using the locate command.

locate *.txt
Enter fullscreen mode Exit fullscreen mode

Exploring the manual pages with man can reveal additional options for each command. Familiarizing these tools will enable you to navigate your operating system effectively.

Image of Datadog

Get the real story behind DevSecOps

Explore data from thousands of apps to uncover how container image size, deployment frequency, and runtime context affect real-world security. Discover seven key insights that can help you build and ship more secure software.

Read the Report

Top comments (0)

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay