Understanding & Learning Basic Shell Scripting and Linux Filesystem Troubleshooting – Part 10

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Gabriel Cánepa

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

31 Responses

  1. BV Naagabaabu says:

    can i check a file and directory existence in a single IF condition. like below.
    if [[ -f /home/user/file_1 && -d /home/user/new_dir/ ]]

    In the above line i’m checking if the “/home/user/file_1” file is existing or not and at the same time checking “/home/user/new_dir/” directory is existing or not.

    While executing the above condition in a script, it only considering the first condition and breaking. I need to know is it possible to check both file and directory in a single condition.

    • Hi @BV Naagabaabu,
      if [[ -f /home/user/file_1 && -d /home/user/new_dir/ ]]
      if [ -f /home/user/file_1 ] && [ -d /home/user/new_dir/ ]
      or with
      if [ -f /home/user/file_1 -a -d /home/user/new_dir/ ]
      That should work if you’re using bash. If it doesn’t please confirm which shell you are using, and post the other relevant lines of code.

      • BV Naagabaabu says:

        hi Gabriel,
        Thanks for the quick reply. coming to my scenario.,

        i,’m using BASH shell. In a shell script I need to check the file and directory exists or not. if both are available then they should be deleted.

        while using the above if condition(I mentioned) my script was stopped after checking first condition(here file existence) in if condion. while debugging (with set -x) i got to know that it was not checking the directory existence.

        I will apply your fix and post my output.

        • BV Naagabaabu says:

          Hi Gabriel.,

          Tested this and came to know that the problem is not with if condition, it is with wild cards., Below is my scenario

          i have 3 files and one directory. Those 3 files are named as file_name1, file_name2, file_name3 and the directory name is logs.

          for checking those 3 files and directory existence, i write a if condition like below.

          if [[ -f /home/user/file_* && -d /home/user/logs ]]

          while executing the above, IF condition is break at first condition, because it doesn’t find the file “/home/user/file_*”.

          How can i use a wild card in if condition

      • BV Naagabaabu says:

        hi Gabriel.,
        thanks for the suggestion. after some search I came to know that we can’t use wild cards in if conditions to check similar files existence.
        I got an alternate solution using ls command. below is how it works

        ls -l /home/user/file_*

        if [ $res =0 ]
        echo “all files exists”
        echo “all files not exists”

  2. Freddy Dominguez says:

    After February 2 this year they added more articles in the series. Many will be in total to cover the LFCS ?. These were previously just 10.

  3. anis says:

    @Gabriel: will you make a update for those tutorials as Linux foundation start a new program for LFCS ?

  4. Rizal Rahman says:

    I took my LFCS exam 3 days ago, and today i got a notification that i passed it. Thank you very much for your incredible LFCS series. Really guiding people like me to know which materials that i should start learn.

  5. Chris says:

    Very useful article, Gabriel. I was wondering whether it wouldn’t be easier to use systemctl –quiet is-active $service instead of
    systemctl status $service | grep –quiet “running”?

  6. Gabriel, Well done on the 10 step tutorials for the LFCS, they are very good indeed.

  7. Minhaj says:

    these lfcs contents are awesome … I certain times use these contents in teaching my students as well and it is a great help for me to update my syllabus as well.. hope you don’t mind. after all it’s all promoting open source.

    Thank you. :)

    • Ravi Saive says:

      After all promoting open soucre? do you really know how difficult is to create such awesome articles? it takes hours or even days to create a one single article, I hope you will give credit to author and Tecmint….and don’t misuse the guides to make some cash…..

      • Minhaj says:

        yes Ravi, I do give credits to all.. and my motive is not earning money.. I always give FREE education to children apart to my instructing job.(where I do not use these contents but ask students to refer this site) and yes I know the pain in making articles,, I have been making articles, tutorials.. etc.. for past 7 years .. ;)

    • @Minhaj,
      As Ravi just said, feel free to use this series in teaching your students as long as you give credit to Tecmint (and me) as the original source of your training materials.

  8. Chris says:

    I know you show it in the example, but I wonder if it’s worth drawing some attention to the spacing in the test command.

    if [ $? -eq 0 ]; then

    There needs to be space between the comparison and the square bracket otherwise the script won’t work. I’ve managed to miss this before and then found myself scratching my head as to why it doesn’t work ;-)

  9. Thanks for this resource, it has been very useful. I have used it to prepare for my LFCS exam which I am due to sit tomorrow morning. I will let you know how it goes when I get my results
    Thanks again.

    • Minhaj says:

      hi .. what about the exam..???? can you tell us how are the questions and all.. not the exact questions .. :P ..and any other study guides where you can recommend..

      thank you ..

      • @Minhaj,
        The Linux Foundation does not allow to disclose the questions in the exam or other parts of it. This series cover the overall topics required to pass the LFCS exam and you should be fine if you study the required competencies from our articles and other sources. Other readers have shared in the comments section sources that have been helpful to them as well.

        • Minhaj says:

          Thanks @Gabriel, NO i meant the structure of the exam, but yes hopefully i’m going to face the exam by the end of this month. since there are some updates to the syllabus as I’ve heard. as a network professional or as in any other field, facing an exam is easy but as a instructor (I am :) ) you know facing an exam means a big thought even though my students gets through the exam easily … hahaha … ;)

    • @Jason,
      I hope you passed the exam. If so, feel free to post back the results :). And thanks for taking the time to comment on this article.

  10. Gaurav Heera says:

    i love to learn about linux and you blog is really an awesome place to learn linux in easy steps. today’s post was very lengthy but i have learned a lot from it. thanks for sharing.


    Gaurav Heera

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.