Friday 27 July 2018

ls Command in Unix and Linux Examples

ls Command, Linux Command, Unix Command, LPI Study Materials

ls is the most widely used command in unix or linux. ls command is used to list the contents of a directory. Learn the power of ls command to make your life easy. The syntax of ls command is

ls [options] [pathnames]


1. Write a unix/linux ls command to display the hidden files and directories?


To display the hidden files and directories in the current directory use the -a option of the ls command.

> ls -a
.  ..  documents  .hidden_file  sum.pl

Hidden files are the one whose name starts with dot (.). The las -a displays the current directory (.) and parent directory (..) also. If you want to exclude the current directory, parent directory, then use -A option.

> ls -A
documents  .hidden_file  sum.pl

2. Write a unix/linux ls command to classify the files with special characters


The -F option to ls command classifies the files. It marks the

◈ Directories with trailing slash (/)
◈ Executable files with trailing asterisk (*)
◈ FIFOs with trailing vertical bar (|)
◈ Symbolic links with trailing at the rate sign (@)
◈ Regular files with nothing

> ls -F
documents/  sum.pl link@

3. Write a unix/linux ls command to print each file in a separate line?


The -1 option to the ls command specifies that each file should be displayed on a separate line

> ls -1
documents
sum.pl

4. Write a unix/linux ls command to display the inode number of file?


In some cases, you want to know the inode number of a file. Use -i option to the ls command to print the inode number of a file.

> ls -i1
10584066 documents
3482450 sum.pl

5. Write a unix/linux ls command to display complete information about the files?


The -l option provides lots of information about the file type, owner, group, permissions, file size, last modification date.

> ls -l
total 16
drwxr-xr-x 2 matt db 4096 Jan 30 23:08 documents
-rw-r--r-- 1 matt db   49 Jan 31 01:17 sum.pl

◈ The first character indicates the type of the file. - for normal file, d for directory, l for link file and s for socket file
◈ The next 9 characters in the first field represent the permissions. Each 3 characters refers the read (r), write (w), execute (x) permissions on owner, group and others. - means no permission.
◈ The second field indicates the number of links to that file.
◈ The third field indicates the owner name.
◈ The fourth field indicates the group name.
◈ The fifth field represents the file size in bytes.
◈ The sixth field represents the last modification date and time of the file.
◈ And finally the seventh field is the name of the file.

6. Write a unix/linux ls command to sort the files by their modification time?


The -t option allows the ls command to sort the files in descending order based on the modification time.

> ls -t1
sum.pl
documents

7. Write a unix/linux ls command to sort the files in ascending order of modification time?


The -r option reverses the order of the files displayed. Combine the -t and -r options to sort the files in ascending order.

> ls -rt1
documents
sum.pl

8. Write a unix/linux ls command to print the files recursively?


So far the ls command prints the files in the current directory. Use the -R option to recursively print the files in the sub-directories also.

> ls -R
.:
documents  sum.pl

./documents:
file.txt

9. Write a unix/linux ls command to print the files in a specific directory?


You can pass a directory to the ls command as an argument to print for the files in it.

> ls /usr/local/bin

10. Write a unix/linux ls command to display files in columns?


The -x option specifies the ls command to display the files in columns.

> ls -x

Related Posts

0 comments:

Post a Comment