What are directory files in linux

Содержание
  1. Directory
  2. Overview of a directory and path
  3. Overview of a Linux directory path
  4. How do I list or view directories?
  5. How to change a directory
  6. How to make a directory
  7. How to delete a directory
  8. What is the purpose of a directory?
  9. What type of files can be stored in a directory?
  10. Invalid directory characters
  11. How are files different than a directory?
  12. Does a directory have an extension?
  13. Linux Directory Structure Explained for Beginners
  14. Linux directory structure
  15. / – The root directory
  16. /bin – Binaries
  17. /dev – Device files
  18. /etc – Configuration files
  19. /usr – User binaries and program data
  20. /home – User personal data
  21. /lib – Shared libraries
  22. /sbin – System binaries
  23. /tmp – Temporary files
  24. /var – Variable data files
  25. /boot – Boot files
  26. /proc – Process and kernel files
  27. /opt – Optional software
  28. /root – The home directory of the root
  29. /media – Mount point for removable media
  30. /mnt – Mount directory
  31. /srv – Service data
  32. The Linux LS Command – How to List Files in a Directory + Option Flags
  33. Prerequisites
  34. The Linux ls Command
  35. How to list Files in a Directory with Options
  36. List files in the current working directory
  37. List files in another directory
  38. List files in the root directory
  39. List files in the parent directory
  40. List files in the user’s home directory (/home/user)
  41. List only directories
  42. List files with subdirectories
  43. List files recursively
  44. List files with their sizes
  45. List files in long format
  46. List files in long format with readable file sizes
  47. List files including hidden files
  48. List files in long format including hidden files
  49. List files and sort by date and time
  50. List files and sort by file size
  51. List files and output the result to a file
  52. Conclusion

Directory

A directory is a location for storing files on your computer. Directories are found in a hierarchical file system, such as Linux, MS-DOS, OS/2, and Unix.

Pictured is an example of output from the Windows/DOS tree command. It shows all the local and subdirectories (e.g., the «big» directory in the «cdn» directory). When looking at this overview, the current directory is the root directory of the C: drive. It’s called the «root» directory because there is nothing beneath it, and the other directories «branch» from it. If you are using an operating system with multiple user accounts, the directory may also be referred to as a home directory.

In a GUI such as Microsoft Windows, directories are referred to as folders. However, a directory and folder are synonymous.

Overview of a directory and path

Below is an example of what a directory path would look like in MS-DOS.

In the picture, C: is the drive letter and the current directory is System32, which is a subdirectory of the Windows directory.

Overview of a Linux directory path

Below is an example of what a directory path may look like in a Linux or Unix variant.

In the above example, the current directory is bin, and it is a subdirectory of the usr directory. The beginning forward slash is the root directory.

How do I list or view directories?

To see directories and files in the current MS-DOS directory, use the dir command. In Linux to view directories and files in the current directory, you’d use the ls command.

  • See the dir command page for further information and examples on this command.
  • See the ls command page for information and examples on this command.

Both of the above commands also have switches that can be added to them to only view directories and not directories and files.

How to change a directory

To change a directory in MS-DOS, Linux, Unix, and most other command line operating systems, use the «cd» command.

How to make a directory

To make a directory in MS-DOS, Linux, Unix, and most other command line operating systems, use the «mkdir» command.

How to delete a directory

To remove a directory in MS-DOS, use the «rmdir» command. In Linux and Unix, use the «rm -r» command.

What is the purpose of a directory?

A directory is used to store, organize, and separate files and directories on a computer. For example, you could have a directory to store pictures and another directory to store all your documents. By storing specific types of files in a folder, you could quickly get to the type of file you wanted to view. In other words, if only pictures were in a pictures directory, it’s easier to find a picture than a directory containing all types of files.

Directories are also used as a place to store programs. For example, when you install a program, its files are stored in a unique directory that may contain subdirectories and tens, hundreds, or thousands of files related to that program. By storing a program in its own directory, it helps prevent files with the same name from getting overwritten, modified, or deleted by other programs.

What type of files can be stored in a directory?

A directory can contain one or more files of any type and can even store other directories that contain files.

Invalid directory characters

Below is a listing of reserved characters that cannot be used when creating a file or directory on most operating systems. When creating directories, if any of these characters are used, you’ll receive an error or encounter other problems.

Читайте также:  Building stained glass windows

How are files different than a directory?

A directory is an area on the computer containing other directories and files and helps keep the computer organized. Files can be contained within a directory and contain information used by the operating system or other programs on the computer. See our file page for further information on files.

Does a directory have an extension?

No. A directory does not have an extension like a file.

Источник

Linux Directory Structure Explained for Beginners

If you are even faintly acquainted with Linux, you might have heard the terms root, lib, bin etc. These are various directories that you’ll find in all Linux distributions.

In fact, the Linux Foundation maintains a Filesystem Hierarchy Standard (FHS). This FHS defines the directory structure and the content/purpose of the directories in Linux distributions. Thanks to this FHS, you’ll find the same directory structure in (almost) all the Linux distributions.

Let’s see the Linux directory structure in detail.

Linux directory structure

Linux is based on UNIX and hence it borrows its filesystem hierarchy from UNIX. You’ll fine a similar directory structure in UNIX-like operating systems such as BSD and macOS. I’ll be using the term Linux hereafter instead of UNIX though.

/ – The root directory

Everything, all the files and directories, in Linux are located under ‘root’ represented by ‘/’. If you look at the directory structure, you’ll realize that it is similar to a plant’s root.

Linux Directory Structure

Since all other directories or files are descended from root, the absolute path of any file is traversed through root. For example, if you have a file in /home/user/documents, you can guess that the directory structure goes from root->home->user->documents.

The cruel rm -rf / joke

You may have come across some jokes on internet that mentions “rm -rf /” . rm command is used for removing files and directories in Linux.

With rm -rf /, you ask your system to forcefully and recursively delete the contents of the root directory. Since root directory has everything underneath, you end up deleting everything and your Linux system just vanishes (theoretically).

Most Linux distribution won’t run this command unless you provide –no-preserve-root. In any case, don’t be curious to run this command. Curiosity killed the cat, after all.

/bin – Binaries

The ‘/bin’ directly contains the executable files of many basic shell commands like ls, cp, cd etc. Mostly the programs are in binary format here and accessible by all the users in the Linux system.

/dev – Device files

This directory only contains special files, including those relating to the devices. These are virtual files, not physically on the disk.

Some interesting examples of these files are:

  • /dev/null: can be sent to destroy any file or string
  • /dev/zero: contains an infinite sequence of 0
  • /dev/random: contains an infinite sequence of random values

/etc – Configuration files

The /etc directory contains the core configuration files of the system, use primarily by the administrator and services, such as the password file and networking files.

If you need to make changes in system configuration (for example changing the hostname), this is where you’ll find the respective files.

/usr – User binaries and program data

in ‘/usr’ go all the executable files, libraries, source of most of the system programs. For this reason, most of the files contained therein is read­only (for the normal user)

  • ‘/usr/bin’ contains basic user commands
  • ‘/usr/sbin’ contains additional commands for the administrator
  • ‘/usr/lib’ contains the system libraries
  • ‘/usr/share’ contains documentation or common to all libraries, for example ‘/usr/share/man’ contains the text of the manpage

/home – User personal data

Home directory contains personal directories for the users. The home directory contains the user data and user-specific configuration files. As a user, you’ll put your personal files, notes, programs etc in your home directory.

When you create a user on your Linux system, it’s a general practice to create a home directory for the user. Suppose your Linux system has two users, Alice and Bob. They’ll have a home directory of their own at locations /home/alice and /home/bob.

Do note that Bob won’t have access to /home/alice and vice versa. That makes sense because only the user should have access to his/her home. You may read about file permissions in Linux to know more on this topic.

/lib – Shared libraries

Libraries are basically codes that can be used by the executable binaries. The /lib directory holds the libraries needed by the binaries in /bin and /sbin directories.

Libraries needed by the binaries in the /usr/bin and /usr/sbin are located in the directory /usr/lib.

/sbin – System binaries

This is similar to the /bin directory. The only difference is that is contains the binaries that can only be run by root or a sudo user. You can think of the ‘s’ in ‘sbin’ as super or sudo.

Читайте также:  Скринсейверы для windows 10 как отключить

/tmp – Temporary files

As the name suggests, this directory holds temporary files. Many applications use this directory to store temporary files. Even you can use directory to store temporary files.

But do note that the contains of the /tmp directories are deleted when your system restarts. Some Linux system also delete files old files automatically so don’ store anything important here.

/var – Variable data files

Var, short for variable, is where programs store runtime information like system logging, user tracking, caches, and other files that system programs create and manage.

The files stored here are NOT cleaned automatically and hence it provides a good place for system administrators to look for information about their system behavior. For example, if you want to check the login history in your Linux system, just check the content of the file in /var/log/wtmp.

/boot – Boot files

The ‘/boot’ directory contains the files of the kernel and boot image, in addition to LILO and Grub. It is often advisable that the directory resides in a partition at the beginning of the disc.

/proc – Process and kernel files

The ‘/proc’ directory contains the information about currently running processes and kernel parameters. The content of the proc directory is used by a number of tools to get runtime system information.

For example, if you want to check processor information in Linux, you can simply refer to the file /proc/cpuinfo. You want to check memory usage of your Linux system, just look at the content of /proc/meminfo file.

/opt – Optional software

Traditionally, the /opt directory is used for installing/storing the files of third-party applications that are not available from the distribution’s repository.

The normal practice is to keep the software code in opt and then link the binary file in the /bin directory so that all the users can run it.

/root – The home directory of the root

There is /root directory as well and it works as the home directory of the root user. So instead of /home/root, the home of root is located at /root. Do not confuse it with the root directory (/).

/media – Mount point for removable media

When you connect a removable media such as USB disk, SD card or DVD, a directory is automatically created under the /media directory for them. You can access the content of the removable media from this directory.

/mnt – Mount directory

This is similar to the /media directory but instead of automatically mounting the removable media, mnt is used by system administrators to manually mount a filesystem.

/srv – Service data

The /srv directory contains data for services provided by the system. For example, if you run a HTTP server, it’s a good practice to store the website data in the /srv directory.

I think this much information is enough for you to understand the Linux directory structure and its usage.

In the end, if you want, you can download and save this image for quick reference to the directory structure in Linux systems.

Linux System Directories

Источник

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:

Читайте также:  Сброс пароля windows утилита linux

List files in another directory

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

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.

Источник

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