How to list files and folders in Linux


THE ls The Linux command is used to list files and directories in the current directory. When you type ls and press enter in the terminal, it will show all the files and folders in the directory you are currently in. It is a vital command that helps users navigate and manage their file system by providing an overview of the contents.

You can also use various options with ls command to view additional details, such as file permissions, ownership, file size, and modification dates. In this post we explore all the possible uses of ls command.

General syntax for ls command:


$ ls [OPTION...] [FILE...]
1. View hidden files
ls -a

This option will display the entire directory listing, including hidden files. Hidden files start with a dot (.)

Example:

.  ..  .config  .bashrc  file1.txt  folder1  .hiddenfolder  picture.jpg

In this example, the single dot (.) represents the current directory and the double period (..) represents the root directory. THE .config AND .bashrc files, along with .hiddenfolderthey are hidden files and directories that would not be shown in the clear ls command but they appear here because of the -a option. The other items are normal, non-hidden files and directories.

2. Sorting by file size
ls -S

Use the ls -S command to sort files and directories by size in descending order, then print them to the terminal.

Example:

file_large.mp4
image1.jpg
document.pdf
textfile.txt
folder1/
folder2/

In this example, file_large.mp4 is the largest file and textfile.txt is the smallest file. The directories folder1/ AND folder2/ are also listed, but the -S the option does not consider their size when sorting. If you want to see the dimensions together with the files, you can combine the files -S option with the -l option, like ls -lS.

3. View long format files
ls -l

THE -l The option displays the contents of the directory in more detail. View the file’s owner and group, last modified time, and more.

Example:

total 48
drwxr-xr-x 5 user user 4096 Aug  6 10:30 Documents
-rw-r--r-- 1 user user  123 Aug  6 10:20 file.txt
drwxr-xr-x 2 user user 4096 Aug  6 10:15 Music
-rwxr-xr-x 1 user user 2048 Aug  6 10:10 script.sh
drwxr-xr-x 3 user user 4096 Aug  6 10:05 Pictures

Here is a breakdown of what each part means:

  • drwxr-xr-x: file permissions (for example, d indicates a directory, rwx means read, write and execute permissions for the owner).
  • 5: The number of hard links to the file or directory.
  • user: the owner of the file or directory (listed twice, once for the owner and once for the group).
  • 4096: the size of the file in bytes.
  • Aug 6 10:30: The date and time the file or directory was last modified.
  • Documents: the name of the file or directory.
4. Sort by date and time
ls -t

This command sorts files by last modified time. The most recently modified files will appear at the top of the output, making them easy to find.

Example:

report.txt  image.png  project/  notes.docx  old_data.csv

In this example, report.txt is the most recently modified file, while old_data.csv he is the oldest. If you were to run the command in a different directory or at a different time, the output would vary based on the files and modification times in that specific directory.

5. View directories only
ls -d */

Use this command to list subdirectories excluding all other files.

Example:

Here’s sample output for the command, assuming you have three directories named Documents, PicturesAND Music in the current directory:

Documents/  Pictures/  Music/

Each directory name is followed by a slash (/), indicating that it is a directory.

6. List the files and save the results to a file
ls > [filename]

THE ls > [filename] The command saves the output of the previous command to a file.

Example:

The command ls > filename.txt it doesn’t display any output in the terminal. Instead, it redirects the file’s output ls command in a file named filename.txt.

If I were to run the ls command in a directory containing files and folders, the names of those files and folders will be written filename.txt. You wouldn’t see anything in the terminal itself.

Here’s an example of what might be inside filename.txt if the directory contained three files and one folder:

file1.txt
file2.jpg
folder1
file3.pdf

The exact contents would depend on the files and directories present in the current directory where the command was run.

7. List file owners with their ID
ls -n

This option displays the owner and group as a UID and GID.

Example:

drwxr-xr-x 2 1001 1001 4096 Apr  1 12:34 directory1
-rw-r--r-- 1 1001 1001    0 Apr  1 12:34 file1.txt
-rwxr-xr-x 1 1002 1002  123 Apr  1 12:34 script.sh

In this example, the first column shows the file permissions, the second column shows the number of hard links, the third and fourth columns show the user and group IDs, the fifth column shows the file size in bytes, and the sixth and seventh column shows the date and time of the last modification. The last column shows the file or directory name.

Other Linux commands:



Source link

Related Posts

Leave a Reply

Open chat
Scan the code
Hello 👋
Can we help you?