Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

On Ubuntu, how do you get all timestamps with file backup?

That Shell scripting tutorial could use more file commands and stuff. I guess I will have to watch a video or some. I know I will use "ls -a" and some way to make it an array like thing of names in current directory. Yes I want all things recorded. Any idea how to convert "ls -a" to like an array? X E.
 
That Shell scripting tutorial could use more file commands and stuff. I guess I will have to watch a video or some. I know I will use "ls -a" and some way to make it an array like thing of names in current directory. Yes I want all things recorded. Any idea how to convert "ls -a" to like an array? X E.
So you can actually do something like this:
Bash:
#!/bin/bash

#save list of file name to variable
manifest="$(ls -p | grep -v /)"

#iterate through each file name and pass it to stat to get birth, access, modify info
while IFS= read -r line; do
    stat $line
done <<< $manifest
I called my script "test", when you run that, you should get something like this
1703885503719.png
 
First, what, you have a Linux terminal, and second, I am unsure how to make an array of stat and format it. I also would need how to write to a file and only place I found that was zzzcode.ai. Here is like my progress so far.
Bash:
#!/bin/bash

function recurse() {
    #save list of file name to variable
    manifest="$(ls -pa $1 | grep -v /)";
    echo "$(stat $1)";
    #iterate through each file name and pass it to stat to get birth, access, modify info
    while IFS= read -r line; do
        stat "$1$line"
    done <<< $manifest
}
recurse ./
#X E.
I feel like getting close but do not know how, does that second video explain stuff like this because I did not watch that yet. I should also make a thing to check if it is a folder and if not ending in / add /. You could probably do this easily. I am trying for like line by line write out what I need. I would probably be doing recursion and for each file do stat and record it with absolute path first. I am getting more advanced but unsure if someone else who knows what they are doing should be doing this. I think inode, links, accessed by, accessed time, modify time, change time, and birth time should be what to record. Basically everything. If you ever make this thing it might help a lot of Linux users. I guess I may learn from AI for now. X E.
 
Now I have this.
Bash:
#!/bin/bash
#12/29/2023, 5:59PM, started, X E.
function recurse() {
    #save list of file name to variable
    manifest="$(ls -a $1 | grep -v /)";
    echo "$(stat $1)";
    #iterate through each file name and pass it to stat to get birth, access, modify info
    while IFS= read -r line; do
        if [[ "$line" != ".." && "$line" != "." ]]; then
            if [ -d "$1$line" ]; then
                if [[ "$1$line" == */ ]]; then
                    recurse "$1$line";
                else
                    recurse "$1$line/";
                fi
                #echo "$line";
            fi
        #echo "$1$line"
        fi
    done <<< $manifest
}
if [ $# -gt 0 ]; then
    recurse "$1";
else
    recurse "$(pwd)/";
fi
#X E.
Now all I need is a formatted text for stat and writer. X E.
 
Here is like my current code.
Bash:
#!/bin/bash
#12/29/2023, 5:59PM, started, X E.
function recurseXE() {
    #save list of file name to variable
    manifest="$(ls -a $1 | grep -v /)";
    #echo "$(stat $1)";
    writeXE $1;
    #iterate through each file name and pass it to stat to get birth, access, modify info
    while IFS= read -r line; do
        if [[ "$line" != ".." && "$line" != "." ]]; then
            if [ -d "$1$line" ]; then
                if [[ "$1$line" == */ ]]; then
                    recurseXE "$1$line";
                else
                    recurseXE "$1$line/";
                fi
                #echo "$line";
            else
                writeXE $1;
            fi
        #echo "$1$line"
        fi
    done <<< $manifest
    #X E.
}
#X E.
function writeXE() {
    echo $1;
    # Store the output of the stat command in a variable
    output="$(stat $1)";
    # Use command substitution to split the output into an array
    readarray -t stat_array <<< "$output";
    # Print the array elements
    for element in "${stat_array[@]}"; do
        if [[ $element == *"Access:"* ]]; then
            echo "access";
        fi
        if [[ $element == *"Modify:"* ]]; then
            echo "modify";
        fi
        if [[ $element == *"Change:"* ]]; then
            echo "change";
        fi
        if [[ $element == *"Birth:"* ]]; then
            echo "born";
        fi
    done
    #X E
}
#X E
if [ $# -gt 0 ]; then
    if [ -d "$1" ]; then
        if [[ "$1" == */ ]]; then
            recurseXE "$1";
        else
            recurseXE "$1/";
        fi
    fi
else
    recurseXE "$(pwd)/";
fi
#X E.
What do I need to do to have this write to a file? Something like echo > file.txt, correct? I just need to know what format can I use for touch to understand later now. I can probably do everything else. X E.
 
First, what, you have a Linux terminal, and second, I am unsure how to make an array of stat and format it. I also would need how to write to a file and only place I found that was zzzcode.ai. Here is like my progress so far.
Bash:
#!/bin/bash

function recurse() {
    #save list of file name to variable
    manifest="$(ls -pa $1 | grep -v /)";
    echo "$(stat $1)";
    #iterate through each file name and pass it to stat to get birth, access, modify info
    while IFS= read -r line; do
        stat "$1$line"
    done <<< $manifest
}
recurse ./
#X E.
I feel like getting close but do not know how, does that second video explain stuff like this because I did not watch that yet. I should also make a thing to check if it is a folder and if not ending in / add /. You could probably do this easily. I am trying for like line by line write out what I need. I would probably be doing recursion and for each file do stat and record it with absolute path first. I am getting more advanced but unsure if someone else who knows what they are doing should be doing this. I think inode, links, accessed by, accessed time, modify time, change time, and birth time should be what to record. Basically everything. If you ever make this thing it might help a lot of Linux users. I guess I may learn from AI for now. X E.
1) I have ubuntu Latest stable on Windows Subsystem for Linux.
 
Now I have this.
Bash:
#!/bin/bash
#12/29/2023, 5:59PM, started, X E.
function recurse() {
    #save list of file name to variable
    manifest="$(ls -a $1 | grep -v /)";
    echo "$(stat $1)";
    #iterate through each file name and pass it to stat to get birth, access, modify info
    while IFS= read -r line; do
        if [[ "$line" != ".." && "$line" != "." ]]; then
            if [ -d "$1$line" ]; then
                if [[ "$1$line" == */ ]]; then
                    recurse "$1$line";
                else
                    recurse "$1$line/";
                fi
                #echo "$line";
            fi
        #echo "$1$line"
        fi
    done <<< $manifest
}
if [ $# -gt 0 ]; then
    recurse "$1";
else
    recurse "$(pwd)/";
fi
#X E.
Now all I need is a formatted text for stat and writer. X E.
2) you could do something like this
Bash:
#Set up array at beginning
arr=('Hello' 'World')

#add stat of files to array
arr+=("$(stat $line)")

#iterate thru
for stats in ${arr[@]}; do
    birth="$(grep 'Birth' $stats)"
    modified="$(grep 'Modify' $stats)"
    #get the date from the new variable and save to file
    printf "" >> file
 
1) I have ubuntu Latest stable on Windows Subsystem for Linux.
I could have easily popped open my Parrot Security OS box, or my linux mint boot drive, but since I was on my windows machine, I just popped wsl because laziness XD
 
Now I have this function.
Bash:
function writeXE() {
    echo $1;
    # Get the file's creation time using stat
    created_at=$(stat -c %W $1);
    # Get the timestamps using stat
    modified_at=$(stat -c %y $1)
    accessed_at=$(stat -c %x $1)
    changed_at=$(stat -c %z $1)
    # Format the creation time using the date command
    formatted_created_at=$(date -d @$created_at +"%Y-%m-%d %H:%M:%S");
    # Format the timestamps using date
    formatted_modified_at=$(date -d "$modified_at" +"%Y-%m-%d %H:%M:%S");
    formatted_accessed_at=$(date -d "$accessed_at" +"%Y-%m-%d %H:%M:%S");
    formatted_changed_at=$(date -d "$changed_at" +"%Y-%m-%d %H:%M:%S");
    echo "$formatted_created_at, $formatted_modified_at, $formatted_accessed_at, $formatted_changed_at";
    # Store the output of the stat command in a variable
    #output="$(stat $1)";
    #output=($output);
    # Use command substitution to split the output into an array
    #readarray -t stat_array <<< "$output";
    # Print the array elements
    #for element in "${stat_array[@]}"; do
     #   if [[ $element == *"Access:"* ]]; then
      #      echo "access";
       # fi
    #    if [[ $element == *"Modify:"* ]]; then
     #       echo "modify";
      #  fi
       # if [[ $element == *"Change:"* ]]; then
        #    echo "change";
        #fi
      #  if [[ $element == *"Birth:"* ]]; then
       #     echo "born";
       # fi
    #done
    #X E.
}
#X E.
It directly gets formatted stuff now. Now just writing to a file. I should probably eliminate some variables but do not know how. X E.
 
I think what I may do is line by line, path, then next line a timestamp and next line another timestamp and so on. I do not know how to do file read and write yet and I can't seem to get \n to appear as new line so can you help @Antero360? X E.
 
Now I have this function.
Bash:
function writeXE() {
    echo $1;
    # Get the file's creation time using stat
    created_at=$(stat -c %W $1);
    # Get the timestamps using stat
    modified_at=$(stat -c %y $1)
    accessed_at=$(stat -c %x $1)
    changed_at=$(stat -c %z $1)
    # Format the creation time using the date command
    formatted_created_at=$(date -d @$created_at +"%Y-%m-%d %H:%M:%S");
    # Format the timestamps using date
    formatted_modified_at=$(date -d "$modified_at" +"%Y-%m-%d %H:%M:%S");
    formatted_accessed_at=$(date -d "$accessed_at" +"%Y-%m-%d %H:%M:%S");
    formatted_changed_at=$(date -d "$changed_at" +"%Y-%m-%d %H:%M:%S");
    echo "$formatted_created_at, $formatted_modified_at, $formatted_accessed_at, $formatted_changed_at";
    # Store the output of the stat command in a variable
    #output="$(stat $1)";
    #output=($output);
    # Use command substitution to split the output into an array
    #readarray -t stat_array <<< "$output";
    # Print the array elements
    #for element in "${stat_array[@]}"; do
     #   if [[ $element == *"Access:"* ]]; then
      #      echo "access";
       # fi
    #    if [[ $element == *"Modify:"* ]]; then
     #       echo "modify";
      #  fi
       # if [[ $element == *"Change:"* ]]; then
        #    echo "change";
        #fi
      #  if [[ $element == *"Birth:"* ]]; then
       #     echo "born";
       # fi
    #done
    #X E.
}
#X E.
It directly gets formatted stuff now. Now just writing to a file. I should probably eliminate some variables but do not know how. X E.
in order to save to a file, you can redirect that echo to the file like so

echo "ggggjuh" >> file.txt
 
Okay, so that is appending and I will be starting with a new file each time. That works. Will each call to echo make a new line? I never found out how to make a new line. X E.
 
Like my current code.
Bash:
#!/bin/bash
#12/29/2023, 5:59PM, started, X E.
function startXE() {
    if [ -f "$1propsXE.txt" ]; then
        echo "$1propsXE.txt exists";
        rm i "$1propsXE.txt";
        echo "File deleted";
    else
        echo "$1propsXE.txt does not exist";
    fi
    recurseXE $1 "$1propsXE.txt";
    echo "" >> "$1propsXE.txt";
    echo "X E." >> "$1propsXE.txt";
    #X E.
}
#X E.
function recurseXE() {
    #save list of file name to variable
    manifest="$(ls -a $1 | grep -v /)";
    #echo "$(stat $1)";
    writeXE $1 $2;
    #iterate through each file name and pass it to stat to get birth, access, modify info
    while IFS= read -r line; do
        if [[ "$line" != ".." && "$line" != "." ]]; then
            if [ -d "$1$line" ]; then
                if [[ "$1$line" == */ ]]; then
                    recurseXE "$1$line";
                else
                    recurseXE "$1$line/";
                fi
                #echo "$line";
            else
                writeXE $1 $2;
            fi
        #echo "$1$line"
        fi
    done <<< $manifest
    #X E.
}
#X E.
function writeXE() {
    echo $1 >> $2;
    # Get the file's creation time using stat
    created_at=$(stat -c %W $1);
    # Get the timestamps using stat
    modified_at=$(stat -c %y $1)
    accessed_at=$(stat -c %x $1)
    changed_at=$(stat -c %z $1)
    # Format the creation time using the date command
    formatted_created_at=$(date -d @$created_at +"%Y-%m-%d %H:%M:%S");
    # Format the timestamps using date
    formatted_modified_at=$(date -d "$modified_at" +"%Y-%m-%d %H:%M:%S");
    formatted_accessed_at=$(date -d "$accessed_at" +"%Y-%m-%d %H:%M:%S");
    formatted_changed_at=$(date -d "$changed_at" +"%Y-%m-%d %H:%M:%S");
    echo $formatted_created_at >> $2;
    echo $formatted_modified_at >> $2;
    echo $formatted_accessed_at >> $2;
    echo $formatted_changed_at >> $2;
    # Store the output of the stat command in a variable
    #output="$(stat $1)";
    #output=($output);
    # Use command substitution to split the output into an array
    #readarray -t stat_array <<< "$output";
    # Print the array elements
    #for element in "${stat_array[@]}"; do
     #   if [[ $element == *"Access:"* ]]; then
      #      echo "access";
       # fi
    #    if [[ $element == *"Modify:"* ]]; then
     #       echo "modify";
      #  fi
       # if [[ $element == *"Change:"* ]]; then
        #    echo "change";
        #fi
      #  if [[ $element == *"Birth:"* ]]; then
       #     echo "born";
       # fi
    #done
    #X E.
}
#X E.
if [ $# -gt 0 ]; then
    if [ -d "$1" ]; then
        if [[ "$1" == */ ]]; then
            startXE "$1";
        else
            startXE "$1/";
        fi
    fi
else
    if [[ "$(pwd)" == */ ]]; then
        startXE "$(pwd)";
    else
        startXE "$(pwd)/";
    fi
fi
#X E.
How does this look and should I run it? X E.
 
Like my current code.
Bash:
#!/bin/bash
#12/29/2023, 5:59PM, started, X E.
function startXE() {
    if [ -f "$1propsXE.txt" ]; then
        echo "$1propsXE.txt exists";
        rm i "$1propsXE.txt";
        echo "File deleted";
    else
        echo "$1propsXE.txt does not exist";
    fi
    recurseXE $1 "$1propsXE.txt";
    echo "" >> "$1propsXE.txt";
    echo "X E." >> "$1propsXE.txt";
    #X E.
}
#X E.
function recurseXE() {
    #save list of file name to variable
    manifest="$(ls -a $1 | grep -v /)";
    #echo "$(stat $1)";
    writeXE $1 $2;
    #iterate through each file name and pass it to stat to get birth, access, modify info
    while IFS= read -r line; do
        if [[ "$line" != ".." && "$line" != "." ]]; then
            if [ -d "$1$line" ]; then
                if [[ "$1$line" == */ ]]; then
                    recurseXE "$1$line";
                else
                    recurseXE "$1$line/";
                fi
                #echo "$line";
            else
                writeXE $1 $2;
            fi
        #echo "$1$line"
        fi
    done <<< $manifest
    #X E.
}
#X E.
function writeXE() {
    echo $1 >> $2;
    # Get the file's creation time using stat
    created_at=$(stat -c %W $1);
    # Get the timestamps using stat
    modified_at=$(stat -c %y $1)
    accessed_at=$(stat -c %x $1)
    changed_at=$(stat -c %z $1)
    # Format the creation time using the date command
    formatted_created_at=$(date -d @$created_at +"%Y-%m-%d %H:%M:%S");
    # Format the timestamps using date
    formatted_modified_at=$(date -d "$modified_at" +"%Y-%m-%d %H:%M:%S");
    formatted_accessed_at=$(date -d "$accessed_at" +"%Y-%m-%d %H:%M:%S");
    formatted_changed_at=$(date -d "$changed_at" +"%Y-%m-%d %H:%M:%S");
    echo $formatted_created_at >> $2;
    echo $formatted_modified_at >> $2;
    echo $formatted_accessed_at >> $2;
    echo $formatted_changed_at >> $2;
    # Store the output of the stat command in a variable
    #output="$(stat $1)";
    #output=($output);
    # Use command substitution to split the output into an array
    #readarray -t stat_array <<< "$output";
    # Print the array elements
    #for element in "${stat_array[@]}"; do
     #   if [[ $element == *"Access:"* ]]; then
      #      echo "access";
       # fi
    #    if [[ $element == *"Modify:"* ]]; then
     #       echo "modify";
      #  fi
       # if [[ $element == *"Change:"* ]]; then
        #    echo "change";
        #fi
      #  if [[ $element == *"Birth:"* ]]; then
       #     echo "born";
       # fi
    #done
    #X E.
}
#X E.
if [ $# -gt 0 ]; then
    if [ -d "$1" ]; then
        if [[ "$1" == */ ]]; then
            startXE "$1";
        else
            startXE "$1/";
        fi
    fi
else
    if [[ "$(pwd)" == */ ]]; then
        startXE "$(pwd)";
    else
        startXE "$(pwd)/";
    fi
fi
#X E.
How does this look and should I run it? X E.
give it a shot
 

New Threads

Buy us a coffee!

Back
Top Bottom