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
Recursively Find Files with a Specific Extension
Grep Recursively for a Specific File Type on Linux
Redirecting Output of Bash for Loop
How to Change the Monitor Brightness on Linux
What Is the Pid in the Host, of a Process Running Inside a Docker Container
Tiny "Manually" Created Elf Giving Segmentation Fault
How to Mount from Command Line Like the Nautilus Does
Linux Custom Executable Globally Available
Creating PDF Invoices - Are There Any Templating Solutions
Fork: Retry: Resource Temporarily Unavailable
Writing to Serial Port from Linux Command Line
Append Text to File from Command Line Without Using Io Redirection
Diff Command to Get Number of Different Lines Only
Cmake Complains "The Cxx Compiler Identification Is Unknown"
How to Grep a String After a Specified Line Number
Squid: How to Disable Authentication
What Is the Meaning of Question Marks '' in Linux Kernel Panic Call Traces