Find file name in linux11/2/2023 To find only directories, use the -type option with d value. To find only files, use the -type option with f value. To perform a case-insensitive search, use the -iname option instead of -name. For example, the following command finds files and directories where the name is test in docs directory: find docs -name test To find files and directories by name, use find command with -name option. ![]() This tutorial demonstrates how to do that.Ĭreate few directories and files for testing: mkdir -p docs/dir_1 docs/dir_2 docs/test echo "Hello world" > docs/dir_1/test echo "Hello world" > docs/dir_2/test echo "Hello world" > docs/test/data.txt Find files and directories Simply add the -delete flag at the end for the command to find empty folders and files in Linux.When working with files in the Linux systems, there may need to find files or directories by name. Once you are happy with the results, then you can apply the -delete flag to finally delete the items.ĭelete Empty Files and Directories with find Commandĭeleting empty files and directories is fairly simple. We recommend you to always first run the find command without te delete flag. If you want to know about more ways to delete files and directories in Linux, check out articles on how to delete a file in Linux and how to delete a directory in Linux. pdf files in the current directory, you can use this command: The syntax to delete all files with the same extensions is:įor example, if you want to delete all. When you need to delete all files with the same extension in a directory, all you need to do is add the -delete flag at the end. Say you want to delete the “test.txt” file in the current directory, you can use this command as: The syntax to delete a file using find is: The syntax to delete files using the -delete flag with the find command in Linux is:įind -type -delete Delete File and Directory with Exact Nameĭeleting a file/directory works the same as using the find command to search for it. To make things simpler, you can use the -delete flag instead of the entire -exec part to delete the items. Some users may find the above command difficult to grasp. The basic syntax to delete files using the find command is: However, with the power of the -exec command, you can easily delete files or directories filtered by the find commands based on the specified rules. You’d think that a complicated shell script needs to be written for it. ![]() Sometimes you may need to delete specific files/directories in Linux. Find and Delete Files and Directories in Linux If you do not know the exact file name, the find command being very versatile can be used to search for files even with the part of a name.ĩ. The most common way to search for files and directories with the find command in Linux is with their respective name. This flag is used to search for files and directories owned by a specific user.īest Ways to Use the find Command in Linux This flag filters files and directories based on their permissions. This flag filters the items based on their memory occupancy size. ![]() The exec flag is used to execute shell commands on the output. This specifies the type of items to look for. This option is used to specify the name of the file or folder. Some of the common options to pair with the find command are: In this case the -print acts as a conditional and becomes an. Perhaps they are trying to control printing. Here from the man page they are saying if the pattern matches, print it.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |