List all folder linux

how to list folders or directories in linux

In Linux, everything is implemented as “Files“. That mostly means that they all share several “common” features/properties while having some unique properties of their own. All files and folders are implemented the same way, as Files. That is a very abstract explanation of the whole thing and there are still some differences between file systems.

If you want to really understand how files and folders are implemented in your filesystem, then you can find what specific filesystems you are using and then look into the specific implementation of the same. Folders are just “special” files with certain specific properties. Any more detailed explanation of files and folders is probably beyond the scope of this post.

The important thing for you to remember that everything including files, folders, links and devices etc. etc. are all Files at its core. That helps to better understand how the whole thing works.

So, you want to list all folders or directories inside a folder. We will mainly deal with listing only folders (and not files) although these commands can list both files and folders. There are mainly two different commands you can use to list files and folders: ls and find.

Please remember that the output might work slightly different depending on the shell you use. I use fish by default, but the following examples should work in other shells as well, such as bash and zsh.

list all folders in a folder

When using ls to list folders, the command line option of consequence is -d. The -d option will list the directories by themselves, and not its content. This, however also depends on how you specify the path to the folder. So, if you want to all directories in the current working folder…

In order to specify another directory, which is not the current folder you will need to specify the path as below…ending with */

Another option is the find command. I find that the find command is much more versatile than ls itself, but it can be slower depending on the number of files you have in the folder. So, using find command to print out folders in the current directory.

$ find . -maxdepth 1 -type d

The -maxdepth command line option lets you control the depth of the folders you want to traverse. This can be an extremely powerful feature when you want to traverse the folder recursively. The -type option is used to specify what types of files you want listed. The d specifies that we want only directories. You can use f for files and l for symbolic links.

If you want to list first level folders in another directory, then substitute the path (.) in the above example with the path. Also, if you want to see the directory/file metadata such as date, permissions, user etc then tack on the -ls option at the end.

$ find newfolder/ -maxdepth 1 -type d -ls

list all folders and sub-folders

Now, let’s say you want to print out all folders and sub-folders inside a directory. In my opinion, the simplest and easiest command for this is find. You can use ls as well, but there is no straight forward way to print out only the directories without filtering the output by using either awk, grep or cut etc.

Читайте также:  Usb port configuration windows

The command line option -R or –recursive will allow you to recurse into each and every folder and its sub-folders. You can use pretty much the same find command in the previous section to print the folders and sub-folders.

$find myfolder/ -type d -ls

Now, you can easily filter the depth of the folders using the -maxdepth option. If you want to display only folders and sub-folder that are 3 levels deep, then

$ find path/to/folder -maxdepth 3 -type d

list folders by size

If you want to find the size of any particular folder, then it can easily be done with either ls or du commands. We will quickly deal with listing the folders along with the total size of the folder. We can then sort the output to find which folder is using the most space.

We use the du command to compute the size or space used by each listed folder first. We can then the sort command to sort the folders in decreasing order of size.

bash$ find ./ -maxdepth 3 -type d -exec du -s <> \; | sort -nr

You can leave out the maxdepth option if you want to list all sub-folders recursively.

The above syntax will work with bash. If you encounter an error with another shell, then refer to the shell documentation. For example, in fish you will need to either escape the “<” and “>” (ie. \ <\>) or use quotes around it ‘<>’. The example below will work with both shells.

fish$ find ./ -maxdepth 3 -type d -exec du -s ‘<>‘ \; | sort -nr

Источник

How to find a folder in Linux using the command line

Command to find a folder in Linux

  1. find command – Search for files and folder in a directory hierarchy
  2. locate command – Find files and folders by name using prebuilt database/index

How to find folder on Linux using find command

The syntax is:
find /where/to/look/up/ criteria action
find /folder/path/to/look/up/ criteria action
find /folder/path/ -name «folder-name-here»
find /search/path/ -name «folder-name-here» -print
find /search/path/ -name «folder-name-here» -ls
find /folder/ -name «pattern»

Finding a folder named Documents

To find a folder named “Documents” in your home directory ($HOME i.e. /home/vivek/ home directory), run:
find $HOME -type d -name «Documents»
OR
find

-type d -name «Documents»
OR
find /home/vivek/ -type d -name «Documents»

find command in action on Linux

How to search for case incentive folder names

You can force find command interpret upper and lowercase letters as being the same. For example match Documents, DOCUMENTS, DocuMEnts and so on by passing the -iname option:
find $HOME -type d -iname «Documents»
OR
find

-type d -iname «Documents»
OR
find /home/vivek/ -type d -iname «Documents»
Sample outputs:

How to search a folder named /etc/ in the root (/) file system

When searching / (root) file system, you need to run the find command as root user:
# find / -type d -name «etc»
OR
$ sudo find / -type d -name «etc»
OR
$ sudo find / -type d -iname «etc»

How to hide “Permission denied error messages” when using find command

The find will show an error message for each directory/file on which you don’t have read permission. To avoid those messages, append 2>/dev/null at the end of each find command:
$ find /where/to/look/ criteria action 2>/dev/null
$ sudo find / -type d -iname «etc» 2>/dev/null

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

How do I find a directory called python.projects?

Try:
find / -type d -iname «python.projects» -ls
OR
find / -type d -name «python.projects» -ls
It is also possible to use the bash shell wild cards, run:
find / -type d -name «python.*»
sudo find / -type d -name «?ython.*»

Understanding find command options

  • -name : Base of file name (the path with the leading directories removed) matches shell pattern.
  • -iname : Perform a case insensitive search for given pattern
  • -print : Print the full file name on the standard output (usually screen), followed by a newline.
  • -ls : Display current file in ls -dils format on standard output i.e. your screen.
  • -type d : Only list folders or directories.
  • -type f : Only list files.

Search folder in Linux using locate command

To search for a folder named exactly dir1 (not *dir1*), type:
$ locate -b ‘\dir1’
$ locate -b ‘\folder2’
Just search for file name matching Pictures, type:
$ locate Pictures
For more info see “UNIX Find A File Command“.

Читайте также:  Easycap viewer mac os

Conclusion

In this tutorial, you learned how to find a folder on the Linux system using find and locate commands. For more info see gnu find command help page here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to List All Files Ordered by Size in Linux

In one of our several articles about listing files using the popular ls command, we covered how to list and sort files by last modification time (date and time) in Linux. In this short handy article, we will present a number of useful ls command options to list all of the files in a certain directory and sort them by file size in Linux.

To list all files in a directory, open a terminal window and run the following command. Note that when ls invoked without any arguments, it will list the files in the current working directory.

In the following command the -l flag means long listing and -a tells ls to list all files including (.) or hidden files. To avoid showing the . and .. files, use the -A option instead of -a .

List All Files in Linux

To list all files and sort them by size, use the -S option. By default, it displays output in descending order (biggest to smallest in size).

List All Files Sort By Sizes

You can output the file sizes in human-readable format by adding the -h option as shown.

List Files Sort By Sizes in Linux

And to sort in reverse order, add the -r flag as follows.

List All Files Sort By Sizes in Reverse Order

Besides, you can list subdirectories recursively using the -R option.

List Sub-directories Recursively

You will also find the following related articles useful:

If you any other way to list the files ordered by sizes in Linux, do share with us or do you have questions or thoughts to share about this guide? If yes, reach us via the feedback form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

The Linux LS Command – How to List Files in a Directory + Option Flags

Since the creation of Unix in the 1970s, a lot of operating systems have used it as their foundation. Many of these operating systems failed, while others succeeded.

Linux is one of the most popular Unix based operating systems. It’s open source, and is used all over the world across many industries.

One amazing feature of the Linux operating system is the Command Line Interface (CLI) which allows users to interact with their computer from a shell. The Linux shell is a REPL (Read, Evaluate, Print, Loop) environment where users can enter a command and the shell runs it and returns a result.

The ls command is one of the many Linux commands that allow a user to list files or directories from the CLI.

In this article, we’ll go in depth on the ls command and some of the most important flags you’ll need day-to-day.

Prerequisites

  • A computer with directories and files
  • Have one of the Linux distros installed
  • Basic knowledge of navigating around the CLI
  • A smile on your face 🙂

The Linux ls Command

The ls command is used to list files or directories in Linux and other Unix-based operating systems.

Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

Launch your terminal and type ls to see this in action:

How to list Files in a Directory with Options

The ls command also accepts some flags (also known as options) which are additional information that changes how files or directories are listed in your terminal.

In other words, flags change how the ls command works:

PS: The word contents used in throughout the article refers to the files and directories being listed, not the actual contents of the files/directories ?

List files in the current working directory

Type the ls command to list the contents of the current working directory:

List files in another directory

Type the ls [directory path here] command to list the contents of another directory:

Читайте также:  Чем проверить ssd linux

List files in the root directory

Type the ls / command to list the contents of the root directory:

List files in the parent directory

Type the ls .. command to list the contents of the parent directory one level above. Use ls ../.. for contents two levels above:

List files in the user’s home directory (/home/user)

command to list the contents in the users’s home directory:

List only directories

Type the ls -d */ command to list only directories:

List files with subdirectories

Type the ls * command to list the contents of the directory with it’s subdirectories:

List files recursively

Type the ls -R command to list all files and directories with their corresponding subdirectories down to the last file:

If you have a lot of files, this can take a very long time to complete as every single file in each directory will be printed out. You can instead specify a directory to run this command in, like so: ls Downloads -R

List files with their sizes

Type the ls -s command (the s is lowercase) to list files or directories with their sizes:

List files in long format

Type the ls -l command to list the contents of the directory in a table format with columns including:

  • content permissions
  • number of links to the content
  • owner of the content
  • group owner of the content
  • size of the content in bytes
  • last modified date / time of the content
  • file or directory name

List files in long format with readable file sizes

Type the ls -lh command to list the files or directories in the same table format above, but with another column representing the size of each file/directory:

Note that sizes are listed in bytes (B), megabytes (MB), gigabytes (GB), or terabytes (TB) when the file or directory’s size is larger than 1024 bytes.

List files including hidden files

Type the ls -a command to list files or directories including hidden files or directories. In Linux, anything that begins with a . is considered a hidden file:

List files in long format including hidden files

Type the ls -l -a or ls -a -l or ls -la or ls -al command to list files or directories in a table format with extra information including hidden files or directories:

List files and sort by date and time

Type the ls -t command to list files or directories and sort by last modified date and time in descending order (biggest to smallest).

You can also add a -r flag to reverse the sorting order like so: ls -tr :

List files and sort by file size

Type the ls -S (the S is uppercase) command to list files or directories and sort by date or time in descending order (biggest to smallest).

You can also add a -r flag to reverse the sorting order like so: ls -Sr :

List files and output the result to a file

Type the ls > output.txt command to print the output of the preceding command into an output.txt file. You can use any of the flags discussed before like -la — the key point here is that the result will be outputted into a file and not logged to the command line.

Then you can use the file as you see fit, or log the contents of the file with cat output.txt :

.

Conclusion

There are tons of other commands and combinations you can explore to list out files and directories based on your needs. One thing to remember is the ability to combine multiple commands together at once.

Imagine you want to list a file in long format, including hidden files, and sort by file size. The command would be ls -alS , which is a combination of ls -l , ls -a , and ls -S .

If you forget any command or are unsure about what to do, you can run ls —help or man ls which will display a manual with all possible options for the ls command:

Thanks for reading!

Software Engineer, Content Creator & Developer Advocate.

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives and help pay for servers, services, and staff.

Источник

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