list files and directory with dot or no extension in Linux

0
(0)

File management is one of the main tasks of Linux and includes list files and directory with or without dot, the creation, modification, permissions and maintenance of files and directories. There are various scenarios in which we may need to find specific files. In these situations, Linux provides versatile commands in the bash to efficiently find files and directories.

In this tutorial, you will learn how to find dot files and directories in the GNU Bash.

In this tutorial, you will learn how to find files and directories in the GNU Bash without using any additional utilities.

First one of the most used command to list files in a directory.

$ ls -al

or using the-A, --almost-alloption do not list implied . and ..

$ ls -Al
list files in directory be using ls -Al

Advanced list find dot files and directory

Up to this point it was not a deep knowledge and most Linux users probably know to use list files, so let us expand the commands in bash.

For example, if you only want to list files that start with a (.) dot, in Linux these are hidden files and hidden directories, the common list command is like this.

$ ls -ld .?*

This also works with almost the same output with the next command.

$ ls -dl .[^.]*

Again to list directories themselves, but not their files contents.

$ ls -dl .!(|.)

This command work only in newer GNU bash version 5 and newer.

Next list command shown long listing format for files in any directory.

$ ls -l .*

These listing can be achieved using grep, to list files and directory starting with a dot.

$ ls -a | grep "^\."
.
..
.bashrc
.config
.java
.local
.profile
.viminfo

Search for files in a directory hierarchy using find

Another powerful and versatile command isfindto search for files in a directory hierarchy, here does list files and directories starting with a dot.

$ find . -maxdepth 1 -name ".*"
.
./.local
./.profile
./.config
./.bashrc
./.java
./.viminfo

Next find in the reverse order, list only files starting without dot recursively, no directory.

$ find . -type f ! -name '.*'
./README.txt

Show only directories without dot at the beginning recursively.

$ find . -type d ! -name '.*'
./docs

Thefindcommand also allows the output to be passed to another command. Using thexargsoption does only directories that start with a dot are set permission with run chmod 755 for one directory level.

$ find . -maxdepth 1 -type d -name ".*" | xargs chmod 755

Very useful using the find command for the following scenario, here to set permission for all directories recursively with run chmod 755.

$ find . -type d -print0 | xargs -0 chmod 0755

The same to set permission of all files while run chmod 644 recursively in all directories.

$ find . -type f -print0 | xargs -0 chmod 0644

This find command is also possible with theexecoption, as shown in the next example.

$ find . -type f -exec chmod 644 {} \;

find options

-namesearching name pattern.
-type fsearch relevant files.
-type dsearch relevant directories.
-print0print the full file name on the standard output w/o trailing (LF).
-exec
-xargs
This variant of the -exec action runs the specified command on the selected files. The command line is built in much the same way that xargs builds.
-maxdepthDescend at most levels (a non-negative integer) levels of directories below the starting-points.

Search content with pattern in files recursively

find is also useful for searching patterns in files, let’s say we searching for “Red Hot Chili Peppers“, starting from the current directory recursively in all subdirectories.

$ find . -type f -print0 | xargs -0 grep -n "Red Hot Chili Peppers"
./build/README.txt:16:Red Hot Chili Peppers
./docs/Credits:2:Red Hot Chili Peppers

The output shows the path and line number of the files where the search pattern is found, enabled by the grep option -n. Note. the option -i or --ignore-case due case distinctions in patterns, default is case sensitive.

list only files and directory with no extension

If only list files and directory should be that have no extension, i.e. without dot at the end such as .txt, ran this in the command-line.

$ ls -d !(*.*)
Credits  docs

This command work only in newer GNU bash version 5 and newer.

Sometime we need more detailed output be using the long list format.

$ ls -dl !(*.*)

Verify the bash release which is available on your system, you can hit and ran this in your /bin/bash.

$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

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