Professional Documents
Culture Documents
Learn more.
How-To Geek
Linux
The ls command
RELATED
is one of those
10 Basic Linux
commands with a
Commands for
wealth of options. Beginners
Perhaps this is
part of the problem. There are so many options, how do you
sift through them to find the useful ones? And having found
them, how do you remember them?
type ls
Simple ls Listings
Everyone who’s spent some time using the Linux terminal
knows that, by default, ls lists the files and directories in the
current directory.
ls
ls -1
We’ll discuss that weird-looking filename at the top of the
listing in a minute.
ls Help gc_help
When ls has listed the contents of the first directory it lists the
contents of the second. It prints the name of each directory as
it processes them:
Using File Patterns
ls ip_*
ls *.c
You can also use ls with grep , and use grep‘s pattern
matching capabilities. Let’s look for any files that have the
string “_pin_” in their name:
ls | grep _pin_
ls | grep _pin_
ls *_pin_*
You can use the -b (escape) option to allow you to see what
the file name actually contains. This option causes ls to use
the escape sequences of the C programming language to
represent the control-characters.
ls -b a*
Ignoring Files
ls
ls --hide=*.bak
ls -l
The first thing ls displays is the total size of all the files in the
listing. Then each file or directory is displayed on a line by
itself.
The first set of ten letters and dashes are the file type and the
owner, group and other file permissions.
The very first character represents the file type. It will be one
of:
d: A directory.
l: A symbolic link.
n: A network file.
p: A named pipe.
s: A socket.
The first set of three characters are the permissions for the file
owner. The second set of three permissions are for group
members, and the last set of three permissions is for others.
The execution permission for the group can also be an s. This
is the setgid bit. When this is applied to a file, it means the file
will be executed with the privileges of the ower’s group. When
used with a directory, any files created inside it will take their
group permissions from the directory they’re being created in,
not from the user who is creating the file.
We can see the sticky bit on the “/tmp” directory. Note the use
of the -d (directory) option. This causes ls to report on the
details of the directory. Without this option, ls will report on
the files inside the directory.
ls -l -d /tmp
The name of the owner and group are displayed next. They are
followed by the file size and the date of the last modification of
the file. Finally, the filename is given.
ls -l -h
Showing Hidden Files
ls -l -a
The two entries “.” and “..” represent the current directory and
the parent directory, respectively. A file called “.base_settings”
is now visible for the first time.
Omitting . and .. from Listings
If you don’t want your listing cluttered up with the “.” and “..”
entries, but you do want to see hidden files, use the -A (almost
all) option:
ls -l -A
The hidden file is still listed, but the “.” and “..” entries are
suppressed.
ls -l -R
ls works its way through the entire directory tree below the
starting directory, and lists the files in each subdirectory.
ls -n
Sorting The Listings
ls -X -1
The directories are listed first (no extensions at all) then the
rest follow in alphabetical order, according to the extensions.
ls -l -h -S
ls -t | head -1
ls -t | tail -1
To Reverse the Sort Order
ls -l -h -S -r
The listing is now ordered from the smallest file to the largest
file.
Check out the man page for ls, there are many more options.
Some of them satisfy somewhat obscure use cases, but once
in a while, you’ll be glad you know about them.
Perhaps you want to see the inode number of the files? Use
the inode option:
ls -i
/: A directory.
@: A symlink.
|: A named pipe.
=: A socket.
*: An executable files
ls -F
Linux Commands
DAVE MCKAY
Dave McKay first used computers when
punched paper tape was in vogue, and he has
been programming ever since. After over 30
years in the IT industry, he is now a full-time
technology journalist. During his career, he has worked as a
freelance programmer, manager of an international software
development team, an IT services project manager, and, most
recently, as a Data Protection Officer. His writing has been
published by howtogeek.com, cloudsavvyit.com,
itenterpriser.com, and opensource.com. Dave is a Linux
evangelist and open source advocate. READ FULL BIO »
How-To Geek is where you turn when you want experts to explain technology. Since we
launched in 2006, our articles have been read more than 1 billion times. Want to know
more?