
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get only the file name using find command on Linux?
Linux find statement is one of the most widely used statements that allows us to walk a file hierarchy. It is used to mostly find a specific file or directories and we can also append different other Linux statements or flags along with it to enhance or do a complex operation.
Let’s explore an example of a find statement to understand it better.
In the linux code shown below I am trying to search for a file inside my Downloads folder, and for that I am making use of the find statement.
find sample.sh
Output
sample.sh
Notice that if the find command is able to locate the file then it will simply print the name of the file, if not then it will not return anything the terminal process will terminate.
Now we know how find statement works, let’s explore the case where we only want to get the names of the files and not everything that comes with it.
If you are making use of GNU terminal, then the command shown below will help you to achieve just that.
Just write the command to your GNU terminal
find ./ -type f -printf "%f\n"
Output
immukul@192 ~ % find ./ -type f -printf "%f\n" Applications C: emp1 Desktop Documents Downloads Exercism IdeaProjects Library MEGAsync MEGAsync Downloads Movies Music Pictures Public PycharmProjects
It should be noted that if you are not on a GNU terminal, then you might get an error stating that -printf is not found. In that case, you can make use of a bash script that helps us to achieve the same output.
Consider the script code shown below −
for file in **; do echo ${file##*/}; done
Just save the above code in a file ending with a .sh extension and then give the file permission using the following command −
chmod 777 out.sh
Finally execute the shell script, using the following command −
./out.sh
Output
immukul@192 ~ % ./out.sh Applications C: emp1 Desktop Documents Downloads Exercism IdeaProjects Library MEGAsync MEGAsync Downloads Movies Music Pictures Public PycharmProjects
- Related Questions & Answers
- How to encrypt and decrypt a file using gpg command on linux
- How to get hardware information with dmidecode command on linux
- How to Install a Software on Linux Using Yum Command?
- How to send a file as an email attachment using the Linux command line?
- How to find the most recent file in a directory on Linux?
- How to Save Command Output to a File in Linux?
- How to get the maximum file name length limit using Python?
- find command in Linux with examples using C++.
- 10 Netstat Command Examples on Linux
- The best way to compress and extract files using the tar command on linux
- How to use a pipe with Linux find command?
- How to get time in milliseconds using C++ on Linux?
- How to get all the processes on the local computer with Get-Process command using PowerShell?
- How to replace spaces in file names using bash script on Linux?
- How to create a CPU spike with bash command on Linux?