Linux list only files

How to list files without directories, and filter by name (ls options)

I know about the -d flag, but it doesn’t get me quite what I want.

9 Answers 9

This sounds like a job for find .

  • Use -maxdepth to only return the current directory, not recursivly search inside subfolders
  • Use -type f to only return files and not directories or device nodes or whatever else
  • Use a combination if -not and -name to avoid the files with names you don’t want

It might come together like this:

find /path/to/uploads -maxdepth 1 -type f -not -name 't_*' 

@EmmyS: You might find one more little trick useful here. @Gilles mentioned using -exec ls -lG — <> + in his answer to get the output using extra options to ls. You can also add just an -ls to this find to get a quick and dirty approximation of ls’s detailed view.

GNU ls (i.e. the ls command on non-embedded Linux systems and Cygwin, also available on some other unices) has an option to hide some files, based on their names. There’s no way to ignore directories though.

Another approach is to make your shell do the matching. Bash, ksh and zsh have a negation pattern !(t_*) to match all files except those matching t* ; in bash this feature needs to be turned on with shopt -s extglob , and in zsh it needs to be turned on with setopt ksh_glob . Zsh also has the equivalent syntax ^t_* which needs to be turned on with setopt extended_glob . This still doesn’t ignore directories. Zsh has an extra feature that allows to match files not only by name but also by metadata and more: glob qualifiers. Add (.) at the end of a match to restrict to regular files. The negation ^ is part of the name matching syntax, so ^t_*(.) means “all regular files not matching t_* ” and not “all files that aren’t regular files matching t_* ”.

setopt extended_glob # put this in your ~/.zshrc ls uploads/^t_*(.) 

If you find yourself without advanced tools, you can do this on any unix with find . It’s not the kind of thing you’d typically type on the command line, but it’s powerful and precise. Caleb has already shown how to do this with GNU find. The -maxdepth option isn’t portable; you can use -prune instead, to portably stop find from recursing.

find uploads/* -type d -prune -o \! -type f -name 't_*' -print 

Replace -print by -exec ls -lG — <> + to execute ls with your favorite options on the files.

All the commands above hide dot files (i.e. files whose name begins with a . ). If you want to display them, pass -A to ls , or add the D glob qualifier in zsh ( ls uploads/^t_*(.D) ). With find , you can use a different approach of making it recurse one level only ( find doesn’t treat dot files specially). This only fully works if you run find in the current directory.

cd uploads && find . -name . -o -type d -prune -o \! -type f -name 't_*' -print 

Источник

Читайте также:  Linux close file descriptor

How to Only List the Files and Not the Directories in Linux

In Linux, we use the “ls” command to navigate through directories for files. Listing all the files and folders in Linux is a common command. But occasionally, we must only list the files and not the folders. In that case, this command does not work. To only list the files using the “ls” command, we need to write “ls *.txt”. But this command works if we need to only list the text files since this command only lists the text files. If we need to list all the files and not only the text files, this command becomes useless. For that purpose, Linux provides us with many options. To list all the files in a specific directory only, we use the following methods:

Using the Ls Command to List the Directories

The ls command is the one that can be used to list the files from directories. The “ls” command works the same as how we manually go through the directory and then get all of the files that we want but it makes it easy for us to enlist them by just running a simple command. It also enables us to pass the options along with it to instruct the compiler to list the files according to your desire.

Like the previously-mentioned ls command, we pass the –p option. If we want to see all the hidden files, we add the “-a” option to the ls command. If we know the file name or a part of it, we can also use the “run-parts regex” command. This command lists all the files whose names match the provided name. But to make this command work, we must know at least a part of the filename.

Listing the Files by Name

When it comes to listing the files by names, using the ls command is the easiest way to list them. To list the name of files, we simply have to write the following command:

In the provided syntax, we use the ls command along with the head and the number “4”. This means that it only displays the first four encountered files where the head indicates that each file name is printed in a new line instead of printing all in the same line to make it easy to read. When we run the previous command, the output is shown like in the following snippet in which the names of the files are displayed in our terminal.

Now, let’s suppose that we want to display all the files in the terminal. We simply run the following command:

As we can see in the screenshot, all of the files that are currently stored in the home directory are displayed by running the previous command.

Same as in the previous example, we can print the complete details of the files along with its name by passing another flag which is “-l” along with the ls command using the following command:

As seen in the following figure, the details of the files are also printed in the terminal:

Читайте также:  Удалил файл linux восстановить

Listing the Files in Reverse Order

Using the ls command, we can also print the file names in reverse order. This means that we want to list the files in an upside-down order, or that our compiler starts listing the files from the last one until the first. For that, we run the following command:

After running this command, we get the following output in which the files are printed in reverse order:

Listing the Directories

The ls command enables us to list the files as well as the directories. When we only want to list the name of the directories within a specific location, we simply use the “-d” option with the ls files. The “d” flag denotes the directories.

After running the previous command, all of the directories that are created in the home directory are displayed in the terminal as shown in the following figure:

Listing the Files Using the Run-Parts Regex Command

The run-parts regex is the Linux command that is used to list all the files that are stored in our system. It is responsible for displaying all the available files. A regex is a tool or pattern that can be used to match the strings that follow any pattern. When it is about searching for the files, the “run apart regex” command can be used. It is effective for enlisting the files from the directories. For that, we must run the following command:

When we run the provided command, we get all the files enlisted that are stored in the home directory of our system. When we want to fetch from any specified directory, we just have to pass the path of the directory from which the files are to be fetched.

Listing the Files Using the Find Command

To list all the files only in a specific directory, we use the find command. Same as the “ls” command, there are some options that can be passed with the find command to instruct our compiler to get the desired output.

By using maxdepth -1, we make sure that we are looking only at the current directory. If we want to look into all files in that directory and all the other subdirectories, we remove this option where “type f” denotes the type of files that we are searching.

By running this command, we fetch the files that are currently stored in our home directory. Let’s suppose we want to fetch for the other directory. We simply replace the dot “.” with the directory’s path which should be used to enlist the files. In the following snippet, the output of the command is shown in which all files are enlisted in our terminal.

Conclusion

In this guide, we discussed how the files are listed using the commands, and how we can list multiple files according to our needs without manually searching for them. By a single command, we can get our desired files. After introducing you to the methods of listing the files, we performed the various examples to make it easy for you to implement them according to your need.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.

Читайте также:  Linux create local repository

Источник

How to List Only Files Not Directories in Linux?

In Linux, we use the “ls” command for listing the content of the directories or folders but if the user wants to display only files this command doesn’t work. However, it can be filtered out for specific files such as “ls .pdf,” but what if the user wants to display all types of files? In this situation, Linux provides various methods for displaying only files in the directory or folder.

This article will demonstrate the methods to list only files in Linux. The content for the post is:

Method 1: List Only Files Using ls Utility

The combination of the “grep” and the “ls” utility through pipe(|) command can be used to display the only files for the specified directory. The output of the “ls” command will be sent to the “grep” command as an input then the “v” flag will be used for non-matching. Here the “d” option is used for the directory. So the grep command will not display the directories, and as a result, you will get the output for only files:

All files in the home directory will be displayed.

Users can save this command as an alias with a variable name in the .bashrc file, and that variable can be used as a command for displaying files only. Open .bashrc in the nano editor and paste the given line:

$ nano .bashrc #alias alias lf="ls -l | egrep -v '^d'"

Save the file by pressing “Ctrl+O” and exit from the editor by pressing “Ctrl+X”.

Run the source command for executing the new changes in the .bashrc file:

The “.bashrc” file will be executed.

Now run the “lf” as a command for listing the files:

All the files will be listed in the directory.

Let’s move toward method 2.

Method 2: List Only Files Using find Utility

The second method for listing the files only uses the find utility. The “find” is a utility for searching files or directories in Linux. We can use this utility for finding and listing the files. Run the given command in the terminal with the “maxdepth 1” parameter, which means searching the files into one subdirectory. Then the “ls” command will list that files:

$ find . -maxdepth 1 -type f -ls

All files will be listed, which can be seen in the above image.

Note: To know in-depth knowledge of the find command, check out our latest article on the find command.

Method 3: List Only Files Using run-parts regex Utility

The last method to list the only files in Linux is using the “run-parts” utility. This utility is used for displaying all executable files available in the directory. Use the “run-parts” command with the “-list” flag that will display the name of all the valid files, and the “regex” option is used for specifying the pattern:

All the files will be listed in the directory.

Conclusion

There are three possible methods to list only the files in the directory. The first method combines the “ls” and “grep” utilities. The second method is to use the “find” utility, while the third method is to use the “run-parts” utility with the “list” flag. This write-up has illustrated the three most efficient methods for displaying only files in Linux.

Источник

Оцените статью
Adblock
detector