Followers

Linux ls command

 

How to Use the ls Command

The ls command allows you to list all the files and folders in a given directory.

The most basic usage of the ls command is to use it without any options. When you run ls alone, it will display a list of all the files and folders in your current working directory. Here’s an example of the ls command in action:

ls

Here’s the output of this command:

README.md      lib         package-lock.json     pages        styles.css
components     node_modules     package.json     public       yarn.lock

As you can see, the ls command has returned a list of all the files in the folder currently being viewed. Also note that ls orders the files in a folder alphabetically. Capital letters are given priority over lowercase letters, which is why “README.md” appears at the start of our list of tiles.

You can also use the command to list the files in a particular directory. Suppose you want to know what files exist in the “/var” directory on your computer. You could do so using this command:

ls /var

You can only use the ls command if you have permission to read a file and its contents. Otherwise, an error will be returned.

List Hidden Files

If you have some experience working with Linux, you’ll know there are a lot of hidden files on your computer. These are files that begin with a period and do not appear when you try to list the files and folders in a directory.

To view these files, you need to specify the -a flag with the ls command, like so:

ls -a 

This command returns:

..
.bash_history
.devaccounts
Desktop

This is only a sample of the files returned by this command. As you can see, our current working folder contains files like “.bash_history” and “.devaccounts” which would otherwise have been hidden if we had not specified the -a flag.

Show Detailed File Information

The ls command, by default, shows file names. But what if you want to learn more about those files? What if you want to know the owners of that file, or when it was last modified? That’s where the -l flag comes in.

The -l flag instructs Linux to print out a list of files with detailed descriptions.

Suppose we want more information about the file “/etc/passwd”. We can get it using this command:

ls -l /etc/passwd

Our command returns:

-rw-r--r--  1 root  wheel  6946 Feb 29 06:10 /etc/passwd

As you can see, there’s a lot more information now displayed for us to review. The -l flag shows us the following pieces of information, in order:

  • The file type
  • File permissions
  • Number of hard links to the file
  • The owner of the file
  • The file group
  • The size of the file
  • When the file was last modified
  • The name of the file and its location

This tells us that the /etc/passwd file is owned by the user “root”, and that the file was last modified on February 29 at 6:10am.

List Files Recursively

By default, the ls command only lists the files and folders in a particular directory. However, you can also use the command to generate a recursive list of the contents of all subdirectories in a folder. You can do so using the -R flag, like so:

ls -R

This command returns:

app.rb        	config.rb        	README.md   	lib

./lib:
create_schema.rb

In the output of this command, we can see not only a list of the files in our current folder, but also a recursive list of the contents of all subdirectories. In this case, the “lib” directory contains the file “create_schema.rb”, which is displayed in our output alongside all the files in our current directory.

Sort Files by File Size

The -lS flag allows you to sort the files and folders returned by the ls command by size. The files will be ordered in descending order of their size.

Suppose we want to generate a list of all the files in the “~/ folder” in order of their size. We could do so using this command:

ls -lS

Our command returns:

drwx------@ 98 James  staff  3136 May 16 17:37 Library
drwxrwxr-x  18 James  staff   576 Jun  9 10:34 Projects
drwx------@ 14 James  staff   448 Apr 10 12:48 Dropbox

The output of this command has been edited for brevity. As you can see, our files and folders are ordered in descending order of their size. “Library” is the biggest folder whose size is 3136, so it appears at the top of our list.

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
static_page
DevOpsWorld: Linux ls command
Linux ls command
DevOpsWorld
https://www.devopsworld.co.in/p/linux-ls-command.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/p/linux-ls-command.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content