unix - What is the modification date ls reports for directories in Bash shell?

09
2014-02
  • Nagel

    When I run ls -l in Bash shell, it lists a modification time for both files and directories. As noted in this thread, the modification time for directories does not reflect all of its contents. It also does not seem to be affected by changing the name of the directory. What exactly does the modification time for a directory reflect?

    Is it the latest modification time of a file (or directory) in the top level of the directory?

  • Answers
  • Dolda2000

    Note that, in Unix, a directory does not "contain" the files in it. Rather, it contains links to them. See the link(2) system call for further information.

    This means that the "direct" contents of a directory is a list of filenames and corresponding i-numbers. The modification date of the directory, therefore, indicates whenever this list changed. Operations that would cause such changes would include, but not necessarily be limited to, the following:

    • Creating a new file in the directory
    • Removing (or, rather, unlink(2)ing) a file from the directory
    • Renaming a file in the directory
    • Hard-linking a file elsewhere into the directory
  • user32019

    Think of directory as of simple file containing list of other files. Whenever you change its contents (rename files, add or remove contained files) the modification time of a directory changes.


  • Related Question

    linux - Bash Shell Script with Menu
  • rjdelight

    I've written a bash shell script (code provided below) that gives the user 4 options. However I'm having a little trouble with the code. Right now when they select option 3, to show the date. It loops over and over again. I have to close the terminal window to stop it because it's an infinite loop. How would I prevent this? Also quit doesn't seem to be working either.

    If someone could help me out a bit, thanks.

    #!/bin/bashe
     echo -n "Name please? "
     read name
     echo "Menu for $name
        1. Display a long listing of the current directory
        2. Display who is logged on the system
        3. Display the current date and time
        4. Quit "
    read input
    input1="1"
    input2="2"
    input3=$(date)
    input4=$(exit)
    while [ "$input" = "3" ]
    do
    echo "Current date and time: $input3"
    done
    
    while [ "$input" = "4" ]
    do
    echo "Goodbye $input4"
    done
    

  • Related Answers
  • glenn jackman

    A compact version:

    options=(
        "Display a long listing of the current directory"
        "Display who is logged on the system"
        "Display the current date and time"
        "Quit" 
    )
    
    select option in "${options[@]}"; do
        case "$REPLY" in 
            1) ls -l ;;
            2) who ;;
            3) date ;;
            4) break ;;
        esac
    done
    
  • bmk

    I guess it's due to the while loop ;)

    The value of $input doesn't change inside of the loop. Either you insert something like a read input into the loop or you change the while ... do into a if ... then. If I understand the intention of your script correctly you don't need those while loops where they are now. You need one big while loop covering everything from echo "Menu for $name till the end.

    My version of the script would be:

    #!/bin/bash
    echo -n "Name please? "
    read name
    input=""
    while [ "$input" != "4" ]; do
      echo "Menu for $name
        1. Display a long listing of the current directory
        2. Display who is logged on the system
        3. Display the current date and time
        4. Quit "
      read input
      input1="1"
      input2="2"
      input3=$(date)
      input4=$(exit)
      if [ "$input" = "3" ]; then
        echo "Current date and time: $input3"
      fi
    
      if [ "$input" = "4" ]; then
        echo "Goodbye $input4"
      fi
    done
    
  • Ranjithkumar T

    case is here to help you, try

     # cat list.sh
     #!/bin/bash
     echo -n "Name please? "
     read name
     echo "Menu for $name"
     echo -e "Enter ( 1 ) to Display a long listing of the current directory \nEnter ( 2 )             to Display who is logged on the system\nEnter ( 3 ) to Display the current date and     time\nEnter ( 4 ) to Quit"
     read en
     case "$en" in
     1)ls -lR;;
     2)who;;
     3)date;;
     4)echo "Good Bye..."
     sleep 0;;
     *)echo "Wrong Option selected" ;;
     esac
    

    Output:

     # ./list.sh
     Name please? Ranjith
     Menu for Ranjith
     Enter ( 1 ) to Display a long listing of the current directory
     Enter ( 2 ) to Display who is logged on the system
     Enter ( 3 ) to Display the current date and time
     Enter ( 4 ) to Quit
    

    If you enter other options then the menu shows, then

     # ./list.sh
     Name please? Ranjith
     Menu for Ranjith
     Enter ( 1 ) to Display a long listing of the current directory
     Enter ( 2 ) to Display who is logged on the system
     Enter ( 3 ) to Display the current date and time
     Enter ( 4 ) to Quit
     5
     Wrong Option selected
    

    ...