Pages

Wednesday, December 16, 2015

ls command one liners


Display All Information about Files/Directories Using ls -l
# ls -l
-rw-r----- 1 samual team-dev 9275204 Jun 13 15:27 sample.txt
Let’s split above output in parts as below,
- à Field 0
rw-r-----  à Field 1
1 à Field 2
samual à Field 3
team-dev à Field 4
9275204 à Field 5
Jun 13 15:27 à Field 6
sample.txt à Field 7

Description of each field can be understood as below,

Field 0:- File Type
Possible values are as below,
 - normal file
 d directory
 s socket file
 l link file

Field 1:- File Permissions
9 character specifies the files permission. Each 3 characters refers to the read, write, execute permissions for user, group and world/others
Example
-rw-r à indicates read-write permission for user, read permission for group, and no permission for others

Field 2:- Number of links
This field specifies the number of links for that file
Example
1 indicates only one link to this file

Field 3:- Owner
This field specifies owner of the file
Example
File in example is owned by username ‘samual’

Field 4:- Group
This field specifies the group of the file
Example
This file belongs to ‘team-dev’ group

Field 5:- Size
This field specifies the size of file
Example
’9275204' indicates the file size in bytes

Field 6:- Last modified date & time
This field specifies the date and time of the last modification of the file
Example
‘Jun 13 15:27’ specifies the last modification time of the file.

Field 7:- File name
This last field is the name of the file
Example
The file name is sample.txt

Display File Size in Human Readable Format Using ls -lh
# ls -lh

Display Directory Information Using ls -ld
# ls -ld /etc

Order Files Based on Last Modified Time Using ls -lt
# ls -lt

Order files based on last modified time (In Reverse Order) using ls -ltr
# ls -ltr

Display Hidden Files Using ls -a (or) ls -A
This will display all hidden files in Unix starts with ‘.’ in its file name including the ‘.’ (Current directory) and ‘..’ (Parent directory).
# ls -a

Display Files Recursively Using ls -R
# ls  /etc/sysconfig/networking
# ls  -R /etc/sysconfig/networking

Display File Inode Number Using ls -i
Sometimes you may want to know the inone number of a file for internal maintenance. Use -i option as shown below to display inone number. Using inode number you can remove files that has special characters in it’s name
# ls -i /etc/xinetd.d/

Hide Control Characters Using ls -q
To print question mark instead of the non-graphics control characters use the -q option
# ls -q

Display File UID and GID Using ls -n
This will display the UID in place of user and GID in place of group as described in the field description in ls -l command output
# ls -l ~/.bash_profile
# ls -n ~/.bash_profile

Visual Classification of Files with Special Characters Using ls -F
# ls -F

Visual Classification of Files with colours using ls -F
# ls --color=auto
Note:-
Recognizing the file type by the colour in which it gets displayed is an another kind in classification of file
-          Directories get displayed in blue
-          Soft links get displayed in green
-          Ordinary files gets displayed in default colour

To see size for the files present in one directory passed as argument
# ls -s folder

Useful ls Command Aliases
-          Long list the file with size in human understandable form.
alias ll="ls -lh"
-          Classify the file type by appending special characters.
alias lv="ls -F"
-          Classify the file type by both color and special character.
alias ls="ls -F --color=auto"

Back To Top
Home

No comments:

Post a Comment