How to List One Filename Per Output Line in Linux

How do I list one filename per output line in Linux?

Use the -1 option (note this is a "one" digit, not a lowercase letter "L"), like this:

ls -1a

First, though, make sure your ls supports -1. GNU coreutils (installed on standard Linux systems) and Solaris do; but if in doubt, use man ls or ls --help or check the documentation. E.g.:

$ man ls
...
-1 list one file per line. Avoid '\n' with -q or -b

How to get a list of file names in different lines

ls -1

That is a number, not small L.

Why `ls` list multiple files per line, but `ls pipe/redirect` list just 1 file per line?

You can check this line from the source:

if (format == long_format)
format = (isatty (STDOUT_FILENO) ? many_per_line : one_per_line);

It uses the isatty function to check if stdout points to a tty, and to print many_per_line if it does or one_per_line if it does not.

Display filename before matching line

Try this little trick to coax grep into thinking it is dealing with multiple files, so that it displays the filename:

grep 'pattern' file /dev/null

To also get the line number:

grep -n 'pattern' file /dev/null

How can I use grep to show just filenames on Linux?

The standard option grep -l (that is a lowercase L) could do this.

From the Unix standard:

-l
(The letter ell.) Write only the names of files containing selected
lines to standard output. Pathnames are written once per file searched.
If the standard input is searched, a pathname of (standard input) will
be written, in the POSIX locale. In other locales, standard input may be
replaced by something more appropriate in those locales.

You also do not need -H in this case.

Print list of files in a directory to a text file (but not the text file itself) from terminal


printf '%s\n' * > output.txt

Note that this assumes that there's no preexisting output.txt file -
if so, delete it first.

  • printf '%s\n' * uses globbing (filename expansion) to robustly print the names of all files and subdirectories located in the current directory, line by line.

  • Globbing happens before output.txt is created via output redirection > output.txt (which still happens before the command is executed, which explains your problem), so its name is not included in the output.

  • Globbing also avoids the use of ls, whose use in scripting is generally discouraged.

To show only file name without the entire directory path

ls whateveryouwant | xargs -n 1 basename

Does that work for you?

Otherwise you can (cd /the/directory && ls) (yes, parentheses intended)

List all files with file count as one of the output columns in $BASH?

Directories

ls -al | awk '/^d/{d++}{print}END{print "Directories: "d}'

All files

ls -al | awk '{print}END{print "Files:" NR}'

LS - sort by date, display only file name

You could try the following command:

ls -ltr | awk '{ print $9 }' | tail -n +2

It extracts the file names from the ls -ltr command.



Related Topics



Leave a reply



Submit