List all files in linux terminal

20 Basic ‘ls’ Command Examples in Linux

Listing files is one of the most widely undertaken tasks by ordinary Linux users and system administrators. In Linux, the ls command, short for “list” is used to list or display the contents of a directory.

This could either be your current directory or any other directory on the system. The command displays both files and subdirectories and in most cases distinguishes between different file types using color codes.

Without any command-line options, the ls command will simply list all the directory contents. However, it provides an array of handy command-line options to manipulate the output and display the desired output.

You might also like:

In this article, we’ll be discussing the basics of ls command examples with all the available various command options that it provides in Linux.

ls Command Options in Linux

The ls command takes the following syntax:

$ ls [ options ] /path/to/directory

The options section represents the command-line arguments that can be passed to manipulate the output of the command.

In this tutorial, we will cover the following ls command arguments.

Options Description
ls -m Lists directory contents separated by a comma.
ls -Q Displays directory contents enclosed by quotation marks.
ls -l Displays files in a long-list format.
ls -lh Display file size in a human-readable format.
ls -g Omits group ownership column.
ls -F Adds a forward slash to directories.
ls -i Display inode number of files and directories.
ls -a Display all files including hidden files.
ls *. Filters files according to the file extension.
ls -la Displays all files and directories in long list format.
ls -R Display files and directories recursively.
ls -r Sort Files in reverse.
ls -X Sort files alphabetically by file extension.
ls -tl Display files according to file creation date and time.
ls -n List UIDs and GIDs.

1. List Files and Directories in Linux

Running ls command without passing any command-line options or arguments, the ls command simply lists the directory contents in alphabetical order. Here we won’t be able to view details like file types, size, modified date and time, permission and links, etc.

List Directory Contents in Linux

2. Long Listing of Files in Linux

The -l command option lets you print out detailed information about the directory contents in a columnar format that includes size, modified date and time, file or directory name and owner of the file, and its permission.

Читайте также:  Linux ppk to pub

Long Listinig of Files in Linux

Starting from the far left, we have:

  • 1st column – File/directory permissions.
  • 2nd column – Number of links.
  • 3rd column – Name of the owner.
  • 4th column – Name of the group that the file belongs to.
  • 5th column – File size in bytes.
  • 6th column to 8th column – Last modification date.
  • 9th column – File / Directory name.

3. List Hidden Files and Directories

Hidden files are special files that store user settings and configuration files, which are used by running programs and services for reading and storing information.

For example. the .bashrc file is a script that contains user settings and configurations of the currently logged-in user, which include command aliases, shell history, the coloring of the terminal font, etc.

The .bash_logout file is executed when you log out of your bash sessions. It’s mainly used for cleanup purposes i.e. carrying out any operations that need to be performed once you exit the bash shell.

To list hidden files, pass the -a option as shown, which displays both hidden files and directories.

List Hidden Files on Linux

4. List All Files in Linux

As you have noticed the -a option not only lists hidden files but all the files and directories. For better viewing, you can use the -la option

List All Files in Linux

5. Display File Size in a Human-readable Format

To present the output in a better format, add the -h flag to print the file size in a human-readable format. From the output, the file size is displayed in Kilobytes, Megabytes, and Gigabytes. By all means, this looks more presentable.

Linux ls Command File Size Formatting

6. Distinguish Directories and Files in Linux

When running the ls command, it’s not always easy to make a clear distinction between files and directories. The -F option adds a forward slash (/) to directories, making it easier for them to stand out from the rest of the files.

Distinguish Directories and Files in Linux

7. Sorting Files in Reverse Order

By default, the ls command sorts files and directories alphabetically (From A – Z). You can opt to sort the directory contents in reverse order using the -r option.

Sort Files in Reverse Order in Linux

In addition, you can sort the file extensions alphabetically using the -X flag.

8. List Files Recursively in Linux

The -R flag lists files recursively. First, the command lists all the files and directories in your current directory, then proceeds to display files contained in individual directories and subdirectories.

In the following example, the files in individual directories have been listed as well.

List Files Recursively in Linux

9. Sort Files By Modification Time in Linux

The ls -ltr command shows the files in the long listing format in reverse sorted by modification time, which means it will display detailed information about each file or directory in reverse order based on their last modified date/time stamp.

Sort Files By Modification Time in Linux

10. Sort Files By Newest to Oldest in Linux

You can sort files by time and date using the -t option, which sorts the files in order starting from the newest to the oldest.

Sort Files By Newest in Linux

11. Sort Files by File Size in Linux

With a combination of -lS option, it will displays file size in descending order (biggest to smallest in size).

List Files By Size in Linux

12. List File Inode Number in Linux

You can display the files and directories’ inode numbers using the -i option as shown.

Print File Inode Number in Linux

13. List Files and Directories Separated by Commas

The -m flag lists the directory contents one after the other separated by a comma.

Читайте также:  Apache ftp server linux

With the -Q flag, all the directory contents are enclosed by double quotation marks as shown.

List Files Separated by Commas in Linux

14. Omit Group Ownership in a Long-List Format

When used with the -l command option, the ls command prints both user and group ownership of the file. You can opt to omit the group column by passing the -g option.

List Files Without Group Owner in Linux

15. List Specific File Types or Extensions

To list specific file types or extensions, use the wildcard notation (*) followed by the file extension.

For example, to display all files with a .jpg extension, run the command:

Similarly, to list all PDF files, run the command:

List Files By Extension in Linux

16. List the UID and GID of Files

To display the UID and GID of files and directories, use the -n option as shown.

Print UID and GID of Files

17. Check ls Command Version

If you are a little curious and want to check the version of the ls command, you can do so as follows:

From the output, you can see that we are running ls version 9.1.

Check ls Command Version

18. Show ls Command Help Page

The ls program provides a wealth of command-line options. What we have covered are just some of the commonly used ones. For a comprehensive list of all the command options, run the following command:

View ls Command Help

Optionally, you can visit the man pages by running:

ls Command Manual

19. List Directory Information in Linux

With the ls -l command list files under directory /tmp. Wherein with -ld options, it will display information of the /tmp directory.

Print Directory Info in Linux

20. Create ls Command Aliase

We have made an alias for the ls command when we execute the ls command it will take the -l option by default and display a long listing as mentioned earlier.

To view a number of aliases available in your system, use the below alias command and the same can be unalias as shown below example.

To remove an alias previously defined, just use the unalias command.

Create ls Command Alias

In this guide, we have demonstrated how to use the ls command to view the contents of a folder or directory. In addition, we went a step further and explored command options that can be used with the ls command in Linux.

In our next article, we’ll cover more advanced ls commands with their examples. Also, I suggest you go through some interview questions on the ls command, and also if we’ve missed anything in the list, please update us via the comment section.

Источник

Get a list of all files in folder and sub-folder in a file

How do I get a list of all files in a folder, including all the files within all the subfolders and put the output in a file?

7 Answers 7

You can do this on command line, using the -R switch (recursive) and then piping the output to a file thus:

this will make a file called filename1 in the current directory, containing a full directory listing of the current directory and all of the sub-directories under it.

You can list directories other than the current one by specifying the full path eg:

will list everything in and under /var and put the results in a file in the current directory called filename2. This works on directories owned by another user including root as long as you have read access for the directories.

You can also list directories you don’t have access to such as /root with the use of the sudo command. eg:

sudo ls -R /root > filename3 

Would list everything in /root, putting the results in a file called filename3 in the current directory. Since most Ubuntu systems have nothing in this directory filename3 will not contain anything, but it would work if it did.

Читайте также:  Running command in background in linux

Maybe telling the person to cd into the directory first could be added to answer.Also this works fine if i own the directory but if trying in a directory say owned by root it didnt.I got the usual permission denied and sudo followed by your command also gave permission denied. IS there a work around without logging in as root?

Well I did say «current» directory. The correct use of CD might the subject of another question, and I’m sure it has been. You can list directories owned by root as long as you have read access to them. Directories owned by root to which the user has read access can be listed with ls -R. It’s hard to imagine why you’d want to list directories owned by root to which you don’t have read access, but sudo does indeed work if you give the full path. I’m adding examples for both of these, but excluding the use of CD.

Just use the find command with the directory name. For example to see the files and all files within folders in your home directory, use

Also check find GNU info page by using info find command in a terminal.

This is the most powerful approach. find has many parameters to customize output format and file selection.

That’s the best approach in my opinion. Simple and practical. Could also do $ find . > output if there’s many directories.

tree

An alternative to recursive ls is the command line tool tree that comes with quite a lot of options to customize the format of the output diplayed. See the manpage for tree for all options.

will give you the same as tree using other characters for the lines.

to display hidden files too

  1. Go to the folder you want to get a content list from.
  2. Select the files you want in your list ( Ctrl + A if you want the entire folder).
  3. Copy the content with Ctrl + C .
  4. Open gedit and paste the content using Ctrl + V . It will be pasted as a list and you can then save the file.

This method will not include subfolder, content though.

You could also use the GUI counterpart to Takkat’s tree suggestion which is Baobab. It is used to view folders and subfolders, often for the purpose of analysing disk usage. You may have it installed already if you are using a GNOME desktop (it is often called disk usage analyser).

sudo apt-get install baobab 

You can select a folder and also view all its subfolders, while also getting the sizes of the folders and their contents as the screenshot below shows. You just click the small down arrow to view a subfolder within a folder. It is very useful for gaining a quick insight into what you’ve got in your folders and can produce viewable lists, but at the present moment it cannot export them to file. It has been requested as a feature, however, at Launchpad. You can even use it to view the root filesystem if you use gksudo baobab .

(You can also get a list of files with their sizes by using ls -shR ~/myfolder and then export that to file.)

Источник

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