How to Run “ls” Command in Linux?

“ls” is a basic command that any Linux user should know. The ls command displays basic information about files and folders inside the file system. It’s included in the GNU core utility kit which comes standard with all Linux distributions.

Syntax:

$ ls [ option ] [ file_name ]

Use “ls” Command:

List All Files

To see all the files and folders, run “ls” without any options or arguments.

$ ls

Display Current Directory:

$ pwd displays the current working directory.

$ pwd

“ls” Command with Different Options:

List Root Directory:

$ ls /

Display Directory:

To display the specific directory, pass the directory name as an argument with the “ls” command.

$ ls /Directory_name
$ ls /home

$ ls /home /home/user

It will display all the subdirectories/ files.

Display Files in Long List Format:

$ -l (Lowercase L) gives detailed information of all files and directories.

$ ls –l

It prints the files and directories with all information.

drwxr-xr-x: file access.
2: number of links to file.
User: File owner
User: File group
4096: File size
Mar 6 06:39: Date and time
Desktop: File/Folder name

Display Specific File or Directory Content:

To display specific Directory or file information, pass the argument with the –l option.

$ ls –l /filename

Display Hidden Files:

In Linux, a file that begins with Dot(.) are hidden files.

To display the hidden files, Use -a option with “ls”:

$ ls -a

Display the Content of Hidden Files:

Use a combination of –a and -l to display the content of all hidden files. (Files with. Dot is hidden).

$ ls -al

Display Files in Reverse Order:

User –r option to display the files in reverse order:

$ ls -r

Display Most Recent Modified Files and Directories:

-t options sort the files according to their modification dates. Recently, modified files will be displayed on top.

$ ls -t

Display the Latest Modified Files:

To look at the contents of recently edited files and directories in reverse order, use the –r option.

$ ls -ltr

Display Files in Human-readable Order:

Use -h option to present the files in a readable format for humans.

$ ls -h

Display the Long List Format in Human-readable Format

Use –l and –h options to display the size of files in a human-readable format:

$ls –l -h

Display the Files Alphabetically by Extension:

Use the -x option to display the files or directories alphabetically by extension.

$ ls -x

Display Files Size:

Use the –s option with the “ls” command to see the size of files and folders:

$ ls -s

Sort by Size:

The -lS option sorts files and directories by their size. The larger files will be displayed first followed by smaller files.

$ ls -lS

Display UID and GID:

To display the (User ID) and (Group ID) of files and folder. Use the option –n with “ls” command:

$ ls -n

Append Character with Files:

Use –F option with “ls” command to add character at the end with each directory.

$ ls -F

Check ls Version:

To check the version of the “ls” command, use the –version as a parameter.

$ ls --version

Display Files and Directories Recursively:

The -R option shows the files recursively. This means we will list folders and their contents recursively which will take us to the deepest path.

$ ls -R

Add “/” Character with All Directories:

-p option append “/” character with each directory.

$ ls -p

Display the Last Edited File:

To print the latest modified file in a directory user, use head with “ls” command:

$ ls –t | head -1

Display the Oldest File:

To show the oldest file in a directory combine tail with “ls”.

$ ls –t | tail -1

Display Files Inode Number:

Use –I option to print the files/Directories in inode number. As you can see, the files’ name is preceded by a number

$ ls -i

Display One File/Directory in One Line:

We may set out that only one file is specified in a single line. This is commonly used to include a file list as an input to other commands or programs.

$ ls -1

Conclusion:

The “ls” command can display a variety of details about the files and sort them out using a variety of options and perform recursive listing. In this article, we have executed the “ls” command with different options.



from Linux Hint https://ift.tt/3uDeJ6K

Post a Comment

0 Comments