Linux file and directory listing - ls command
Linux command line prompt offers the command 'ls' to list the files and directories.
More information on this command can be found using the Linux manual (man ls).
Common Usages
We will now see the most common usages of this command.
1. ls -lrt
This happens to be the most common and frequent usage of the ls command. This provides a listing of all files and directories in an order sorted by file creation time.
2. ls -lrth
You see there is a 'h' appendage here. It stands for human readable form . ls command displays the file size information in bytes by default. The 'h' makes it display in MB or GB or KB.
3. ls -la
The 'a' represents all. Hidden files in linux are files which begin with a dot. The 'a' command will display all these files.
LS used in pipe stream conversions
Some commands in relation to 'ls' as a basic to aid in your daily use.
a. Produce a count of number of files and directories.
ls -l | wc -l
b. Print file names without other information.
ls
ls -lrt | rev | cut -d ' ' -f 1 | rev
c. Do some operation over list files by running a loop.
For example , here i will try to zip all files last modified or created in 2019 or even having name like 2019.
for f in `ls -lrt | grep 2019 | rev | cut -d ' ' -f 1 | rev`; do gzip "$f"; done
You can modify above example to suit your needs.
Understanding file mask permissions
A file in unix has three permissions associated with it - read (r) , write (w) and execute (x).
These permissions are defined for three different user groups :
1. Owner
2. Group
3. Others
When you execute the 'ls -l' command , you should see something like :
drwxrw-r-- (just an example)
This would mean that the file (which is a directory because of the d) , has full read ,write and execute permission for the owner. Read and write permissions for group. Read permission for others.
Comments
Post a Comment