28 April 2013

Some LS Command Practical Examples

46 comments


ls command is one of the most frequently used command during a Linux Session. Here are some ls command practical examples to make your Linux Command line experience more enjoyable and faster. Iam assuming that you know basic ls usage like ls -l, ls -a, ls -R etc.


Trick 1:

List Only directories in a Current Directory using ls -d */

$ ls -d */
bootstrap/        Documents/     Music/      sandeep/      VirtualBox VMs/

Trick 2:

List Only hidden directories in a Current Directory using ls -d .*/

$ ls -d .*/
./             .gimp-2.8/          .mplayer/          .ssh/ 
../            .gnome2/            .mysql/            .subversion/ 
.adobe/        .gnome2_private/    .nbi/              .swt/ 

Trick 3:

Display file size in human readable format using -h switch.

$ ls -lh
total 264K
drwxr-xr-x  5 rajkumar rajkumar 4.0K Jan 20 14:54 bootstrap
drwxrwxr-x  7 rajkumar rajkumar 4.0K Mar 29 13:15 Calibre Library
-rw-rw-r--  1 rajkumar rajkumar 4.8K Apr 20 17:36 C:\nppdf32Log\debuglog.txt
drwxrwxr-x  4 rajkumar rajkumar 4.0K Mar 30 22:08 css3
drwxr-xr-x  5 rajkumar rajkumar 4.0K Apr 28 19:12 Desktop

Trick 4:

Sort files according to the modification time using -t switch.

$ vim four.sh
$ ls -lt
total 264
-rw-rw-r--  1 rajkumar rajkumar     0 Apr 28 19:51 four.sh
drwxr-xr-x  5 rajkumar rajkumar  4096 Apr 28 19:12 Desktop
drwxrwxrwx 37 rajkumar rajkumar 20480 Apr 24 20:07 Downloads

Note: In above example we have just modified four.sh, so it was displayed on the top of the list.

Note: To open last edited file with vim, type following command in terminal

$ vim `ls -t | head -1` 

Trick 5:

List Files according to their size in descending using -S switch.

$ ls -lSh
total 264K
-rw-rw-r--  1 rajkumar rajkumar  64K Apr  8 14:11 hs_err_pid3385.log
-rw-rw-r--  1 rajkumar rajkumar  62K Apr 11 21:51 hs_err_pid4197.log
-rw-rw-r--  1 rajkumar rajkumar  22K Oct 13  2012 fbcmd_update.php
drwxrwxrwx 37 rajkumar rajkumar  20K Apr 24 20:07 Downloads

Trick 6:

List Files according to their size in ascending order using -rS switch.

$ ls -lShr
total 264K
-rw-rw-r--  1 rajkumar rajkumar    0 Apr 28 19:58 four.sh
-rw-rw-r--  1 rajkumar rajkumar   25 Apr 18 12:12 spe.sh
-rwxrwxrwx  1 rajkumar rajkumar   97 Apr 18 12:08 spe1.sh
-rw-r--r--  1 rajkumar rajkumar  179 Jun 17  2012 examples.desktop
-rwxrwxrwx  1 rajkumar rajkumar  228 Mar  3 08:56 if.py

Trick 7:

Consider a case in which you have to display time in a specified format like(YYYY-MM-DD), how to do it?. You can do it by using --time-style switch. Lets see few example to understand it.

$ ls -lh --time-style=+"%Y-%B-%d"
total 264K
drwxr-xr-x  5 rajkumar rajkumar 4.0K 2013-January-20 bootstrap
drwxrwxr-x  7 rajkumar rajkumar 4.0K 2013-March-29 Calibre Library
-rw-rw-r--  1 rajkumar rajkumar 4.8K 2013-April-20 C:\nppdf32Log\debuglog.txt
drwxrwxr-x  4 rajkumar rajkumar 4.0K 2013-March-30 css3
drwxr-xr-x  5 rajkumar rajkumar 4.0K 2013-April-28 Desktop

Note: You can use any time format after + sign. Lets see another example

$ ls -lh --time-style=+"%Y-%B-%d %H:%M:%S"
total 264K
drwxr-xr-x  5 rajkumar rajkumar 4.0K 2013-January-20 14:54:29 bootstrap
drwxrwxr-x  7 rajkumar rajkumar 4.0K 2013-March-29 13:15:12 Calibre Library
-rw-rw-r--  1 rajkumar rajkumar 4.8K 2013-April-20 17:36:00 C:\nppdf32Log\debuglog.txt
drwxrwxr-x  4 rajkumar rajkumar 4.0K 2013-March-30 22:08:08 css3

Note: You can use some default option like long-iso, iso, locale etc.

$ ls -lh --time-style=long-iso
total 264K
drwxr-xr-x  5 rajkumar rajkumar 4.0K 2013-01-20 14:54 bootstrap
drwxrwxr-x  7 rajkumar rajkumar 4.0K 2013-03-29 13:15 Calibre Library
-rw-rw-r--  1 rajkumar rajkumar 4.8K 2013-04-20 17:36 C:\nppdf32Log\debuglog.txt
drwxrwxr-x  4 rajkumar rajkumar 4.0K 2013-03-30 22:08 css3
drwxr-xr-x  5 rajkumar rajkumar 4.0K 2013-04-28 19:12 Desktop

Trick 8:

Display one file per line using -1 switch.

$ ls -1
0071765654_preassess.pdf
027__06__MAY12.pdf
bash_cookbook(Oreilly).pdf
bootstrap
eclipse-php-3.0.2.v20120611144-x86.tar.gz
iiitdpdf
innovate.svg
IPV6
IPv6 and the Future of the Internet.pdf

Trick 9:

Remember THOR film? Now you are thinking that why Iam talking about film here?. If you see it carefully than you will note that it is a nice trick to remember some of the options used with ls command. Another use of above trick is that you can use it to display files in reverse order of Modification time.

$ ls -thor
total 264K
-rw-r--r--  1 rajkumar  179 Jun 17  2012 examples.desktop
drwxr-xr-x  2 rajkumar 4.0K Jun 17  2012 Music
drwxrwxr-x  2 rajkumar 4.0K Aug 25  2012 wordpress theme
drwxrwxr-x  2 rajkumar 4.0K Sep  6  2012 lectureplan
-rw-rw-r--  1 rajkumar  22K Oct 13  2012 fbcmd_update.php
drwxr-xr-x  3 rajkumar 4.0K Dec 27 18:59 Videos
drwxr-xr-x  2 rajkumar 4.0K Jan 12 23:59 Templates
drwxr-xr-x  2 rajkumar 4.0K Jan 12 23:59 Public

Note: -r - Reverse order.
-t - Modification time.
-o - Display only Owner name.
-h - Human Readable format.

That's it.
Hope you Enjoyed the post.


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

46 comments:

  1. hi, and thank you for listing these examples it helps me a lot and guide me in some topics where i am lacking behind. For other assignments tutorials visit programming assignments help
    is the best assignment help provider in the United Kingdom.
    programming assignment help
    Our Computer Programming Assignment Help
    UK is especially dedicated for the students studying in all UK colleges and universities. Submit assignment to get the best quality assignment help.
    computer network assignment help
    Object Oriented Programming
    database assignment help. Thank you again for this great blog!

    ReplyDelete
  2. I am looking for essay writing help, because I am stuck with my essay writing. My teacher assigned me an essay writing task and that should be submitted within four days. Topic can be selected by our self. I can’t make a good essay without any other’s help. So I am here to get some reviews of online
    dissertation Writing Service

    ReplyDelete
  3. Thanks for sharing your thoughts and ideas on this one. Please keep posting about such articles as do my assignment for me they really spread useful information. Thanks for this particular sharing. I hope it stays updated, take care.

    ReplyDelete
  4. You there, this is really good post here. Thanks for taking the time to post such valuable information. Buy Resident Evil 4 Online Quality content is what always gets the visitors coming

    ReplyDelete
  5. I’m going to bookmark your web page and maintain checking for new details. I am looking for some good blog sites for studying. I was searching over search engines and found your blog site. do my essay for me Well i like your high quality blog site design plus your posting abilities. Keep doing it.

    ReplyDelete
  6. I hope this is just begin of your writing. If you want you can check my blog as well. Of course if you want. You have done a great job on this article. It’s very readable and highly assignment writers uk intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you.

    ReplyDelete
  7. I hope this is just begin of your writing. If you want you can check my blog as well. Of course if you want. You have done a great job on this article. It’s very readable and highly intelligent. Tunnel Snakes Rule Jacket You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you.

    ReplyDelete
  8. There are amazing. Go for me. This line of drinks and they taste really good. I ran across your post searching for the. Victoria Secret Leather Jacket

    ReplyDelete
  9. Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but uk essay I'm most definately interested in this one. Just thought that I would post and let you know.

    ReplyDelete
  10. Fantastic and great post you have share here. I thumb up your post. You have done a good job here. help with assignment Thank you very much for sharing this with us.

    ReplyDelete
  11. But it was one sided. Tell him yes you did read it and thought it was nice. Either that or come clean with him, and tell him that you. Rdr 2 Arthur Morgan Jacket

    ReplyDelete

  12. A critique of a dissertation includes a brief summary in order to make the readers easily grasp the main thoughts and proofs of the passage that is under examination. The critique of a dissertation comes in various sizes and shapes. It is essential to carefully read the passage and make plenty of notes and ask a number of questions. It is impossible to critique something without even understanding the chosen topic. The introduction should persuasively and clearly state the thesis of the author and the arguments related to it. The introduction should provide the reader with a little background in order to make that person understand the worth of writing the dissertation critique elements.

    A brief summary should also be drafted. It can be included in the draft of the critique. Adequate transitions should be used in order to maintain a smooth flow of the writing. The presentation of the author should be analyzed thoroughly. The reader should be provided with an in-depth analysis of the logical reasoning of the author and his use of evidence. Presenting the information for easy understanding is essential, by using transitional elements whenever it is necessary for preserving the smooth flow of the writing. You should agree or disagree with the viewpoint of the author and this is where you make your own views clearly on the issue. 
    nursing dissertation help

    ReplyDelete
  13. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you! essay writing servics

    ReplyDelete
  14. I have to say that you are on the right track. This will be loved by several individuals as it is detailed and interesting. All the best for your future work.
    rta bus routes

    ReplyDelete
  15. You ought to be a part of a contest for one of the finest websites on the internet. I most certainly will highly recommend this web site! insta stalker

    ReplyDelete
  16. You have to be a part of a contest for one of the finest web sites on the internet. I most, in reality, will fairly recommend this web website! Military Style Jacket

    ReplyDelete
  17. I am very happy to read this. This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs. Appreciate your sharing this best posting. Connor Jessup Puffer Jacket The effort you made to share the knowledge. This is really a great stuff for sharing. Keep it up . Thanks for sharing.

    ReplyDelete
  18. Hi tÒ»ere the whole thing Ñ–s going well here and ofcourse every one is sharing data.
    Top Movie Jackets

    ReplyDelete
  19. I am amazed by the way you have explained things in this article. This article is quite interesting and I am looking forward to reading more of your posts. Thanks for sharing this article with us.. Dark Knight Rises Bane Coat

    ReplyDelete
  20. On that website page, you'll see your description, why not read through this.
    Tom Cruise Jacket

    ReplyDelete
  21. I found your this post while searching for information about blog-related research ... It's a good post .. keep posting and updating information.. Sons Of Anarchy Vest

    ReplyDelete
  22. Thanks for sharing such a fantastic blog. Since the last few weeks, I am not finding your blogs that disappoint me a lot, as I love reading your posts. This article is really attractive, and I’m waiting for your new ideas as well. Hope to see your next blog as soon as possible. https://technargle.com/cartooncrazy/

    ReplyDelete
  23. From this blog we can get better college base method and educational system I like to read this blog Persian runners this is very interesting and informative. If you are a teacher or a student you really need to have read this blog so you get amazing information and new method of studies.

    ReplyDelete
  24. This is a standout amongst other blog that I have at any point seen up until now , assuming you truly need to know something new, see Write My Assignment Cheap subtleties here.

    ReplyDelete
  25. Thank you for making this resource available for free. Free resources provided by blogs are highly appreciated by me. buy YouTube Views

    ReplyDelete

  26. The Guarantee That Makes Us a Reliable Online Coursework Help Website. Sowing the seeds of faith in the mind and soul of the students was not an overnight task. But, slowly and steadily, we achieved our target. We established ourselves as a trusted and credible research writing help website and have managed to win the trust of the students. Our guarantees played an important role in this battle of trust. The bond of truth was well woven by our assurance, with the aim of imparting trust.

    ReplyDelete
  27. I'm glad to be here, I'm a student and I'm in my final year I got dissertation that is related to IT and LMS command and finally I got an IT Dissertation help, the way you define each trick with example are tremendous.

    ReplyDelete
  28. a responsibility of appreciation is for sharing. what's even more other than This article gives the light in which we can see reality. this is genuinely staggering one and gives indepth information. grateful for this extraordinary article Nasa jacket

    ReplyDelete
  29. It is a fantastic post – immense clear and easy to understand BEST PDF EDITORS FOR ANDROID in this side close to my comfortable outfit

    ReplyDelete
  30. I read your article several times since you make so many excellent points. For the most part, your viewpoints are similar to mine. This is excellent material for your readers.
    Sell Garmin Watch

    ReplyDelete
  31. a commitment of appreciation is for sharing. what's all the more besides This article gives the light in which we can see reality. this is entirely staggering one and gives in-depth data. grateful for this great article mens jean jacket outfit

    ReplyDelete
  32. This is a fantastic post with lots of useful information. Thank you for sharing this information with us; it is really appreciated. This post appealed to me because it has all pertinent information and facts. Gap Brown Hoodie

    ReplyDelete
  33. Pretty cool post. It’s really very nice and useful post. Keep it up!!Resident Alien Merch

    ReplyDelete
  34. I enjoy your material since the information on the issue is quite entertaining and the description of the topic is very technical. Cruella Jacket

    ReplyDelete
  35. I'm totally agree with your opinion, no doubt UK wiriter provide the most legitimate IT Homework Writing Service in USA, they are highly skilled and well experienced to resolved any kind of academic writing related queries.

    ReplyDelete
  36. The command-line interface is an essential tool for Linux users, allowing them to navigate and interact with their systems efficiently. Among the numerous commands available, the "ls" command stands out as a common and versatile tool. In this section, we will explore the practical applications of the "ls" command and provide examples that can enhance your experience and speed up your workflow on the command line.
    Whether you are a beginner or an experienced Linux user, understanding how to effectively use the "ls" command can greatly improve your productivity. From listing files and directories to sorting and filtering results, this powerful tool offers various options that allow you to tailor your output according to your specific needs.
    By utilizing practical examples, we will demonstrate how you can harness the full potential of the "ls" command. From basic usage such as displaying file permissions and sizes, to more advanced techniques like searching for specific files or filtering results based on patterns, these examples will empower you with valuable knowledge that can be applied in real-world scenarios.
    So whether you are a student seeking global assignment help or a professional looking to optimize your Linux sessions, this section will equip you with the necessary skills to make the most out of the "ls" command. Let's dive in and unlock its capabilities together!

    ReplyDelete
  37. It's a reliable source of information. The importance of BPM Test in music cannot be overstated, as it sets the pace and mood of a composition.

    ReplyDelete